15 if v.lower()
in [
'on',
'yes',
'true',
'y',
't']:
17 elif v.lower()
in [
'off',
'no',
'false',
'n',
'f']:
20 raise NotImplementedError
22backends = [cv.dnn.DNN_BACKEND_OPENCV, cv.dnn.DNN_BACKEND_CUDA]
23targets = [cv.dnn.DNN_TARGET_CPU, cv.dnn.DNN_TARGET_CUDA, cv.dnn.DNN_TARGET_CUDA_FP16]
24help_msg_backends =
"Choose one of the computation backends: {:d}: OpenCV implementation (default); {:d}: CUDA"
25help_msg_targets =
"Chose one of the target computation devices: {:d}: CPU (default); {:d}: CUDA; {:d}: CUDA fp16"
27 backends += [cv.dnn.DNN_BACKEND_TIMVX]
28 targets += [cv.dnn.DNN_TARGET_NPU]
29 help_msg_backends +=
"; {:d}: TIMVX"
30 help_msg_targets +=
"; {:d}: NPU"
32 print(
'This version of OpenCV does not support TIM-VX and NPU. Visit https://gist.github.com/fengyuentau/5a7a5ba36328f2b763aea026c43fa45f for more information.')
34parser = argparse.ArgumentParser(description=
'Real-time Scene Text Detection with Differentiable Binarization (https://arxiv.org/abs/1911.08947).')
35parser.add_argument(
'--input',
'-i', type=str, help=
'Path to the input image. Omit for using default camera.')
36parser.add_argument(
'--model',
'-m', type=str, default=
'text_detection_DB_TD500_resnet18_2021sep.onnx', help=
'Path to the model.')
37parser.add_argument(
'--backend',
'-b', type=int, default=backends[0], help=help_msg_backends.format(*backends))
38parser.add_argument(
'--target',
'-t', type=int, default=targets[0], help=help_msg_targets.format(*targets))
39parser.add_argument(
'--width', type=int, default=736,
40 help=
'Preprocess input image by resizing to a specific width. It should be multiple by 32.')
41parser.add_argument(
'--height', type=int, default=736,
42 help=
'Preprocess input image by resizing to a specific height. It should be multiple by 32.')
43parser.add_argument(
'--binary_threshold', type=float, default=0.3, help=
'Threshold of the binary map.')
44parser.add_argument(
'--polygon_threshold', type=float, default=0.5, help=
'Threshold of polygons.')
45parser.add_argument(
'--max_candidates', type=int, default=200, help=
'Max candidates of polygons.')
46parser.add_argument(
'--unclip_ratio', type=np.float64, default=2.0, help=
' The unclip ratio of the detected text region, which determines the output size.')
47parser.add_argument(
'--save',
'-s', type=str, default=
False, help=
'Set true to save results. This flag is invalid when using camera.')
48parser.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.')
49args = parser.parse_args()
51def visualize(image, results, box_color=(0, 255, 0), text_color=(0, 0, 255), isClosed=
True, thickness=2, fps=
None):
55 cv.putText(output,
'FPS: {:.2f}'.format(fps), (0, 15), cv.FONT_HERSHEY_SIMPLEX, 0.5, text_color)
57 pts = np.array(results[0])
58 output = cv.polylines(output, pts, isClosed, box_color, thickness)
62if __name__ ==
'__main__':
64 model =
DB(modelPath=args.model,
65 inputSize=[args.width, args.height],
66 binaryThreshold=args.binary_threshold,
67 polygonThreshold=args.polygon_threshold,
68 maxCandidates=args.max_candidates,
69 unclipRatio=args.unclip_ratio,
70 backendId=args.backend,
75 if args.input
is not None:
76 image = cv.imread(args.input)
77 image = cv.resize(image, [args.width, args.height])
80 results = model.infer(image)
83 print(
'{} texts detected.'.format(len(results[0])))
84 for idx, (bbox, score)
in enumerate(zip(results[0], results[1])):
85 print(
'{}: {} {} {} {}, {:.2f}'.format(idx, bbox[0], bbox[1], bbox[2], bbox[3], score))
92 print(
'Resutls saved to result.jpg\n')
93 cv.imwrite(
'result.jpg', image)
97 cv.namedWindow(args.input, cv.WINDOW_AUTOSIZE)
98 cv.imshow(args.input, image)
102 cap = cv.VideoCapture(deviceId)
105 while cv.waitKey(1) < 0:
106 hasFrame, frame = cap.read()
108 print(
'No frames grabbed!')
111 frame = cv.resize(frame, [args.width, args.height])
114 results = model.infer(frame)
118 frame =
visualize(frame, results, fps=tm.getFPS())
121 cv.imshow(
'{} Demo'.format(model.name), frame)
visualize(image, results, box_color=(0, 255, 0), text_color=(0, 0, 255), fps=None)