我正在使用Tkinter來製作一個GUI來顯示USB攝像頭的輸出。這是一個顯微鏡實驗的想法是,gui顯示低分辨率的實時流,但點擊一個按鈕時會拍攝高分辨率圖像。我已經能夠使用我的筆記本電腦的內置攝像頭(VideoCapture(0))工作,但是當我嘗試在預期的攝像頭(https://www.leopardimaging.com/uploads/LI-OV5640-USB-72_datasheet.pdf - VideoCapture(1))上使用代碼時,它會崩潰。 的代碼是這樣的:第一次按下按鈕,出現以下錯誤Python 2.7 CV2和Tkinter與攝像頭
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1486, in __call__
return self.func(*args)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 533, in callit
func(*args)
File "C:/Users/.../LI_USB_GUI_RR_worksWithInBuiltCam2.py", line 109, in show_frame
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
error: ..\..\..\..\opencv\modules\imgproc\src\color.cpp:3648: error: (-215) scn == 3 || scn == 4 in function cv::cvtColor
,是爲了被寫入文件後
import Tkinter as tk
import cv2
import cv2.cv as cv
import numpy as np
from PIL import Image, ImageTk
global counter
counter = 0
global save_dir
save_dir = "C:/Users/etc..."
global runner
runner = 50
global run_num
run_num = "50"
##########################################################################
global hi_w, hi_h
global lo_w, lo_h
hi_w, hi_h = 640,480 # Camera intended resolution 2592,1944
lo_w, lo_h = 320,240 # Camera intended resolution 640,480
cap = cv2.VideoCapture(1)
cap.set(3, lo_w)
cap.set(4, lo_h)
cap.set(5,15)
##########################################################################
# Define the Tkinter functions
#-- QUIT_ --#
#-----------#
def quit_(root):
root.destroy()
#---------------------
#-- FUNCTION1 --#
#---------------#
def function1(root):
global counter
counter = 1
#---------------------
#-- FUNCTION2 --#
#---------------#
def function2(root):
global counter
counter = 2
#---------------------
#-- FUNCTION3 --#
#---------------#
def function3(root):
global counter
counter = 3
#---------------------
def capture(filename):
print 'capturing'
global hi_w, hi_h, lo_w, lo_h
cap.set(3, hi_w)
cap.set(4, hi_h)
flag2, frame2 = cap.read()
frame2 = cv2.flip(frame2, 1)
print 'writing'
cv2.imwrite(filename, frame2)
print 'resetting'
cap.set(3, lo_w)
cap.set(4, lo_h)
del flag2, frame2
global counter
counter = 0
def show_frame():
#Set up dummy frame
global counter, save_dir, runner, run_num
if counter == 1:
flag,frame = cap.read()
filename = save_dir + "z01_" + run_num + ".jpeg"
capture(filename)
elif counter == 2:
flag, frame = cap.read()
filename = save_dir + "z02_" + run_num + ".jpeg"
capture(filename)
elif counter == 3:
flag, frame = cap.read()
filename = save_dir + "z03_" + run_num + ".jpeg"
capture(filename)
runner = runner + 1
run_num = '{0:02d}'.format(runner)
counter = 0
else:
flag, frame = cap.read()
frame = cv2.flip(frame, 1)
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
img = Image.fromarray(cv2image)
imgtk = ImageTk.PhotoImage(image=img)
lmain.imgtk = imgtk
lmain.configure(image=imgtk)
lmain.after(10, show_frame)
if __name__ == '__main__':
root = tk.Tk()
lmain = tk.Label(master=root)
lmain.grid(column=0, rowspan=4, padx=5, pady=5)
button1 = tk.Button(master=root, text='Function 1', command=lambda: function1(root))
button1.grid(column=1, columnspan=2, row=0, padx=5, pady=5)
button2 = tk.Button(master=root, text='Function 2', command=lambda: function2(root))
button2.grid(column=1, columnspan=2, row=1, padx=5, pady=5)
button3 = tk.Button(master=root, text='Function 3', command=lambda: function3(root))
button3.grid(column=1, columnspan=2, row=2, padx=5, pady=5)
quit_button = tk.Button(master=root, text='Quit',bg="red3", fg="white", command=lambda: quit_(root))
quit_button.grid(column=1, row=3, padx=5, pady=5)
show_frame()
root.mainloop()
cap.release()
的程序崩潰是在大小零個字節。儘管相機仍然處於開啓狀態並且可以通過外殼進行控制,但應該在此處進行顯示的框架已變空。我真的很困惑,爲什麼內置攝像頭可以工作,但USB不會有相同的代碼。
請幫忙...
您的行'cv2image = cv2.cvtColor(frame,cv2.COLOR_BGR2RGBA)'中的對象'frame'是'None'。您可能需要檢查cap.read()正在返回的值。或者如果'frame = cv2.flip(frame,1)'返回一個'None'對象。 –
嗨,我不幸離開了我的辦公桌几天,所以我不能做任何明確的檢查,但作爲記憶服務你是對的。 'cap.read()'當我嘗試調整feed大小時返回None,但是fed仍然處於活動狀態,當它崩潰時,我仍然可以控制shell中的相機。這很奇怪! – Chris
我做了一點挖掘,發現此前[answer](http://stackoverflow.com/a/8046886/5066845)。它看起來像OpenCV有關於網絡攝像頭的問題。是否可以停用內置攝像頭?我有一個不同的硬件設置,所以我不能再現這種行爲。 –