TKinter_video_cap.py


#coding: utf-8
import numpy as np
import cv2
import tkinter as tk, threading
from PIL import Image, ImageTk

cap = cv2.VideoCapture(0)

def stream(label):
    def showlabel():
        _, image = cap.read()
        image = cv2.flip(image, 1)
        cv2image = cv2.cvtColor(image, cv2.COLOR_BGR2RGBA)
        frame_image = ImageTk.PhotoImage(Image.fromarray(cv2image))
        label.config(image=frame_image)
        label.image = frame_image
        label.after(14, showlabel) #フレーム・レイトの調整
    showlabel()

if __name__ == "__main__":

    root = tk.Tk()
    root.title('TKinterでヴィデオを見る')
    my_label = tk.Label(root)
    my_label.pack()
    thread = threading.Thread(target=stream, args=(my_label,))
    thread.daemon = 1
    thread.start()
    root.mainloop()