15sys.path.append(
'../text_detection_db')
19 if v.lower()
in [
'on',
'yes',
'true',
'y',
't']:
21 elif v.lower()
in [
'off',
'no',
'false',
'n',
'f']:
24 raise NotImplementedError
26backends = [cv.dnn.DNN_BACKEND_OPENCV, cv.dnn.DNN_BACKEND_CUDA]
27targets = [cv.dnn.DNN_TARGET_CPU, cv.dnn.DNN_TARGET_CUDA, cv.dnn.DNN_TARGET_CUDA_FP16]
28help_msg_backends =
"Choose one of the computation backends: {:d}: OpenCV implementation (default); {:d}: CUDA"
29help_msg_targets =
"Chose one of the target computation devices: {:d}: CPU (default); {:d}: CUDA; {:d}: CUDA fp16"
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"
36 print(
'This version of OpenCV does not support TIM-VX and NPU. Visit https://gist.github.com/fengyuentau/5a7a5ba36328f2b763aea026c43fa45f for more information.')
38parser = 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)")
40parser.add_argument(
'--input',
'-i', type=str, help=
'Path to the input image. Omit for using default camera.')
41parser.add_argument(
'--model',
'-m', type=str, default=
'text_recognition_CRNN_EN_2021sep.onnx', help=
'Path to the model.')
42parser.add_argument(
'--backend',
'-b', type=int, default=backends[0], help=help_msg_backends.format(*backends))
43parser.add_argument(
'--target',
'-t', type=int, default=targets[0], help=help_msg_targets.format(*targets))
44parser.add_argument(
'--charset',
'-c', type=str, default=
'charset_36_EN.txt', help=
'Path to the charset file corresponding to the selected model.')
45parser.add_argument(
'--save',
'-s', type=str, default=
False, help=
'Set true to save results. This flag is invalid when using camera.')
46parser.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.')
47args = parser.parse_args()
49def visualize(image, boxes, texts, color=(0, 255, 0), isClosed=
True, thickness=2):
52 pts = np.array(boxes[0])
53 output = cv.polylines(output, pts, isClosed, color, thickness)
54 for box, text
in zip(boxes[0], texts):
55 cv.putText(output, text, (box[1].astype(np.int32)), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255))
58if __name__ ==
'__main__':
60 recognizer = CRNN(modelPath=args.model, charsetPath=args.charset)
62 detector = DB(modelPath=
'../text_detection_db/text_detection_DB_IC15_resnet18_2021sep.onnx',
68 backendId=args.backend,
73 if args.input
is not None:
74 image = cv.imread(args.input)
75 image = cv.resize(image, [args.width, args.height])
78 results = detector.infer(image)
80 for box, score
in zip(results[0], results[1]):
82 recognizer.infer(image, box.reshape(8))
90 print(
'Resutls saved to result.jpg\n')
91 cv.imwrite(
'result.jpg', image)
95 cv.namedWindow(args.input, cv.WINDOW_AUTOSIZE)
96 cv.imshow(args.input, image)
100 cap = cv.VideoCapture(deviceId)
103 while cv.waitKey(1) < 0:
104 hasFrame, frame = cap.read()
106 print(
'No frames grabbed!')
109 frame = cv.resize(frame, [736, 736])
112 results = detector.infer(frame)
114 cv.putText(frame,
'Latency - {}: {:.2f}'.format(detector.name, tm.getFPS()), (0, 15), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255))
118 if len(results[0])
and len(results[1]):
121 for box, score
in zip(results[0], results[1]):
123 (box.reshape(8), score)
126 recognizer.infer(frame, box.reshape(8))
129 cv.putText(frame,
'Latency - {}: {:.2f}'.format(recognizer.name, tm.getFPS()), (0, 30), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255))
137 cv.imshow(
'{} Demo'.format(recognizer.name), frame)
visualize(image, results, box_color=(0, 255, 0), text_color=(0, 0, 255), fps=None)