import tkinter as tk from tkinter import ttk import cv2 from PIL import Image, ImageTk from threading import Thread import argparse import os import shutil import time from pathlib import Path import torch import torch.backends.cudnn as cudnn import serial from numpy import random from models.experimental import attempt_load from utils.datasets import LoadStreams, LoadImages from utils.general import ( check_img_size, non_max_suppression, apply_classifier, scale_coords, xyxy2xywh, plot_one_box, strip_optimizer, set_logging) from utils.torch_utils import select_device, load_classifier, time_synchronized # 定义全局变量 video_path = "ljlj.mp4" class Detect_UI(tk.Tk): def __init__(self): super().__init__() self.title("垃圾分类识别") self.geometry("1280x720") self.label_video = tk.Label(self) self.label_video.place(x=20, y=20, width=640, height=320) self.label_recognition = tk.Label(self) self.label_recognition.place(x=20, y=320, width=640, height=400) self.Counts = tk.Label(self, text="投放顺序", font=("Helvetica", 24), wraplength=140, justify="left") self.Counts.place(x=690, y=140, width=140, height=48) self.counts = tk.Label(self, text="", font=("Helvetica", 24), wraplength=80, justify="left") self.counts.place(x=740, y=280, width=48, height=48) self.amount = tk.Label(self, text="数量", font=("Helvetica", 24), wraplength=80, justify="left") self.amount.place(x=1060, y=140, width=96, height=48) self.Kind = tk.Label(self, text="垃圾名称", font=("Helvetica", 24), wraplength=160, justify="left") self.Kind.place(x=880, y=140, width=140, height=48) self.kind = tk.Label(self, text="", font=("Helvetica", 24), wraplength=400, justify="left") self.kind.place(x=850, y=280, width=300, height=48) self.ok = tk.Label(self, text="任务完成提示", font=("Helvetica", 24), wraplength=240, justify="left") self.ok.place(x=680, y=420, width=240, height=48) self.okok = tk.Label(self, text="", font=("Helvetica", 24), wraplength=100, justify="left") self.okok.place(x=750, y=500, width=100, height=48) self.fulldetect = tk.Label(self, text="满载情况", font=("Helvetica", 24), wraplength=160, justify="left") self.fulldetect.place(x=1000, y=420, width=160, height=48) self.full = tk.Label(self, text="未满载", font=("Helvetica", 24), wraplength=240, justify="left") self.full.place(x=960, y=500, width=240, height=48) # self.start_button = tk.Button(self, text="重置", command=self.refresh,width=10, height=2,bg="black",fg="white") # self.start_button.place(x=900, y=50) self.start_button = tk.Button(self, text="重置", command=self.refresh, width=10, height=2, bg="black", fg="white", font=("Helvetica", 20)) self.start_button.place(x=860, y=30) self.video_thread = None self.recognition_thread = None self.play_video() self.start_recognition() def play_video(self): def play(): cap = cv2.VideoCapture(video_path) while cap.isOpened(): ret, frame = cap.rea