在 tkinter 中显示帧内的视频流

Posted

技术标签:

【中文标题】在 tkinter 中显示帧内的视频流【英文标题】:to show video streaming inside frame in tkinter 【发布时间】:2018-11-28 01:11:40 【问题描述】:

我正在尝试在我的 frame1 中显示我的实时视频。我想知道为什么它不像我写的 cv2.imshow 那样工作 frame1。 任何人都可以帮助我吗?

我的框架/窗口代码:

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 中的视频流功能:

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()

如果有另一种方法可以做到这一点,但它必须是最简单的,请。

【问题讨论】:

为什么 代码 _not 工作?不是很好地描述问题,试着描述问题,应该做什么以及实际在做什么。 【参考方案1】:

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

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 -

pip install Pillow

【讨论】:

我正在尝试做类似的事情,但是是网络流。如果我使用 cv2.VideoCapture("192.168.0.4:8081/"),代码会在启动时关闭,而如果我使用 cv2.VideoCapture(0),它会显示我的网络摄像头。我正在使用动作从树莓派连续流到运行此程序的笔记本电脑代码。

以上是关于在 tkinter 中显示帧内的视频流的主要内容,如果未能解决你的问题,请参考以下文章

视频编解码 — 帧内预测

OpenCV - 获取视频帧内检测到的图像的法线

python: tkinter 显示来自网络摄像头的视频并进行 QR 扫描

[python][转载]tkinter opencv显示视频一闪一闪解决方法

视频压缩编码 gop(Group of Pictures)(I帧间隔)的概念IDRI帧(关键帧,intra picture)P帧B帧帧内压缩帧间压缩pts(显示时间)dts(解码时间)

十五帧内编码:1帧内编码的基本原理