【问题标题】:Live video feed from camera to tkinter window with opencv使用opencv从相机到tkinter窗口的实时视频馈送
【发布时间】:2021-07-01 12:24:32
【问题描述】:

我目前正在尝试将实时网络摄像头馈送到我的 gui 中的一个框中。我要放入的盒子是标有“视频源”的盒子。 cap = cv2.videoCapture(0) 是我的视频输入。我不知道如何将实时视频实现到 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()

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

【问题讨论】:

  • 所以你想在标签上显示实时捕获?
  • 我想要标签所在的实时视频源
  • 这很简单,请在 google 上找到示例
  • @Jonamite 你做错了很多事情。抱歉,我可能帮不上忙。顺便说一句,你看过这些link1link2吗?

标签: python opencv tkinter


【解决方案1】:

这个例子可以解决你的问题;它展示了如何将 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()

【讨论】:

    猜你喜欢
    • 2020-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-30
    • 2014-11-13
    相关资源
    最近更新 更多