JeVoisBase  1.18
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
db.py
Go to the documentation of this file.
1 # This file is part of OpenCV Zoo project.
2 # It is subject to the license terms in the LICENSE file found in the same directory.
3 #
4 # Copyright (C) 2021, Shenzhen Institute of Artificial Intelligence and Robotics for Society, all rights reserved.
5 # Third party copyrights are property of their respective owners.
6 
7 import numpy as np
8 import cv2 as cv
9 
10 class DB:
11  def __init__(self, modelPath, inputSize=[736, 736], binaryThreshold=0.3, polygonThreshold=0.5, maxCandidates=200, unclipRatio=2.0, backendId=0, targetId=0):
12  self._modelPath = modelPath
13  self._model = cv.dnn_TextDetectionModel_DB(
14  cv.dnn.readNet(self._modelPath)
15  )
16 
17  self._inputSize = tuple(inputSize) # (w, h)
18  self._inputHeight = inputSize[0]
19  self._inputWidth = inputSize[1]
20  self._binaryThreshold = binaryThreshold
21  self._polygonThreshold = polygonThreshold
22  self._maxCandidates = maxCandidates
23  self._unclipRatio = unclipRatio
24  self._backendId = backendId
25  self._targetId = targetId
26 
27  self._model.setPreferableBackend(self._backendId)
28  self._model.setPreferableTarget(self._targetId)
29 
30  self._model.setBinaryThreshold(self._binaryThreshold)
31  self._model.setPolygonThreshold(self._polygonThreshold)
32  self._model.setUnclipRatio(self._unclipRatio)
33  self._model.setMaxCandidates(self._maxCandidates)
34 
35  self._model.setInputParams(1.0/255.0, self._inputSize, (122.67891434, 116.66876762, 104.00698793))
36 
37  @property
38  def name(self):
39  return self.__class__.__name__
40 
41  def setBackend(self, backend):
42  self._backendId = backend
43  self._model.setPreferableBackend(self._backendId)
44 
45  def setTarget(self, target):
46  self._targetId = target
47  self._model.setPreferableTarget(self._targetId)
48 
49  def setInputSize(self, input_size):
50  self._inputSize = tuple(input_size)
51  self._model.setInputParams(1.0/255.0, self._inputSize, (122.67891434, 116.66876762, 104.00698793))
52 
53  def infer(self, image):
54  assert image.shape[0] == self._inputSize[1], '{} (height of input image) != {} (preset height)'.format(image.shape[0], self._inputSize[1])
55  assert image.shape[1] == self._inputSize[0], '{} (width of input image) != {} (preset width)'.format(image.shape[1], self._inputSize[0])
56 
57  return self._model.detect(image)
58 
db.DB._inputSize
_inputSize
Definition: db.py:17
db.DB._polygonThreshold
_polygonThreshold
Definition: db.py:21
db.DB.setInputSize
def setInputSize(self, input_size)
Definition: db.py:49
db.DB.name
def name(self)
Definition: db.py:38
db.DB._model
_model
Definition: db.py:13
db.DB
Definition: db.py:10
db.DB._inputWidth
_inputWidth
Definition: db.py:19
db.DB._targetId
_targetId
Definition: db.py:25
db.DB._modelPath
_modelPath
Definition: db.py:12
db.DB._binaryThreshold
_binaryThreshold
Definition: db.py:20
db.DB._unclipRatio
_unclipRatio
Definition: db.py:23
db.DB.setBackend
def setBackend(self, backend)
Definition: db.py:41
db.DB.__init__
def __init__(self, modelPath, inputSize=[736, 736], binaryThreshold=0.3, polygonThreshold=0.5, maxCandidates=200, unclipRatio=2.0, backendId=0, targetId=0)
Definition: db.py:11
db.DB.setTarget
def setTarget(self, target)
Definition: db.py:45
db.DB._inputHeight
_inputHeight
Definition: db.py:18
db.DB._backendId
_backendId
Definition: db.py:24
db.DB._maxCandidates
_maxCandidates
Definition: db.py:22
db.DB.infer
def infer(self, image)
Definition: db.py:53