12from wechatqrcode
import WeChatQRCode
15 if v.lower()
in [
'on',
'yes',
'true',
'y',
't']:
17 elif v.lower()
in [
'off',
'no',
'false',
'n',
'f']:
20 raise NotImplementedError
22parser = argparse.ArgumentParser(
23 description=
"WeChat QR code detector for detecting and parsing QR code (https://github.com/opencv/opencv_contrib/tree/master/modules/wechat_qrcode)")
24parser.add_argument(
'--input',
'-i', type=str, help=
'Path to the input image. Omit for using default camera.')
25parser.add_argument(
'--detect_prototxt_path', type=str, default=
'detect_2021sep.prototxt', help=
'Path to detect.prototxt.')
26parser.add_argument(
'--detect_model_path', type=str, default=
'detect_2021sep.caffemodel', help=
'Path to detect.caffemodel.')
27parser.add_argument(
'--sr_prototxt_path', type=str, default=
'sr_2021sep.prototxt', help=
'Path to sr.prototxt.')
28parser.add_argument(
'--sr_model_path', type=str, default=
'sr_2021sep.caffemodel', help=
'Path to sr.caffemodel.')
29parser.add_argument(
'--save',
'-s', type=str2bool, default=
False, help=
'Set true to save results. This flag is invalid when using camera.')
30parser.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.')
31args = parser.parse_args()
33def visualize(image, res, points, points_color=(0, 255, 0), text_color=(0, 255, 0), fps=
None):
35 h, w, _ = output.shape
38 cv.putText(output,
'FPS: {:.2f}'.format(fps), (0, 15), cv.FONT_HERSHEY_SIMPLEX, 0.5, text_color)
42 for r, p
in zip(res, points):
43 p = p.astype(np.int32)
45 cv.circle(output, _p, 10, points_color, -1)
47 qrcode_center_x =
int((p[0][0] + p[2][0]) / 2)
48 qrcode_center_y =
int((p[0][1] + p[2][1]) / 2)
50 text_size, baseline = cv.getTextSize(r, cv.FONT_HERSHEY_DUPLEX, fontScale, fontSize)
51 text_x = qrcode_center_x -
int(text_size[0] / 2)
52 text_y = qrcode_center_y -
int(text_size[1] / 2)
53 cv.putText(output,
'{}'.format(r), (text_x, text_y), cv.FONT_HERSHEY_DUPLEX, fontScale, text_color, fontSize)
58if __name__ ==
'__main__':
61 args.detect_model_path,
62 args.sr_prototxt_path,
66 if args.input
is not None:
67 image = cv.imread(args.input)
68 res, points = model.infer(image)
79 print(
'Results saved to result.jpg\n')
80 cv.imwrite(
'result.jpg', image)
84 cv.namedWindow(args.input, cv.WINDOW_AUTOSIZE)
85 cv.imshow(args.input, image)
89 cap = cv.VideoCapture(deviceId)
92 while cv.waitKey(1) < 0:
93 hasFrame, frame = cap.read()
95 print(
'No frames grabbed!')
100 res, points = model.infer(frame)
105 frame =
visualize(frame, res, points, fps=fps)
108 cv.imshow(
'WeChatQRCode Demo', frame)
visualize(image, results, box_color=(0, 255, 0), text_color=(0, 0, 255), fps=None)