在tkinter中显示视频流在帧内 [英] to show video streaming inside frame in tkinter

查看:901
本文介绍了在tkinter中显示视频流在帧内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在frame1内显示实时视频.我想知道为什么它不能像cv2.imshow中那样工作,我已经写了frame1. 谁能帮帮我吗?

I'm trying to display my live video inside my frame1. I want to know why it's not working as in cv2.imshow I have written frame1. Can anyone help me out Please?

我的框架/窗口代码:

from tkinter import Tk, Text, BOTH, W, N, E, S
from tkinter.ttk import Frame, Button, Label, Style
from tkinter import *

# from tkinter import *

class Example(Frame):

    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        self.master.title("Nav Track app")
        self.pack(fill=BOTH, expand=True)

        self.style = Style()
        self.style.theme_use("clam")

        self.columnconfigure(1, weight=1)
        self.columnconfigure(3, pad=7)
        self.rowconfigure(4, weight=1)
        self.rowconfigure(8, pad=7)

        lbl = Label(self, text="Keep your eyes on screen")
        lbl.grid(sticky=W, pady=4, padx=5)

        frame1 = Frame(self, bg="")
        frame1.grid(row=1, column=0, columnspan=2, rowspan=6, padx=8, sticky=E + W + S + N)

        # area = Text(self)
        # area.grid(row=1, column=0, columnspan=2, rowspan=6, padx=8, sticky=E + W + S + N)

        self.button = Button(self, text="Start Camera", command=hello)
        self.button.grid(row=1, column=3, padx=4)


        cbtn = Button(self, text="Select Object")
        cbtn.grid(row=2, column=3, padx=4, pady=4)


        dbtn = Button(self, text="Play")
        dbtn.grid(row=3, column=3, padx=4, pady=4)

        ebtn = Button(self, text="Start Tracking")
        ebtn.grid(row=4, column=3, padx=4, pady=4)

        fbtn = Button(self, text="Start Recording")
        fbtn.grid(row=5, column=3, padx=4, pady=4)

        fbtn = Button(self, text="Take Snapshots")
        fbtn.grid(row=6, column=3, padx=4, pady=4)

        hbtn = Button(self, text="Help")
        hbtn.grid(row=9, column=0, padx=5, pady=6)

        obtn = Button(self, text="Exit")
        obtn.grid(row=9, column=3, pady=6)

OpenCV中视频流的功能:

Function for video streaming in OpenCV:

def hello():
    import cv2

    # Create a VideoCapture object
    cap = cv2.VideoCapture(0)

    # Check if camera opened successfully
    if (cap.isOpened() == False):
        print("Unable to read camera feed")

    # Default resolutions of the frame are obtained.The default resolutions are system dependent.
    # We convert the resolutions from float to integer.
    # frame_width = int(cap.get(3))
    # frame_height = int(cap.get(4))
    frame_width = int(cap.set(3, 1280))
    frame_height = int(cap.set(4, 1024))
    frameyowidth = int(cap.get(3))
    frameyoheight = int(cap.get(4))

    # Define the codec and create VideoWriter object.The output is stored in 'outpy.avi' file.
    out = cv2.VideoWriter('outpy.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 30, (frameyowidth, frameyoheight))

    while (True):
        ret, frame = cap.read()

        if ret == True:

            # Write the frame into the file 'output.avi'
            out.write(frame)

            # Display the resulting frame
            cv2.imshow("chdh", frame1)

            # Press Q on keyboard to stop recording
            if cv2.waitKey(1) & 0xFF == 27:
                break

        # Break the loop
        else:
            break

        # When everything done, release the video capture and video write objects
    cap.release()
    out.release()

    # Closes all the frames
    cv2.destroyAllWindows()

如果还有另一种替代方法,但必须是最简单的方法.

and if there is another alternative way to do so but it must be the easiest one, please.

推荐答案

要在tkinter中显示视频流在帧内,我觉得最简单的方法是使用PIL库....

To show video streaming inside frame in tkinter, I feel the easiest way is using PIL library....

from Tkinter import *
from PIL import ImageTk, Image
import cv2


root = Tk()
# Create a frame
app = Frame(root, bg="white")
app.grid()
# Create a label in the frame
lmain = Label(app)
lmain.grid()

# Capture from camera
cap = cv2.VideoCapture(0)

# function for video streaming
def video_stream():
    _, frame = cap.read()
    cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
    img = Image.fromarray(cv2image)
    imgtk = ImageTk.PhotoImage(image=img)
    lmain.imgtk = imgtk
    lmain.configure(image=imgtk)
    lmain.after(1, video_stream) 

video_stream()
root.mainloop()

您可以使用pip安装PIL-

You can install PIL using pip -

pip install Pillow

这篇关于在tkinter中显示视频流在帧内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆