【发布时间】:2017-04-05 02:42:59
【问题描述】:
我正在使用 OpenCV 和 Python Tkinter。 我想将 OpenCV 的视频帧转换为 Tkinter 标签。 我使用了线程,因为我有两个循环。 (我得到了this的指导)
当我尝试运行它向我展示的代码时,
按任意键继续。 . .线程 Thread-2 中的异常:Traceback(最近一次调用最后一次):文件“C:\Python27\lib\threading.py”,第 808 行,在 __bootstrap_inner self.run() 文件“C:\Python27\lib\threading.py ",第 761 行,在运行 self.__target(*self.__args, **self.__kwargs)File "c:\users\user1\documents\visual studio 2013\Projects\defTstWindow\defT stWindow\defTstWindow.py",第 26 行,在 makeGUI img = Image.fromarray(cv2image) AttributeError: 类 Image 没有属性 'fromarray'
我已经尝试过使用 Python 类。我遇到了同样的错误。
但是,如果我在一个函数中运行所有功能(例如 this 的第一个答案),它可以正常工作。
我的代码有什么问题?
现在我有四个 python 模块。
1.Support.py
import cv2
global frame
frame=None
2.CamHandler.py
import cv2
import numpy as np
import Support
cam=cv2.VideoCapture(0)
def getFrame():
while 1:
_,frm=cam.read()
#cv2.imshow('frm',frm)
Support.frame=frm
if cv2.waitKey(1) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
3.defTstWindow.py
import sys
import cv2
import Image, ImageTk
from Tkinter import *
import Support
def makeGUI():
top=Tk()
top.geometry("600x449+650+151")
top.title("Test Window")
top.configure(background="#d9d9d9")
lblFrame = Label(top)
lblFrame.place(relx=0.03, rely=0.04, height=411, width=544)
lblFrame.configure(background="#d9d9d9")
lblFrame.configure(disabledforeground="#a3a3a3")
lblFrame.configure(foreground="#000000")
lblFrame.configure(text='''Label''')
lblFrame.configure(width=544)
cv2image = cv2.cvtColor(Support.frame, cv2.COLOR_BGR2RGBA)
img = Image.fromarray(cv2image)
imgtk = ImageTk.PhotoImage(image=img)
lblFrame.imgtk = imgtk
lblFrame.configure(image=imgtk)
#lblFrame.after(10, show_frame)
top.mainloop()
4.main.py
import CamHandler
import defTstWindow
import threading
import time
threading.Thread(target=CamHandler.getFrame).start()
time.sleep(1)
threading.Thread(target=defTstWindow.makeGUI).start()
【问题讨论】:
标签: python multithreading opencv numpy tkinter