| import os
|
| import sys
|
| import fcntl
|
|
|
|
|
| lock_file_path = '/tmp/my_app.lock'
|
| lock_file = open(lock_file_path, 'w')
|
|
|
| try:
|
|
|
| fcntl.flock(lock_file, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
| except BlockingIOError:
|
|
|
| sys.exit(0)
|
|
|
| import tkinter as tk
|
| from tkinter.ttk import *
|
| import cv2
|
| from PIL import Image, ImageTk
|
| from tkinter import filedialog
|
| import pandas as pd
|
| from ultralytics import YOLO
|
| import cvzone
|
| import threading
|
|
|
|
|
| cap = None
|
| is_camera_on = False
|
| frame_count = 0
|
| frame_skip_threshold = 3
|
| model = YOLO(model='best.pt').to(device='cpu')
|
| video_paused = False
|
|
|
|
|
| def read_classes_from_file(file_path):
|
| with open(file_path, 'r') as file:
|
| classes = [line.strip() for line in file]
|
| return classes
|
|
|
|
|
| def start_webcam():
|
| global cap, is_camera_on, video_paused
|
| if not is_camera_on:
|
| cap = cv2.VideoCapture(0)
|
| is_camera_on = True
|
| video_paused = False
|
| update_canvas()
|
|
|
|
|
| def stop_webcam():
|
| global cap, is_camera_on, video_paused
|
| if cap is not None:
|
| cap.release()
|
| is_camera_on = False
|
| video_paused = False
|
|
|
|
|
| def pause_resume_video():
|
| global video_paused
|
| video_paused = not video_paused
|
|
|
|
|
| def select_file():
|
| global cap, is_camera_on, video_paused
|
| if is_camera_on:
|
| stop_webcam()
|
| file_path = filedialog.askopenfilename(filetypes=[("Video files", "*.mp4 *.avi *.mov")])
|
| if file_path:
|
| cap = cv2.VideoCapture(file_path)
|
| is_camera_on = True
|
| video_paused = False
|
| update_canvas()
|
|
|
|
|
| def update_canvas():
|
| global is_camera_on, frame_count, video_paused
|
| if is_camera_on:
|
| if not video_paused:
|
| ret, frame = cap.read()
|
| if ret:
|
| frame_count += 1
|
| if frame_count % frame_skip_threshold != 0:
|
| canvas.after(10, update_canvas)
|
| return
|
|
|
|
|
|
|
| selected_class = class_selection.get()
|
|
|
| results = model.predict(frame)
|
| a = results[0].boxes.data
|
| px = pd.DataFrame(a.cpu().numpy()).astype("float")
|
| for index, row in px.iterrows():
|
| x1 = int(row[0])
|
| y1 = int(row[1])
|
| x2 = int(row[2])
|
| y2 = int(row[3])
|
| d = int(row[5])
|
| c = class_list[d]
|
| if selected_class == "All" or c == selected_class:
|
| cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 255), 2)
|
| cvzone.putTextRect(frame, f'{c}', (x1, y1), 1, 1)
|
|
|
| photo = ImageTk.PhotoImage(image=Image.fromarray(frame))
|
| canvas.img = photo
|
| canvas.create_image(0, 0, anchor=tk.NW, image=photo)
|
|
|
| canvas.after(10, update_canvas)
|
|
|
|
|
| def quit_app():
|
| stop_webcam()
|
| root.quit()
|
| root.destroy()
|
|
|
|
|
| root = tk.Tk()
|
| root.title("YOLO v8 My App")
|
|
|
|
|
| canvas = tk.Canvas(root, width=1020, height=500)
|
| canvas.pack(fill='both', expand=True)
|
|
|
| class_list = read_classes_from_file('coco.txt')
|
|
|
| class_selection = tk.StringVar()
|
| class_selection.set("All")
|
| class_selection_label = tk.Label(root, text="Select Class:")
|
| class_selection_label.pack(side='left')
|
| class_selection_entry = tk.OptionMenu(root, class_selection, "All", *class_list)
|
| class_selection_entry.pack(side='left')
|
|
|
|
|
| button_frame = tk.Frame(root)
|
| button_frame.pack(fill='x')
|
|
|
|
|
| play_button = tk.Button(button_frame, text="Play", command=start_webcam)
|
| play_button.pack(side='left')
|
|
|
|
|
| stop_button = tk.Button(button_frame, text="Stop", command=stop_webcam)
|
| stop_button.pack(side='left')
|
|
|
|
|
| file_button = tk.Button(button_frame, text="Select File", command=select_file)
|
| file_button.pack(side='left')
|
|
|
|
|
| pause_button = tk.Button(button_frame, text="Pause/Resume", command=pause_resume_video)
|
| pause_button.pack(side='left')
|
|
|
|
|
| quit_button = tk.Button(button_frame, text="Quit", command=quit_app)
|
| quit_button.pack(side='left')
|
|
|
|
|
| initial_image = Image.open('yolo.jpg')
|
| initial_photo = ImageTk.PhotoImage(image=initial_image)
|
| canvas.img = initial_photo
|
| canvas.create_image(0, 0, anchor=tk.NW, image=initial_photo)
|
|
|
|
|
| root.mainloop()
|
|
|