我正在尝试使用opencv和tkinter在我的GUI程序中捕获图像。捕获的图像保存在目录中并处理它并保存为pdf文件。但是当打开视频捕获窗口时,它总是闪烁。点击关闭按钮" VIDIOC_DQBUF:无效的参数"出现错误。
import datetime
import threading
from tkinter import *
from tkinter import Frame
from tkinter import ttk
import PIL.Image
import PIL.ImageTk
import cv2
from current_user_doc_manger import CurrentUserDocServer as cp
class ImageCaptureView(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master.title('Assid- Image Capture')
self.pack()
self.thread_flag = False
self.scanned_document = cp.get_current_doc_text()
self.username = cp.get_current_user_name()
self.userdesig = cp.get_current_user_desig()
self.userpen = cp.get_cur_user_pen()
self.file_name = cp.get_current_user_doc_text()
self.doc_label = ttk.Label(self, width=15, text='Document No: ' +
self.scanned_document)
self.doc_label.pack(side='top', padx=10, pady=10, fill='both')
self.canvas = Canvas(self)
self.canvas.pack()
self.btn_frame = Frame(self)
self.btn_frame.pack(side='bottom', fill='both', expand='yes')
self.create_btn_frame(self.btn_frame)
def create_btn_frame(self, btn_frame):
self.btn_cap = Button(btn_frame, text='capture',
command=self.create_a_snap)
self.btn_cap.pack(side='left', fill='both', expand='yes', padx=10, pady=10)
self.btn_next = Button(btn_frame, text='next', command=self.create_a_snap)
self.btn_next.pack(side='left', fill='both', expand='yes', padx=10, pady=10)
btn_close = Button(btn_frame, text='close', command=self.close_camera)
btn_close.pack(side='left', fill='both', expand='yes', padx=10, pady=10)
def create_image_process_thread(self):
self.thread_flag = True
self.image_thread = threading.Thread(target=self.videoloop, args=())
self.image_thread.daemon = True
self.image_thread.start()
def create_a_snap(self):
cv2.imwrite(self.file_name + '-' +
datetime.datetime.now().strftime('%d.%m.%Y %H:%M:%S.2%f') + '.jpg',
self.frame)
def close_camera(self):
self.cam.release()
self.thread_flag = False
self.destroy()
cp.set_bookno('None')
cp.set_doc_year('None')
cp.set_doc_no('None')
def videoloop(self):
self.thread_flag = True
try:
self.cam = cv2.VideoCapture(0)
except:
pass
while self.thread_flag:
ret, self.frame = self.cam.read()
if not ret:
self.cam.release()
break
else:
try:
self.width = self.cam.get(cv2.CAP_PROP_FRAME_WIDTH)
self.height = self.cam.get(cv2.CAP_PROP_FRAME_HEIGHT)
self.gray = cv2.cvtColor(self.frame, cv2.COLOR_BGR2GRAY)
self.photo = PIL.ImageTk.PhotoImage(image=PIL.Image.fromarray(self.gray))
self.canvas.config(width=int(self.width), height=int(self.height))
self.canvas.create_image(0, 0, image=self.photo, anchor=NW)
except ValueError:
self.cam.release()
break
if __name__ == '__main__':
x = ImageCaptureView()
x.create_image_process_thread()
x.mainloop()