簡體   English   中英

如何在 tkinter 畫布中顯示網絡攝像頭?

[英]How to show webcam within tkinter canvas?

我有一個帶畫布的 Tkinter GUI,當前默認顯示圖像,並且有一個開關可以在來回切換時打印 Cam On 和 Cam off。 我想要做的是讓我的網絡攝像頭在我打開開關時流式傳輸而不是那個圖像。

這是我的 GUI 代碼:

import tkinter
import tkinter.messagebox
import customtkinter
from PIL import Image,ImageTk


# Setting up theme of GUI
customtkinter.set_appearance_mode("Dark")  # Modes: "System" (standard), "Dark", "Light"
customtkinter.set_default_color_theme("blue")  # Themes: "blue" (standard), "green", "dark-blue"

class App(customtkinter.CTk):
    def __init__(self):
        super().__init__()

        # configure window
        self.is_on = True
        self.image = ImageTk.PhotoImage(Image.open("../data/Mars.PNG"))

        self.title("Cool Blue")
        self.geometry(f"{1200}x{635}")

        # configure grid layout (4x4)
        self.grid_columnconfigure(1, weight=1)
        self.grid_columnconfigure((2, 3), weight=0)
        self.grid_rowconfigure((0, 1, 2, 3), weight=0)

        ###################################################################
        # Create Sidebar for LED, LIghts and Camera controls
        ###################################################################
        self.lights_control = customtkinter.CTkFrame(self)
        self.lights_control.grid(row=3, column=0, rowspan = 1, padx=(5, 5), pady=(10, 10), sticky="nsew")
        self.lights_control.grid_rowconfigure(1, weight=1)

        # Camera
        self.camera_switch = customtkinter.CTkSwitch(master=self.lights_control, text="Camera", command=self.camera_switch)
        self.camera_switch.grid(row=2, column=1, pady=10, padx=20, )

        ###################################################################
        # Create canvas for RPCam live stream
        ###################################################################
        self.picam_frame = customtkinter.CTkFrame(self)
        self.picam_frame.grid(row=0, column=1, rowspan=4, padx=(5, 5), pady=(10, 10), sticky="nsew")
        self.picam_frame.grid_rowconfigure(4, weight=1)
        self.picam_canvas = customtkinter.CTkCanvas(self.picam_frame, width=1730, height=944, background="gray")
        self.picam_canvas.create_image(0, 0, image=self.image, anchor="nw")
        self.picam_canvas.pack()

    #########################################################################
    # Camera Switch
    #########################################################################
    def camera_switch(self, event=None):

        if self.is_on:

            print("Cam on")

            self.is_on = False
        else:
            print("Cam off")

            self.is_on = True


if __name__ == "__main__":
    app = App()
    app.mainloop()

現在我做了一些研究,發現下面的這段代碼可以在它自己的畫布上完成這個技巧,但我沒有運氣在我自己上面的代碼中使用下面代碼中的邏輯。

# Import required Libraries
from tkinter import *
from PIL import Image, ImageTk
import cv2

# Create an instance of TKinter Window or frame
win = Tk()

# Set the size of the window
win.geometry("700x350")

# Create a Label to capture the Video frames
label =Label(win)
label.grid(row=0, column=0)
cap= cv2.VideoCapture(0)

# Define function to show frame
def show_frames():
   # Get the latest frame and convert into Image
   cv2image= cv2.cvtColor(cap.read()[1],cv2.COLOR_BGR2RGB)
   img = Image.fromarray(cv2image)
   # Convert image to PhotoImage
   imgtk = ImageTk.PhotoImage(image = img)
   label.imgtk = imgtk
   label.configure(image=imgtk)
   # Repeat after an interval to capture continiously
   label.after(20, show_frames)

show_frames()
win.mainloop()

所以我真正需要的是幫助使用下面代碼中的邏輯並將其合並到我自己上面的代碼中。 你能幫我么?

這是我解決這個問題的最新嘗試。 所以我能夠根據上面提供的邏輯合並這兩個代碼。 當我啟動 GUI 並打開相機按鈕時,我可以看到相機正在啟動,因為相機上的 LED 燈亮起,並且我收到一個 Windows 通知,告知我的程序正在啟動相機。 但是,它還沒有顯示在畫布上。 所以我想我很接近,但我不確定我錯過了什么。

import tkinter
import tkinter.messagebox
import customtkinter
from PIL import Image,ImageTk
import cv2


# Setting up theme of GUI
customtkinter.set_appearance_mode("Dark")  # Modes: "System" (standard), "Dark", "Light"
customtkinter.set_default_color_theme("blue")  # Themes: "blue" (standard), "green", "dark-blue"

class App(customtkinter.CTk):
    def __init__(self):
        super().__init__()

        # configure window
        self.is_on = True
        self.image = ImageTk.PhotoImage(Image.open("../data/Mars.PNG"))
        self.capture = cv2.VideoCapture(0)

        self.title("Cool Blue")
        self.geometry(f"{1200}x{635}")

        # configure grid layout (4x4)
        self.grid_columnconfigure(1, weight=1)
        self.grid_columnconfigure((2, 3), weight=0)
        self.grid_rowconfigure((0, 1, 2, 3), weight=0)

        ###################################################################
        # Create Sidebar for LED, LIghts and Camera controls
        ###################################################################
        self.lights_control = customtkinter.CTkFrame(self)
        self.lights_control.grid(row=3, column=0, rowspan = 1, padx=(5, 5), pady=(10, 10), sticky="nsew")
        self.lights_control.grid_rowconfigure(1, weight=1)

        # Camera
        self.camera_switch = customtkinter.CTkSwitch(master=self.lights_control, text="Camera", command=self.camera_switch)
        self.camera_switch.grid(row=2, column=1, pady=10, padx=20, )

        ###################################################################
        # Create canvas for RPCam live stream
        ###################################################################
        self.picam_frame = customtkinter.CTkFrame(self)
        self.picam_frame.grid(row=0, column=1, rowspan=4, padx=(5, 5), pady=(10, 10), sticky="nsew")
        self.picam_frame.grid_rowconfigure(4, weight=1)


        # self.picam_canvas = customtkinter.CTkCanvas(self.picam_frame, width=1730, height=944, background="gray")
        # self.picam_canvas.create_image(0, 0, image=self.image, anchor="nw")

        self.picam_canvas = tkinter.Canvas(self.picam_frame, width=1730, height=944)
        #self.picam_canvas.pack


    #########################################################################
    # Camera Switch
    #########################################################################
    def camera_switch(self, event=None):

        if self.is_on:
            self.update_frames()

            print("Cam on")

            self.is_on = False
        else:
            #self.close_camera()
            self.image
            print("Cam off")

            self.is_on = True


    def update_frames(self):
        # Get the current frame from the webcam
        _, frame = self.capture.read()

        # Convert the frame to a PhotoImage object
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        frame = Image.fromarray(frame)
        frame = ImageTk.PhotoImage(frame)

        # Update the canvas with the new frame
        self.picam_canvas.create_image(0, 0, image=frame, anchor="nw")
        self.picam_canvas.image = frame

        # Schedule the next update
        self.after(1000, self.update_frames)
    def close_camera(self):
        self.capture.release()

if __name__ == "__main__":
    app = App()
    app.mainloop()

同樣,如果有人有任何見解,我將非常感激。

在代碼中,您可以利用update_frame函數中的變量self.is_on來完成循環。 像這樣的東西。

def update_frames(self):

    # Change the frame by the initial image and breaks the loop
    if self.is_on:
        self.picam_canvas.create_image(0, 0, image=self.image, anchor="nw")
        return
    else:
        _, frame = self.capture.read()

    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

    frame = Image.fromarray(frame)
    frame = ImageTk.PhotoImage(frame)

    self.picam_canvas.create_image(0, 0, image=frame, anchor="nw")
    self.picam_canvas.image = frame

    self.picam_canvas.after(1, self.update_frames)

但這會出錯,因為當您重新打開它時,視頻捕獲沒有開始。 所以我修改了camera_switch函數中的一些行。

def camera_switch(self, event=None):
    if self.is_on:
        self.capture = cv2.VideoCapture(0)
        print("Cam on")
        self.is_on = False
        self.update_frames()
    else:
        self.close_camera()
        self.image
        print("Cam off")

        self.is_on = True

self.capture = cv2.VideoCapture(0)開頭,但我將其刪除並放在此處,以便在按下按鈕時初始化視頻捕獲

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM