当前位置:   article > 正文

Python调用摄像头,实时显示视频在Tkinter界面_tkinter 视频

tkinter 视频

在Python中调用摄像头实时显示监控视频,将视频窗口放到tkinter界面的画布窗口,程序:


import cv2
import tkinter as tk
from tkinter import *
from PIL import Image, ImageTk  # 图像控件

# 创建视频捕获对象,读取笔记本电脑的本地摄像头流
cap = cv2.VideoCapture(0)  # 使用本地摄像头,通常索引为0

# 界面画布更新图像
def tkImage():
    ref, frame = cap.read()
    if not ref:
        print("Failed to capture frame")
        return None
    frame = cv2.flip(frame, 1)  # 摄像头翻转
    cvimage = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
    pilImage = Image.fromarray(cvimage)
    pilImage = pilImage.resize((image_width, image_height), Image.LANCZOS)
    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)

# 保存图像对象,以防止被垃圾回收
image_container = None

def update_frame():
    global image_container
    pic = tkImage()
    if pic:
        canvas.create_image(0, 0, anchor='nw', image=pic)
        image_container = pic  # 保存引用
    else:
        print("No image to display")
    top.after(10, update_frame)  # 每10毫秒更新一次图像

top.after(10, update_frame)  # 启动定时器
top.mainloop()

# 释放摄像头
cap.release()
  • 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
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

效果展示

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/775915
推荐阅读
相关标签
  

闽ICP备14008679号