Add options for demo scripts to select backend & targets (#43)
Browse files* add options for selecting backend & targets
* add eol
crnn.py
CHANGED
@@ -8,10 +8,17 @@ import numpy as np
|
|
8 |
import cv2 as cv
|
9 |
|
10 |
class CRNN:
|
11 |
-
def __init__(self, modelPath, charsetPath):
|
12 |
self._model_path = modelPath
|
|
|
|
|
|
|
|
|
13 |
self._model = cv.dnn.readNet(self._model_path)
|
14 |
-
self.
|
|
|
|
|
|
|
15 |
self._inputSize = [100, 32] # Fixed
|
16 |
self._targetVertices = np.array([
|
17 |
[0, self._inputSize[1] - 1],
|
@@ -33,10 +40,12 @@ class CRNN:
|
|
33 |
return charset
|
34 |
|
35 |
def setBackend(self, backend_id):
|
36 |
-
self.
|
|
|
37 |
|
38 |
def setTarget(self, target_id):
|
39 |
-
self.
|
|
|
40 |
|
41 |
def _preprocess(self, image, rbbox):
|
42 |
# Remove conf, reshape and ensure all is np.float32
|
@@ -81,4 +90,5 @@ class CRNN:
|
|
81 |
for i in range(len(text)):
|
82 |
if text[i] != '-' and (not (i > 0 and text[i] == text[i - 1])):
|
83 |
char_list.append(text[i])
|
84 |
-
return ''.join(char_list)
|
|
|
|
8 |
import cv2 as cv
|
9 |
|
10 |
class CRNN:
|
11 |
+
def __init__(self, modelPath, charsetPath, backendId=0, targetId=0):
|
12 |
self._model_path = modelPath
|
13 |
+
self._charsetPath = charsetPath
|
14 |
+
self._backendId = backendId
|
15 |
+
self._targetId = targetId
|
16 |
+
|
17 |
self._model = cv.dnn.readNet(self._model_path)
|
18 |
+
self._model.setPreferableBackend(self._backendId)
|
19 |
+
self._model.setPreferableTarget(self._targetId)
|
20 |
+
|
21 |
+
self._charset = self._load_charset(self._charsetPath)
|
22 |
self._inputSize = [100, 32] # Fixed
|
23 |
self._targetVertices = np.array([
|
24 |
[0, self._inputSize[1] - 1],
|
|
|
40 |
return charset
|
41 |
|
42 |
def setBackend(self, backend_id):
|
43 |
+
self._backendId = backend_id
|
44 |
+
self._model.setPreferableBackend(self._backendId)
|
45 |
|
46 |
def setTarget(self, target_id):
|
47 |
+
self._targetId = target_id
|
48 |
+
self._model.setPreferableTarget(self._targetId)
|
49 |
|
50 |
def _preprocess(self, image, rbbox):
|
51 |
# Remove conf, reshape and ensure all is np.float32
|
|
|
90 |
for i in range(len(text)):
|
91 |
if text[i] != '-' and (not (i > 0 and text[i] == text[i - 1])):
|
92 |
char_list.append(text[i])
|
93 |
+
return ''.join(char_list)
|
94 |
+
|
demo.py
CHANGED
@@ -23,10 +23,24 @@ def str2bool(v):
|
|
23 |
else:
|
24 |
raise NotImplementedError
|
25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
parser = argparse.ArgumentParser(
|
27 |
description="An End-to-End Trainable Neural Network for Image-based Sequence Recognition and Its Application to Scene Text Recognition (https://arxiv.org/abs/1507.05717)")
|
28 |
parser.add_argument('--input', '-i', type=str, help='Path to the input image. Omit for using default camera.')
|
29 |
parser.add_argument('--model', '-m', type=str, default='text_recognition_CRNN_EN_2021sep.onnx', help='Path to the model.')
|
|
|
|
|
30 |
parser.add_argument('--charset', '-c', type=str, default='charset_36_EN.txt', help='Path to the charset file corresponding to the selected model.')
|
31 |
parser.add_argument('--save', '-s', type=str, default=False, help='Set true to save results. This flag is invalid when using camera.')
|
32 |
parser.add_argument('--vis', '-v', type=str2bool, default=True, help='Set true to open a window for result visualization. This flag is invalid when using camera.')
|
@@ -50,7 +64,9 @@ if __name__ == '__main__':
|
|
50 |
binaryThreshold=0.3,
|
51 |
polygonThreshold=0.5,
|
52 |
maxCandidates=200,
|
53 |
-
unclipRatio=2.0
|
|
|
|
|
54 |
)
|
55 |
|
56 |
# If input is an image
|
@@ -118,4 +134,5 @@ if __name__ == '__main__':
|
|
118 |
print(texts)
|
119 |
|
120 |
# Visualize results in a new Window
|
121 |
-
cv.imshow('{} Demo'.format(recognizer.name), frame)
|
|
|
|
23 |
else:
|
24 |
raise NotImplementedError
|
25 |
|
26 |
+
backends = [cv.dnn.DNN_BACKEND_OPENCV, cv.dnn.DNN_BACKEND_CUDA]
|
27 |
+
targets = [cv.dnn.DNN_TARGET_CPU, cv.dnn.DNN_TARGET_CUDA, cv.dnn.DNN_TARGET_CUDA_FP16]
|
28 |
+
help_msg_backends = "Choose one of the computation backends: {:d}: OpenCV implementation (default); {:d}: CUDA"
|
29 |
+
help_msg_targets = "Chose one of the target computation devices: {:d}: CPU (default); {:d}: CUDA; {:d}: CUDA fp16"
|
30 |
+
try:
|
31 |
+
backends += [cv.dnn.DNN_BACKEND_TIMVX]
|
32 |
+
targets += [cv.dnn.DNN_TARGET_NPU]
|
33 |
+
help_msg_backends += "; {:d}: TIMVX"
|
34 |
+
help_msg_targets += "; {:d}: NPU"
|
35 |
+
except:
|
36 |
+
print('This version of OpenCV does not support TIM-VX and NPU. Visit https://gist.github.com/fengyuentau/5a7a5ba36328f2b763aea026c43fa45f for more information.')
|
37 |
+
|
38 |
parser = argparse.ArgumentParser(
|
39 |
description="An End-to-End Trainable Neural Network for Image-based Sequence Recognition and Its Application to Scene Text Recognition (https://arxiv.org/abs/1507.05717)")
|
40 |
parser.add_argument('--input', '-i', type=str, help='Path to the input image. Omit for using default camera.')
|
41 |
parser.add_argument('--model', '-m', type=str, default='text_recognition_CRNN_EN_2021sep.onnx', help='Path to the model.')
|
42 |
+
parser.add_argument('--backend', '-b', type=int, default=backends[0], help=help_msg_backends.format(*backends))
|
43 |
+
parser.add_argument('--target', '-t', type=int, default=targets[0], help=help_msg_targets.format(*targets))
|
44 |
parser.add_argument('--charset', '-c', type=str, default='charset_36_EN.txt', help='Path to the charset file corresponding to the selected model.')
|
45 |
parser.add_argument('--save', '-s', type=str, default=False, help='Set true to save results. This flag is invalid when using camera.')
|
46 |
parser.add_argument('--vis', '-v', type=str2bool, default=True, help='Set true to open a window for result visualization. This flag is invalid when using camera.')
|
|
|
64 |
binaryThreshold=0.3,
|
65 |
polygonThreshold=0.5,
|
66 |
maxCandidates=200,
|
67 |
+
unclipRatio=2.0,
|
68 |
+
backendId=args.backend,
|
69 |
+
targetId=args.target
|
70 |
)
|
71 |
|
72 |
# If input is an image
|
|
|
134 |
print(texts)
|
135 |
|
136 |
# Visualize results in a new Window
|
137 |
+
cv.imshow('{} Demo'.format(recognizer.name), frame)
|
138 |
+
|