我一直在尝试创建一个tkinter顶级窗口,用于流式传输视频表单摄像头并执行QR扫描。我从SO和another code得到这个QR扫描代码,它只是从网络摄像头更新图像,而不是在tkinter标签上流式传输视频。python:tkinter显示来自网络摄像头的视频并执行QR扫描
我试图将这两者结合起来,这样一个顶层窗口带有标签,可以从网络摄像头更新图像,关闭按钮可以关闭顶层窗口。当它传输图像时,它可以扫描QR码,如果扫描成功,摄像头和顶层窗口会关闭。
这是我试过的。
import cv2
import cv2.cv as cv
import numpy
import zbar
import time
import threading
import Tkinter
from PIL import Image, ImageTk
class BarCodeScanner(threading.Thread, Tkinter.Toplevel):
def __init__(self):
# i made this as a global variable so i can access this image
# outside ie,. beyond the thread to update the image on to the tkinter window
global imgtk
imgtk = None
threading.Thread.__init__(self)
self.WINDOW_NAME = 'Camera'
self.CV_SYSTEM_CACHE_CNT = 5 # Cv has 5-frame cache
self.LOOP_INTERVAL_TIME = 0.2
cv.NamedWindow(self.WINDOW_NAME, cv.CV_WINDOW_NORMAL)
self.cam = cv2.VideoCapture(-1)
self.confirm = 0
def scan(self, aframe):
imgray = cv2.cvtColor(aframe, cv2.COLOR_BGR2GRAY)
# to show coloured image, as from the other code mentioned in the other code
imgcol = cv2.cvtColor(aframe, cv2.COLOR_BGR2RGBA)
imgcol_array = Image.fromarray(imgcol)
imgtk = ImageTk.PhotoImage(image=imgcol_array)
raw = str(imgray.data)
scanner = zbar.ImageScanner()
scanner.parse_config('enable')
width = int(self.cam.get(cv.CV_CAP