在Python中调用摄像头实时显示监控视频,将视频窗口放到tkinter界面的画布窗口,程序:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34import cv2 import tkinter as tk from tkinter import * from PIL import Image, ImageTk#图像控件 cap = cv2.VideoCapture(0)#创建摄像头对象 #界面画布更新图像 def tkImage(): ref,frame=cap.read() frame = cv2.flip(frame, 1) #摄像头翻转 cvimage = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA) pilImage=Image.fromarray(cvimage) pilImage = pilImage.resize((image_width, image_height),Image.ANTIALIAS) tkImage = ImageTk.PhotoImage(image=pilImage) return tkImage top = tk.Tk() top.title('视频窗口') top.geometry('900x600') image_width = 600 image_height = 500 canvas = Canvas(top,bg = 'white',width = image_width,height = image_height )#绘制画布 Label(top,text = '这是一个视频!',font = ("黑体",14),width =15,height = 1).place(x =400,y = 20,anchor = 'nw') canvas.place(x = 150,y = 50) while True: try: #关闭窗口后,由于没有采集图像帧,所以加载不到画布上,会弹出报错。采用异常跳出程序中止 pic = tkImage() canvas.create_image(0,0,anchor = 'nw',image = pic) top.update() top.after(1) except: #关闭窗口后执行 cap.release() #释放摄像头 top.mainloop() #关闭窗口
最后
以上就是沉静帽子最近收集整理的关于Python调用摄像头,实时显示视频在Tkinter界面的全部内容,更多相关Python调用摄像头内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复