繁体   English   中英

从摄像机到 tkinter window 和 opencv 的实时视频馈送

[英]Live video feed from camera to tkinter window with opencv

我目前正在尝试将实时网络摄像头输入到我的 gui 中的一个盒子中。 我想把它放进去的盒子是标有“Video Feed”的盒子。 cap = cv2.videoCapture(0) 是我的视频输入。 我不知道如何将实时视频 go 实现为 tkinter 和 opencv。我在下面添加了运行此程序所需的两个文件,即 ui2.py 和 settings.py。

ui2.py:

from datetime import date
from datetime import datetime
import tkinter as tk
import cv2

root = tk.Tk()
root.title("Traffic Camera")
root.geometry("+0+0")
root.iconbitmap('car_icon.ico')

width, height = root.winfo_screenwidth(), root.winfo_screenheight()
root = tk.Canvas(root, width=width, height=height)
root.configure(bg='#FFFFFF')
root.pack()

cap = cv2.VideoCapture(0)


def main():
    global state, user_email, user_speed, user_location, rec, line1, line2, settings_button, help_button

    for widget in root.winfo_children():
        widget.destroy()
    if state == "settings":
        email_save.destroy()
        speed_save.destroy()
        location_save.destroy()
        back_button.destroy()
    elif state == "help":
        back_button.destroy()
    state = "main"

    time = str(datetime.time(datetime.now()).hour) + ":" + str(datetime.time(datetime.now()).minute)
    mics_info = tk.Label(root, bg='#FFFFFF', text=user_location + " / " + str(date.today()) + " / " + time, font=('', 12))
    root.create_window(width/4, height/8, anchor='sw', window=mics_info)

    rec = root.create_rectangle(width/4, height/8, width/4*3, height/8*6)
    line1 = root.create_line(width/4, height/8, width/4*3, height/8*6)
    line2 = root.create_line(width/4*3, height/8, width/4, height/8*6)
    video_feed = tk.Label(root, bg='#FFFFFF', text="Video Feed", font='bold')
    root.create_window(width/2, height/16*7, anchor='center', window=video_feed)

    speed_limit = tk.Label(root, bg='#FFFFFF', text="Speed Limit: " + str(user_speed) + " mph", font=('', 12))
    root.create_window(width/4, height/8*6+1, anchor='nw', window=speed_limit)

    email = tk.Label(root, bg='#FFFFFF', text="Email: " + user_email, font=('', 12))
    root.create_window(width/4*3, height/8*6+1, anchor='ne', window=email)
    settings_button = tk.Button(text='Settings', width=10, font=('', 12), command=settings)
    root.create_window(width/2-20, height/8*7, anchor='e', window=settings_button)

    help_button = tk.Button(text='Help', width=10, font=('', 12), command=help)
    root.create_window(width/2+20, height/8*7, anchor='w', window=help_button)


def settings():
    global state, user_email, user_speed, user_location, email_field, speed_field, location_field, \
        email_save, speed_save, location_save, back_button

    for widget in root.winfo_children():
        widget.destroy()
    root.delete(rec)
    root.delete(line1)
    root.delete(line2)
    settings_button.destroy()
    help_button.destroy()
    state = "settings"

    settings_title = tk.Label(root, bg='#FFFFFF', text="Settings", font=('', 12))
    root.create_window(width/2, height/20, anchor='center', window=settings_title)

    email_label = tk.Label(root, bg='#FFFFFF', text="Email", font=('', 12))
    root.create_window(width/2-100, height/20*3, anchor='e', window=email_label)
    email_field = tk.Entry(root, width=30, bd=3)
    root.create_window(width/2, height/20*3, anchor='center', window=email_field)

    email_save = tk.Button(text='Save', width=10, font=('', 12), command=save_email)
    root.create_window(width/5*3, height/20*3, anchor='w', window=email_save)

    speed_label = tk.Label(root, bg='#FFFFFF', text="Speed Limit", font=('', 12))
    root.create_window(width/2-100, height/20*4.5, anchor='e', window=speed_label)
    speed_field = tk.Entry(root, width=30, bd=3)
    root.create_window(width/2, height/20*4.5, anchor='center', window=speed_field)

    speed_save = tk.Button(text='Save', width=10, font=('', 12), command=save_speed)
    root.create_window(width/5*3, height/20*4.5, anchor='w', window=speed_save)

    location_label = tk.Label(root, bg='#FFFFFF', text="Location", font=('', 12))
    root.create_window(width/2-100, height/20*6, anchor='e', window=location_label)
    location_field = tk.Entry(root, width=30, bd=3)
    root.create_window(width/2, height/20*6, anchor='center', window=location_field)

    location_save = tk.Button(text='Save', width=10, font=('', 12), command=save_location)
    root.create_window(width/5*3, height/20*6, anchor='w', window=location_save)

    back_button = tk.Button(text='Back', width=10, font=('', 12), command=main)
    root.create_window(width/2, height/20*8, anchor='center', window=back_button)


def help():
    global state, back_button

    for widget in root.winfo_children():
        widget.destroy()
    root.delete(rec)
    root.delete(line1)
    root.delete(line2)
    settings_button.destroy()
    help_button.destroy()
    state = "help"

    help_title = tk.Label(root, bg='#FFFFFF', text="Help", font=('', 12))
    root.create_window(width/2, height/20, anchor='center', window=help_title)

    getting_started = tk.Label(root, bg='#FFFFFF', text="Getting Started", font=('', 12))
    root.create_window(width/2, height/20*3, anchor='center', window=getting_started)

    s_text = """Upon starting the program, you will be greeted by the home
        page of the Traffic Camera software. Here, you can view the live feed 
        of the traffic camera, its location, its speed threshold, the email, 
        as well as the time and date. At the bottom of the 
        screen are two buttons which allow access to the settings page and the 
        help page."""
    start_text = tk.Label(root, bg='#FFFFFF', text=s_text, font=('', 12))
    root.create_window(width/2, height/20*4, anchor='n', window=start_text)

    settings = tk.Label(root, bg='#FFFFFF', text="Settings", font=('', 12))
    root.create_window(width/2, height/20*9, anchor='center', window=settings)

    settings_text = """Upon clicking the 'Settings' button on the home page, 
        a window will be presented giving you a couple options that allow
        modification. The first text box allows you to manipulate the current
        email that will be notified. The second text box allows you to 
        manipulate the speed threshold of the traffic camera. The third text box 
        allows you to manipulate the current location of the traffic camera. 
        Clicking on the 'Save' button next to these text boxes modifies the value
        of the respective text box. Once your changes are complete, 
        clicking on the 'Back' button will bring you back to the home page, where 
        the updated information will be displayed."""
    settings_label = tk.Label(root, bg='#FFFFFF', text=settings_text, font=('', 12))
    root.create_window(width/2, height/20*10, anchor='n', window=settings_label)

    back_button = tk.Button(text='Back', width=10, font=('', 12), command=main)
    root.create_window(width/2, height/20*16, anchor='center', window=back_button)


def save_email():
    global user_email, email_field
    user_email = email_field.get()


def save_speed():
    global user_speed, speed_field
    user_speed = speed_field.get()


def save_location():
    global user_location, location_field
    user_location = location_field.get()


if __name__ == '__main__':
    global state, user_email, user_speed, user_location
    user_email = "example@web.com"
    user_speed = 25
    user_location = "Location"
    state = "main"
    main()
    root.mainloop()

设置.py:

import tkinter as tk

root = tk.Tk()
root.title("Settings")
root.iconbitmap('car_icon.ico')

width, height = root.winfo_screenwidth() / 3, root.winfo_screenheight() / 2
root = tk.Canvas(root, width=width, height=height)
root.configure(bg='#235937')
root.pack()

if __name__ == '__main__':

    settings_title = tk.Label(root,bg='#235937', text="Settings", font=('', 20, 'bold'), fg='#FFFFFF')
    root.create_window(width / 2, height / 12, anchor='center', window=settings_title)

    email_field = tk.Entry(root, width=30, bd=3)
    root.create_window(width / 2, height / 4, anchor='e', window=email_field)

    email_save = tk.Button(text='SAVE', width=10, height=1, font=('', 12))
    root.create_window(width / 2 + 75, height / 4, anchor='w', window=email_save)

    speed_field = tk.Entry(root, width=30, bd=3)
    root.create_window(width / 2, height / 4 + 50, anchor='e', window=speed_field)

    speed_save = tk.Button(text='SAVE', width=10, height=1, font=('', 12))
    root.create_window(width / 2 + 75, height / 4 + 50, anchor='w', window=speed_save)

    local_field = tk.Entry(root, width=30, bd=3)
    root.create_window(width / 2, height / 4 + 100, anchor='e', window=local_field)

    local_save = tk.Button(text='SAVE', width=10, height=1, font=('', 12))
    root.create_window(width / 2 + 75, height / 4 + 100, anchor='w', window=local_save)

    back_button = tk.Button(text='BACK', width=10, height=1, font=('',12))
    root.create_window(width / 2, height / 2 + 150, anchor='center', window=back_button)

    root.mainloop()

这个例子可以解决你的问题; 它展示了如何将 tkinter 和 CV2 集成以流畅地“讨论”的要领。 遵循原则,将你的代码添加到这个例子的“顶部”,就是这样!

import tkinter
from tkinter import *
from tkinter import ttk
import numpy as np
from PIL import Image, ImageTk
import cv2

ikkuna=tkinter.Tk()
ikkuna.title("Example about handy CV2 and tkinter combination...")

frame=np.random.randint(0,255,[100,100,3],dtype='uint8')
img = ImageTk.PhotoImage(Image.fromarray(frame))

paneeli_image=tkinter.Label(ikkuna) #,image=img)
paneeli_image.grid(row=0,column=0,columnspan=3,pady=1,padx=10)

message="You can see some \nclassification results \nhere after you add some intelligent  \nadditional code to your combined and handy \n tkinter & CV2 solution!"
paneeli_text=tkinter.Label(ikkuna,text=message)
paneeli_text.grid(row=1,column=1,pady=1,padx=10)

global cam

def otakuva():
    global frame
    global cam
    cam = cv2.VideoCapture(0)
    #cv2.namedWindow("Experience_in_AI camera")
    while True:
        ret, frame = cam.read()

        #Update the image to tkinter...
        frame=cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
        img_update = ImageTk.PhotoImage(Image.fromarray(frame))
        paneeli_image.configure(image=img_update)
        paneeli_image.image=img_update
        paneeli_image.update()

        if not ret:
            print("failed to grab frame")
            break

        k = cv2.waitKey(1)
        if k%256 == 27:
            # ESC pressed
            print("Escape hit, closing...")

            cam.release()
            cv2.destroyAllWindows()
            break

def lopeta():
    global cam
    cam.release()
    cv2.destroyAllWindows()
    print("Stopped!")

painike_korkeus=10
painike_1=tkinter.Button(ikkuna,text="Start",command=otakuva,height=5,width=20)
painike_1.grid(row=1,column=0,pady=10,padx=10)
painike_1.config(height=1*painike_korkeus,width=20)

painike_korkeus=10
painike_1=tkinter.Button(ikkuna,text="Stop",command=lopeta,height=5,width=20)
painike_1.grid(row=1,column=2,pady=10,padx=10)
painike_1.config(height=1*painike_korkeus,width=20)

ikkuna.mainloop()

基于@ Experience-In-Ai的回答

您可以使用此代码,而不是在按下停止按钮时出现错误:

while (cam.isOpened()):
   ...

并且在按下关闭按钮时不会出现错误:

def on_closing():
    global cam
    cam.release()
    cv2.destroyAllWindows()
    ikkuna.destroy()

ikkuna.protocol("WM_DELETE_WINDOW", on_closing)

ikkuna.mainloop()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM