问题描述
我正在使用 OOP 概念在 python 中开发名为 颜色检测 的项目。在这个项目中,用户必须通过从系统中选择图像或从网络摄像头拍照来输入图像。 我几乎完成了项目,但出现运行时错误。 这是我的代码块:
self.root = Tk()
....
....
self.capture_frame = Frame(self.root,width=750,height=550,bg='black')
....
....
def openCamera(self,capture_frame):
self.hide_all_frames()
self.capture_frame.pack()
#button
snapshot_btn = Button(capture_frame,text="CAPTURE",width=13,bg = '#1cbab5',fg = 'black',font = ('dsrom 10',12,'bold'),command = self.snapshot).place(x=170,y=515)
BACK_btn = Button(capture_frame,text="BACK",bg = 'red',command = lambda :self.firstFrame(self.image_selection_frame)).place(x=400,y=515)
#Labels
self.cam_area = Label(capture_frame,bg = 'black')
self.cam_area.place(x=50,y=20)
self.cap = cv.VideoCapture(0)
#while to open camra
while True:
self.img = self.cap.read()[1]
self.img = cv.flip(self.img,1)
self.img1 = cv.cvtColor(self.img,cv.COLOR_BGR2RGB)
self.img = ImageTk.PhotoImage(image = PIL.Image.fromarray(self.img1))
self.cam_area['image'] = self.img
self.root.update()
self.cap.release()
cv.destroyAllWindows()
当单独运行而不创建任何函数或方法时,此代码运行良好。 当我尝试将它包含在我的项目中时它运行良好,甚至它捕获图像并将其保存在系统中。 但是当我想关闭相机并尝试重新打开它时出现错误:
[ WARN:0] global /tmp/pip-req-build-j6tkfxml/opencv/modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video0): can't open camera by index
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.9/tkinter/__init__.py",line 1892,in __call__
return self.func(*args)
File "/home/safehouse/Desktop/ColorDetection.py",line 145,in <lambda>
font = ('dsrom 10',10,command=lambda
:self.capturecmd(self.capture_frame)).place(x=450,y=130)
File "/home/safehouse/Desktop/ColorDetection.py",line 205,in capturecmd
self.img1 = cv.cvtColor(self.img,cv.COLOR_BGR2RGB)
cv2.error: OpenCV(4.5.1) /tmp/pip-req-build-j6tkfxml/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion Failed) !_src.empty() in function 'cvtColor'
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.9/tkinter/__init__.py",in __call__
return self.func(*args)
File "/home/safehouse/Desktop/ColorDetection.py",in <lambda>
font = ('dsrom 10',line 206,in capturecmd
self.img = ImageTk.PhotoImage(master = self.cam_area,image = Image.fromarray(self.img1))
File "/usr/lib/python3/dist-packages/PIL/ImageTk.py",line 112,in
__init__
self.__photo = tkinter.PhotoImage(**kw)
File "/usr/lib/python3.9/tkinter/__init__.py",line 4064,in __init__
Image.__init__(self,'photo',name,cnf,master,**kw)
File "/usr/lib/python3.9/tkinter/__init__.py",line 4009,in __init__
self.tk.call(('image','create',imgtype,) + options)
_ tkinter.TclError: can't invoke "image" command: application has been destroyed
Exception ignored in: <function PhotoImage.__del__ at 0x7f866f265ca0>
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/PIL/ImageTk.py",line 118,in __del__
name = self.__photo.name
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
当我使用 root = tk() 我得到 Photoimage() 属性错误但是当我使用
root = Toplevel()
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.9/tkinter/__init__.py",command=lambda :self.capturecmd(self.capture_frame)).place(x=450,line 207,in capturecmd
self.cam_area['image'] = self.img
File "/usr/lib/python3.9/tkinter/__init__.py",line 1657,in __setitem__
self.configure({key: value})
File "/usr/lib/python3.9/tkinter/__init__.py",line 1646,in configure
return self._configure('configure',kw)
File "/usr/lib/python3.9/tkinter/__init__.py",line 1636,in _configure
self.tk.call(_flatten((self._w,cmd)) + self._options(cnf))
_tkinter.TclError: invalid command name ".!toplevel.!frame3.!label"
我已经尝试过并到处搜索并尝试使用不同版本的软件包,但当时没有真正起作用。 我所有的项目都运行良好,相机也在拍照并保存。但是由于上述错误,我无法在不关闭整个项目的情况下重新启动相机。由于 Photoimage 错误,它删除了相机屏幕。
如果有人能帮忙,那将是一个很大的帮助。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)