【发布时间】:2020-05-27 04:04:17
【问题描述】:
我正在使用 Python(3.7) 和 OpenCV 2 进行一个项目,在该项目中我必须检测 QR 码并将其保存为图像,我已成功完成检测部分但不知道如何保存二维码作为图片?
这是我迄今为止尝试过的:
检测部分代码:
while True:
frame = vs.read()
frame = imutils.resize(frame, width=400)
barcodes = pyzbar.decode(frame)
for barcode in barcodes:
(x, y, w, h) = barcode.rect
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
barcodeData = barcode.data.decode("utf-8")
barcodeType = barcode.type
text = "{}".format(barcodeData)
cv2.putText(frame, '', (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
if barcodeData not in found:
csv.write("{}\n".format(barcodeData))
csv.flush()
found.clear()
found.add(barcodeData)
# Título do Frame
cv2.imshow("Live Stream Window", frame)
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
如何将检测到的区域(二维码)保存为图片?
更新:下面是自动svae图像的更新代码,但这不起作用。
while True:
frame = vs.read()
frame = imutils.resize(frame, width=400)
original = frame.copy()
barcodes = pyzbar.decode(frame)
barcode_num = 0
frame_dict = {'y': 0, 'w': 0, 'h': 0, 'x': 0}
for barcode in barcodes:
(x, y, w, h) = barcode.rect
print(f'{x}, {y}, {w}, {h}')
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
ROI = original[y:y + h, x:x + w]
frame_dict['y'] = y
frame_dict['x'] = x
frame_dict['h'] = h
frame_dict['w'] = w
barcode_num += 1
barcodeData = barcode.data.decode("utf-8")
print(barcodeData)
barcodeType = barcode.type
text = "{}".format(barcodeData)
cv2.putText(frame, '', (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
if barcodeData not in found:
csv.write("{}\n".format(barcodeData))
csv.flush()
found.clear()
found.add(barcodeData)
cv2.imshow("Live Stream Window", frame)
key = cv2.waitKey(1) & 0xFF
if key == ord("c"):
print('c is pressed')
ROI = original[frame_dict['y']:frame_dict['y'] + frame_dict['h'],
frame_dict['x']:frame_dict['x'] + frame_dict['w']]
cv2.imwrite('barcode_{}.png'.format(barcode_num), ROI)
pass
if key == ord("q"):
break
【问题讨论】:
-
这能回答你的问题吗? Python OpenCV live face detection crop saved
标签: python opencv machine-learning deep-learning computer-vision