我想使用OpenCV和相机SDK VimbaPython在tkinter框架内显示来自Allied Vision相机的图像
初始化相机的唯一可能方法是使用Python with
语句:
with Vimba.get_instance() as vimba:
cams = vimba.get_all_cameras()
with cams[0] as camera:
camera.get_frame()
# Convert frame to opencv image, then use Image.fromarray and ImageTk.PhotoImage to
# display it on the tkinter GUI
到目前为止一切正常。但我不仅仅需要一个框架。相反,我需要不断地获取帧并在屏幕上显示它们,以便它是流式的。
我发现一种方法是从tkinter标签小部件调用.after(delay, function)
方法。
所以,在获得一帧后,我想调用相同的函数来获得一个新帧并再次显示它。代码如下所示:
with Vimba.get_instance() as vimba:
cams = vimba.get_all_cameras()
with cams[0] as camera:
def show_frame():
frame = camera.get_frame()
frame = frame.as_opencv_image()
im = Image.fromarray(frame)
img = Image.PhotoImage(im)
lblVideo.configure(image=img) # this is the Tkinter Label Widget
lblVideo.image = img
show_frame()
lblVideo.after(20, show_frame)
然后显示第一帧并停止,抛出一个错误,表示需要使用with
语句初始化Vimba。我对Python了解不多,但当我使用.after()
方法调用函数时,它似乎以语句结尾
我想知道是否可以在不结束with
的情况下执行这个show_frame()
函数。此外,我不能每次初始化相机,因为程序运行得非常慢。
多谢各位
我试图在openCV中读取帧,并在tkinter标签中显示它们。我可以使用以下代码执行此操作:
虽然它不包含with语句,但您可以尝试替换
show_frame
函数本身内部的after()
回调您需要使用线程来运行捕获代码,并通过
queue
传递读取的帧。然后主tkinter
应用程序读取queue
,并使用.after()
定期显示帧以下是基于您发布的代码的示例:
请注意,我没有安装摄像头和SDK,上面的代码可能不适合您。我只是演示如何使用线程、队列和
.after()
下面是一个测试
vimba
模块(另存为vimba.py
),我使用OpenCV
和网络摄像头来模拟VimbaPython
模块:相关问题 更多 >
编程相关推荐