英文:
My program decodes a QR code standalone but when I integrate it into Tkinter GUI, it does not
问题
使用网络摄像头和pyzbar库,我尝试识别QR码的内容,网络摄像头可以看到QR码,但无法进行识别,请帮助我解决这个问题,以下是代码:
def qr_webcam_reader():
ret, frame = capture.read()
if ret:
frame = cv2.resize(frame, (400, 280))
img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
img = Image.fromarray(img)
imgtk = ImageTk.PhotoImage(image=img)
LabelCamera.imgtk = imgtk
LabelCamera.configure(image=imgtk)
for code in decode(frame):
decoded_data = code.data.decode("utf-8")
rect_pts = code.rect
if decoded_data:
pts = np.array([code.polygon], np.int32)
cv2.polylines(frame, [pts], True, (255, 0, 0), 3)
cv2.putText(frame, str(decoded_data), (rect_pts[0], rect_pts[1]), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 255, 0), 2)
win.after(10, qr_webcam_reader)
下面的代码有效,但对我来说很重要的是要使用tkinter并将读取的代码放入数据库中后显示在标签中:
cap = cv2.VideoCapture(0)
while True:
success, frame = cap.read()
if not success:
break
for code in decode(frame):
decoded_data = code.data.decode("utf-8")
rect_pts = code.rect
if decoded_data:
pts = np.array([code.polygon], np.int32)
cv2.polylines(frame, [pts], True, (255, 0, 0), 3)
cv2.putText(frame, str(decoded_data), (rect_pts[0], rect_pts[1]), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 255, 0), 2)
cv2.imshow("camera", frame)
cv2.waitKey(1)
cap.release()
英文:
using a web camera and the pyzbar library, I am trying to recognize the contents of the QR code, the web camera sees the QR code, but recognition does not occur, please help me figure it out, below is the code
def qr_webcam_reader():
ret, frame = capture.read()
if ret:
frame = cv2.resize(frame, (400, 280))
img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
img = Image.fromarray(img)
imgtk = ImageTk.PhotoImage(image=img)
LabelCamera.imgtk = imgtk
LabelCamera.configure(image=imgtk)
for code in decode(frame):
decoded_data = code.data.decode("utf-8")
rect_pts = code.rect
if decoded_data:
pts = np.array([code.polygon], np.int32)
cv2.polylines(frame, [pts], True, (255, 0, 0), 3)
cv2.putText(frame, str(decoded_data), (rect_pts[0], rect_pts[1]), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 255, 0), 2)
win.after(10, qr_webcam_reader)
the code below works, but it is important for me to do it with tkinter and read the code in the label after putting it in the database
cap = cv2.VideoCapture(0)
while True:
success, frame = cap.read()
if not success:
break
for code in decode(frame):
decoded_data = code.data.decode("utf-8")
rect_pts = code.rect
if decoded_data:
pts = np.array([code.polygon], np.int32)
cv2.polylines(frame, [pts], True, (255, 0, 0), 3)
cv2.putText(frame, str(decoded_data), (rect_pts[0], rect_pts[1]), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 255, 0), 2)
cv2.imshow("camera", frame)
cv2.waitKey(1)
cap.release()
答案1
得分: 1
你需要按照以下顺序执行任务:
- 调整捕获的帧大小
- 解码调整大小后的帧
- 将帧转换为PIL图像
- 显示图像
def qr_webcam_reader():
ret, frame = capture.read()
if ret:
# 调整帧大小
frame = cv2.resize(frame, (400, 280))
# 解码帧
for code in decode(frame):
decoded_data = code.data.decode("utf-8")
if decoded_data:
rect_pts = code.rect
pts = np.array([code.polygon], np.int32)
cv2.polylines(frame, [pts], True, (255, 0, 0), 3)
cv2.putText(frame, str(decoded_data), (rect_pts[0], rect_pts[1]), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 255, 0), 2)
# 将帧转换为PIL图像
img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
img = Image.fromarray(img)
# 显示图像
imgtk = ImageTk.PhotoImage(image=img)
LabelCamera.imgtk = imgtk
LabelCamera.configure(image=imgtk)
win.after(10, qr_webcam_reader)
英文:
You need to do the tasks in this order:
- resize the captured frame
- decode the resized frame
- convert the frame to PIL image
- show the image
def qr_webcam_reader():
ret, frame = capture.read()
if ret:
# resize frame
frame = cv2.resize(frame, (400, 280))
# decode frame
for code in decode(frame):
decoded_data = code.data.decode("utf-8")
if decoded_data:
rect_pts = code.rect
pts = np.array([code.polygon], np.int32)
cv2.polylines(frame, [pts], True, (255, 0, 0), 3)
cv2.putText(frame, str(decoded_data), (rect_pts[0], rect_pts[1]), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 255, 0), 2)
# convert the frame to PILL image
img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
img = Image.fromarray(img)
# show the image
imgtk = ImageTk.PhotoImage(image=img)
LabelCamera.imgtk = imgtk
LabelCamera.configure(image=imgtk)
win.after(10, qr_webcam_reader)
答案2
得分: 0
def qr_webcam_reader():
success, frame = capture.read()
if not success:
return # `return` instead of `break`
for code in decode(frame):
decoded_data = code.data.decode("utf-8")
rect_pts = code.rect
if decoded_data:
pts = np.array([code.polygon], np.int32)
cv2.polylines(frame, [pts], True, (255, 0, 0), 3)
cv2.putText(frame, str(decoded_data), (rect_pts[0], rect_pts[1]), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 255, 0), 2)
# 替换为 `cv.imshow()`
frame = cv2.resize(frame, (400, 280))
img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
img = Image.fromarray(img)
imgtk = ImageTk.PhotoImage(image=img)
LabelCamera.imgtk = imgtk
LabelCamera.configure(image=imgtk)
# ---
win.after(10, qr_webcam_reader)
英文:
I can't test it but I think you should add image to Label
at the end of function qr_webcam_reader()
- similar to cv.imshow()
at the end of loop while True
in original code.
def qr_webcam_reader():
success, frame = capture.read()
if not success:
return # `return` instead of `break`
for code in decode(frame):
decoded_data = code.data.decode("utf-8")
rect_pts = code.rect
if decoded_data:
pts = np.array([code.polygon], np.int32)
cv2.polylines(frame, [pts], True, (255, 0, 0), 3)
cv2.putText(frame, str(decoded_data), (rect_pts[0], rect_pts[1]), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 255, 0), 2)
# instead of `cv.imshow()`
frame = cv2.resize(frame, (400, 280))
img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
img = Image.fromarray(img)
imgtk = ImageTk.PhotoImage(image=img)
LabelCamera.imgtk = imgtk
LabelCamera.configure(image=imgtk)
# ---
win.after(10, qr_webcam_reader)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论