Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,98 +1,53 @@
|
|
| 1 |
-
import
|
| 2 |
-
import tkinter as tk
|
| 3 |
-
from tkinter import ttk, filedialog # Import ttk for themed widgets
|
| 4 |
from ultralytics import YOLO
|
| 5 |
-
import
|
| 6 |
-
import
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
# GUI elements with themed widgets
|
| 21 |
-
self.load_model_button = ttk.Button(master, text="Load Model", command=self.load_model)
|
| 22 |
-
self.load_model_button.pack(pady=10, padx=10, fill=tk.X) # Fill horizontally
|
| 23 |
-
|
| 24 |
-
self.select_video_button = ttk.Button(master, text="Select Video/Camera", command=self.select_video)
|
| 25 |
-
self.select_video_button.pack(pady=10, padx=10, fill=tk.X)
|
| 26 |
-
|
| 27 |
-
self.start_button = ttk.Button(master, text="Start Detection", command=self.start_detection, state=tk.DISABLED)
|
| 28 |
-
self.start_button.pack(pady=10, padx=10, fill=tk.X)
|
| 29 |
-
|
| 30 |
-
self.stop_button = ttk.Button(master, text="Stop Detection", command=self.stop_detection, state=tk.DISABLED)
|
| 31 |
-
self.stop_button.pack(pady=10, padx=10, fill=tk.X)
|
| 32 |
-
|
| 33 |
-
self.video_label = ttk.Label(master) # Themed label
|
| 34 |
-
self.video_label.pack(pady=(0, 10), padx=10) #padding only at the bottom and left and right
|
| 35 |
-
|
| 36 |
-
self.detection_running = False
|
| 37 |
-
self.thread = None
|
| 38 |
-
|
| 39 |
-
# Status Label (for messages)
|
| 40 |
-
self.status_label = ttk.Label(master, text="", wraplength=300) #wraplength to wrap text
|
| 41 |
-
self.status_label.pack(pady=(0,10),padx=10)
|
| 42 |
-
|
| 43 |
-
# ... (rest of the methods: load_model, select_video, start/stop detection, detect_pedestrians)
|
| 44 |
-
|
| 45 |
-
def load_model(self):
|
| 46 |
-
try:
|
| 47 |
-
model_path = "yolov8n.pt"
|
| 48 |
-
self.model = YOLO(model_path)
|
| 49 |
-
self.load_model_button.config(state=tk.DISABLED)
|
| 50 |
-
self.status_label.config(text="Model loaded successfully.") #update status
|
| 51 |
-
except Exception as e:
|
| 52 |
-
self.status_label.config(text=f"Error loading model: {e}") #update status
|
| 53 |
-
|
| 54 |
-
def select_video(self):
|
| 55 |
-
try:
|
| 56 |
-
self.video_source = 0 # Webcam
|
| 57 |
-
if self.video_source is not None:
|
| 58 |
-
self.start_button.config(state=tk.NORMAL)
|
| 59 |
-
self.status_label.config(text=f"Video source selected: {self.video_source}") #update status
|
| 60 |
-
except Exception as e:
|
| 61 |
-
self.status_label.config(text=f"Error selecting video: {e}") #update status
|
| 62 |
-
|
| 63 |
-
def start_detection(self):
|
| 64 |
-
# ... (same as before)
|
| 65 |
-
self.status_label.config(text="Detection started.") #update status
|
| 66 |
-
|
| 67 |
-
def stop_detection(self):
|
| 68 |
-
# ... (same as before)
|
| 69 |
-
self.status_label.config(text="Detection stopped.") #update status
|
| 70 |
-
|
| 71 |
-
def detect_pedestrians(self):
|
| 72 |
-
cap = cv2.VideoCapture(self.video_source)
|
| 73 |
-
|
| 74 |
-
while self.detection_running:
|
| 75 |
ret, frame = cap.read()
|
| 76 |
if not ret:
|
| 77 |
-
self.status_label.config(text="Video ended.") #update status
|
| 78 |
-
self.stop_detection()
|
| 79 |
break
|
| 80 |
|
| 81 |
-
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
-
cap.release()
|
| 93 |
-
cv2.destroyAllWindows()
|
| 94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
-
|
| 97 |
-
detector = PedestrianDetector(root)
|
| 98 |
-
root.mainloop()
|
|
|
|
| 1 |
+
import gradio as gr
|
|
|
|
|
|
|
| 2 |
from ultralytics import YOLO
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# Load the model (do this outside the function for efficiency)
|
| 7 |
+
try:
|
| 8 |
+
model = YOLO("yolov8n.pt") # Or your model path
|
| 9 |
+
except Exception as e:
|
| 10 |
+
print(f"Error loading model: {e}")
|
| 11 |
+
exit() # Exit if the model fails to load
|
| 12 |
+
|
| 13 |
+
def detect_pedestrians(video):
|
| 14 |
+
try:
|
| 15 |
+
cap = cv2.VideoCapture(video)
|
| 16 |
+
frames = []
|
| 17 |
+
while True:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
ret, frame = cap.read()
|
| 19 |
if not ret:
|
|
|
|
|
|
|
| 20 |
break
|
| 21 |
|
| 22 |
+
results = model(frame)
|
| 23 |
|
| 24 |
+
for result in results:
|
| 25 |
+
boxes = result.boxes
|
| 26 |
+
for box in boxes:
|
| 27 |
+
if result.names[int(box.cls)] == 'person':
|
| 28 |
+
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
| 29 |
+
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
| 30 |
+
cv2.putText(frame, 'Person', (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
| 31 |
+
frames.append(frame)
|
| 32 |
+
cap.release()
|
| 33 |
|
| 34 |
+
if not frames: # Handle cases where no frames are read (e.g., corrupted video)
|
| 35 |
+
return np.zeros((480, 640, 3), dtype=np.uint8) # Return a blank image
|
| 36 |
+
|
| 37 |
+
return frames[-1] # Return the last frame with detections
|
| 38 |
+
|
| 39 |
+
except Exception as e:
|
| 40 |
+
print(f"Error in detection: {e}")
|
| 41 |
+
return f"Error: {e}" # Return the error message as a string
|
| 42 |
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
iface = gr.Interface(
|
| 45 |
+
fn=detect_pedestrians,
|
| 46 |
+
inputs=gr.Video(source="upload"), # Use gr.Video for video input
|
| 47 |
+
outputs=gr.Image(), # Use gr.Image for image output
|
| 48 |
+
title="Pedestrian Detection",
|
| 49 |
+
description="Upload a video to detect pedestrians.",
|
| 50 |
+
allow_flagging="never", # Removes flag button
|
| 51 |
+
)
|
| 52 |
|
| 53 |
+
iface.launch()
|
|
|
|
|
|