-
Notifications
You must be signed in to change notification settings - Fork 326
/
qrcode_wechatqrcode.py
executable file
·245 lines (199 loc) · 7.54 KB
/
qrcode_wechatqrcode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
from http.client import NON_AUTHORITATIVE_INFORMATION
from math import sqrt
import cv2
import sys
import time
import numpy as np
import ailia
from qrcode_wechatqrcode_utils import preprocess, postprocess
# import original modules
sys.path.append('../../util')
from arg_utils import get_base_parser, update_parser, get_savepath # noqa: E402
from model_utils import check_and_download_models # noqa: E402
import webcamera_utils # noqa: E402
# logger
from logging import getLogger # noqa: E402
logger = getLogger(__name__)
# ======================
# Parameters
# ======================
MODEL_PARAMS = {
'detect_2021nov': {'input_shape': [384, 384]},
'sr_2021nov': {'input_shape': [224, 224], 'threshold': 160},
}
REMOTE_PATH = 'https://storage.googleapis.com/ailia-models/qrcode_wechatqrcode/'
IMAGE_PATH = 'input.jpg'
SAVE_IMAGE_PATH = 'output.jpg'
# ======================
# Arguemnt Parser Config
# ======================
parser = get_base_parser('QR detection model', IMAGE_PATH, SAVE_IMAGE_PATH)
parser.add_argument(
'--detect_model_name',
help='The model name of QR detection.',
default='detect_2021nov',
)
parser.add_argument(
'--sr_model_name',
help='The model name of super resolution.',
default='sr_2021nov',
)
parser.add_argument(
'-w', '--write_prediction',
action='store_true',
help='Flag to output the prediction file.'
)
parser.add_argument(
'--decode_qrcode',
action='store_true',
help='Decode qrcode using zbar.'
)
args = update_parser(parser)
if args.decode_qrcode:
import pyzbar.pyzbar as zbar
DETECT_MODEL_NAME = args.detect_model_name
DETECT_WEIGHT_PATH = "detect" + ".caffemodel"
DETECT_MODEL_PATH = "detect" + ".prototxt"
DETECT_HEIGHT = MODEL_PARAMS[DETECT_MODEL_NAME]['input_shape'][0]
DETECT_WIDTH = MODEL_PARAMS[DETECT_MODEL_NAME]['input_shape'][1]
SR_MODEL_NAME = args.sr_model_name
SR_WEIGHT_PATH = "sr" + ".caffemodel"
SR_MODEL_PATH = "sr" + ".prototxt"
SR_HEIGHT = MODEL_PARAMS[SR_MODEL_NAME]['input_shape'][0]
SR_WIDTH = MODEL_PARAMS[SR_MODEL_NAME]['input_shape'][1]
SR_TH = MODEL_PARAMS[SR_MODEL_NAME]['threshold']
def scale_image(img, scale, sr):
if scale == 1.0:
return img
elif scale == 2.0:
h, w, _ = img.shape
if sqrt(w * h * 1.0) < SR_TH:
img = preprocess(img, (SR_HEIGHT, SR_WIDTH))
res = sr.run(img[None, None, :, :])
result = np.clip(res[0][0][0] * 255, 0, 255.0).astype(np.int8)
return result
else:
return cv2.resize(img, None, fx=scale, fy=scale, interpolation=cv2.INTER_CUBIC)
elif scale < 1.0:
return cv2.resize(img, None, fx=scale, fy=scale, interpolation=cv2.INTER_AREA)
return None
def get_scale_list(width, height):
if width < 320 or height < 320:
return [1.0, 2.0, 0.5]
elif width < 640 and height < 640:
return [1.0, 0.5]
else:
return [0.5, 1.0]
def decode(raw_img, detections, sr):
PADDING = 0.1
MIN_PADDING = 15.0
decoded = []
for d in detections:
padx = int(max(PADDING * d.w, MIN_PADDING))
pady = int(max(PADDING * d.h, MIN_PADDING))
left = max(d.x - padx, 0)
top = max(d.y - pady, 0)
right = min(d.x + d.w + padx, raw_img.shape[1])
bottom = min(d.y + d.h + pady, raw_img.shape[0])
cropped = raw_img[top:bottom, left:right, :].copy()
ch, cw, _ = cropped.shape
scales = get_scale_list(cw, ch)
for scale in scales:
scaled_img = scale_image(cropped, scale, sr)
text = None
if args.decode_qrcode:
qr = zbar.decode(scaled_img)
if len(qr) > 0:
text = qr[0].data.decode()
if not args.decode_qrcode or text:
obj = {
'left': d.x,
'top': d.y,
'right': d.x + d.w,
'bottom': d.y + d.h,
'text': text
}
decoded.append(obj)
break
return decoded
def visualize(raw_img, decoded):
result_img = raw_img.copy()
for d in decoded:
cv2.putText(result_img, d['text'], (d['left'], d['bottom']), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), thickness=2)
cv2.rectangle(result_img, (d['left'], d['top']), (d['right'], d['bottom']), (0, 0, 255), thickness=3)
return result_img
def recognize_from_image(detection, sr):
# input image loop
for image_path in args.input:
# prepare input data
logger.info(image_path)
raw_img = cv2.imread(image_path)
logger.debug(f'input image shape: {raw_img.shape}')
img = preprocess(raw_img, (DETECT_HEIGHT, DETECT_WIDTH))
# inference
logger.info('Start inference...')
res = None
if args.benchmark:
logger.info('BENCHMARK mode')
total_time = 0
for i in range(args.benchmark_count):
start = int(round(time.time() * 1000))
res = detection.run(img[None, None, :, :])
end = int(round(time.time() * 1000))
if i != 0:
total_time = total_time + (end - start)
logger.info(f'\tailia processing time {end - start} ms')
logger.info(f'\taverage time {total_time / (args.benchmark_count-1)} ms')
else:
res = detection.run(img[None, None, :, :])
detections = postprocess(img, raw_img.shape, res)
decoded = decode(raw_img, detections, sr)
result_img = visualize(raw_img, decoded)
# cv2.imshow("QR", result_img)
# cv2.waitKey()
savepath = get_savepath(args.savepath, image_path)
cv2.imwrite(savepath, result_img)
logger.info(f'saved at : {savepath}')
logger.info('Script finished successfully.')
def recognize_from_video(detection, sr):
capture = webcamera_utils.get_capture(args.video)
# create video writer if savepath is specified as video format
if args.savepath != SAVE_IMAGE_PATH:
f_h = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
f_w = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH))
writer = webcamera_utils.get_writer(args.savepath, f_h, f_w)
else:
writer = None
while(True):
ret, raw_frame = capture.read()
if (cv2.waitKey(1) & 0xFF == ord('q')) or not ret:
break
frame = preprocess(raw_frame, (DETECT_HEIGHT, DETECT_WIDTH))
res = detection.run(frame[None, None, :, :])
detections = postprocess(frame, raw_frame.shape, res)
decoded = decode(raw_frame, detections, sr)
result_frame = visualize(raw_frame, decoded)
cv2.imshow('frame', result_frame)
# save results
if writer is not None:
writer.write(result_frame)
capture.release()
cv2.destroyAllWindows()
if writer is not None:
writer.release()
logger.info('Script finished successfully.')
def main():
# model files check and download
check_and_download_models(DETECT_WEIGHT_PATH, DETECT_MODEL_PATH, REMOTE_PATH)
check_and_download_models(SR_WEIGHT_PATH, SR_MODEL_PATH, REMOTE_PATH)
env_id = args.env_id
detection = ailia.Net(DETECT_MODEL_PATH, DETECT_WEIGHT_PATH, env_id=env_id)
detection.set_input_shape((1, 1, DETECT_WIDTH, DETECT_HEIGHT))
sr = ailia.Net(SR_MODEL_PATH, SR_WEIGHT_PATH, env_id=env_id)
sr.set_input_shape((1, 1, SR_WIDTH, SR_HEIGHT))
if args.video is not None:
recognize_from_video(detection, sr)
else:
recognize_from_image(detection, sr)
if __name__ == '__main__':
main()