Aumkeshchy2003 commited on
Commit
3100b46
·
verified ·
1 Parent(s): a228709

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -118
app.py CHANGED
@@ -1,131 +1,71 @@
 
1
  import torch
2
  import numpy as np
3
  import gradio as gr
4
- import cv2
5
- import time
6
- import os
7
- from pathlib import Path
8
-
9
- # Create cache directory for models
10
- os.makedirs("models", exist_ok=True)
11
-
12
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
13
- print(f"Using device: {device}")
14
-
15
- # Load YOLOv5n model (corrected from original)
16
- model_path = Path("models/yolov5n.pt")
17
- if model_path.exists():
18
- print(f"Loading model from cache: {model_path}")
19
- model = torch.hub.load("ultralytics/yolov5", "yolov5n", pretrained=True,
20
- source="local", path=str(model_path)).to(device)
21
- else:
22
- print("Downloading YOLOv5n model and caching...")
23
- model = torch.hub.load("ultralytics/yolov5", "yolov5n", pretrained=True).to(device)
24
- torch.save(model.state_dict(), model_path)
25
-
26
- # Model configurations
27
- model.conf = 0.6
28
- model.iou = 0.45
29
- model.classes = None
30
-
31
- # Optimizations
32
- if device.type == "cuda":
33
- model.half()
34
- torch.backends.cudnn.benchmark = True
35
- else:
36
- torch.set_num_threads(os.cpu_count())
37
 
38
- model.eval()
39
-
40
- np.random.seed(42)
41
- colors = np.random.uniform(0, 255, size=(len(model.names), 3))
42
-
43
- total_inference_time = 0
44
- inference_count = 0
45
 
46
  def detect_objects(image):
47
- global total_inference_time, inference_count
48
-
49
- if image is None:
50
- return None
51
-
52
- # Convert RGB to BGR for OpenCV operations
53
- image_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
54
- output_image = image_bgr.copy()
55
-
56
- start_time = time.time()
57
-
58
- # Convert to RGB for model inference
59
- img_rgb = cv2.cvtColor(output_image, cv2.COLOR_BGR2RGB)
60
 
61
- with torch.no_grad():
62
- results = model(img_rgb, size=320) # Reduced input size for speed
 
 
 
63
 
64
- inference_time = time.time() - start_time
65
- total_inference_time += inference_time
66
- inference_count += 1
67
- avg_inference_time = total_inference_time / inference_count
68
-
69
- detections = results.pred[0].cpu().numpy()
70
-
71
- for *xyxy, conf, cls in detections:
72
- x1, y1, x2, y2 = map(int, xyxy)
73
- class_id = int(cls)
74
- color = colors[class_id].tolist()
 
 
75
 
76
- # Draw bounding boxes
77
- cv2.rectangle(output_image, (x1, y1), (x2, y2), color, 2, lineType=cv2.LINE_AA)
78
 
79
- # Draw labels
80
- label = f"{model.names[class_id]} {conf:.2f}"
81
- (w, h), _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.6, 1)
82
- cv2.rectangle(output_image, (x1, y1 - 20), (x1 + w, y1), color, -1)
83
- cv2.putText(output_image, label, (x1, y1 - 5),
84
- cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 1, lineType=cv2.LINE_AA)
85
-
86
- # Convert back to RGB for Gradio
87
- output_image_rgb = cv2.cvtColor(output_image, cv2.COLOR_BGR2RGB)
88
-
89
- # Draw performance metrics
90
- fps = 1 / inference_time
91
- cv2.putText(output_image_rgb, f"FPS: {fps:.1f}", (10, 30),
92
- cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2, lineType=cv2.LINE_AA)
93
- cv2.putText(output_image_rgb, f"Avg FPS: {1/avg_inference_time:.1f}", (10, 60),
94
- cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2, lineType=cv2.LINE_AA)
95
-
96
- return output_image_rgb
97
 
98
- # Example images
99
- example_images = ["spring_street_after.jpg", "pexels-hikaique-109919.jpg"]
100
- os.makedirs("examples", exist_ok=True)
101
 
102
- with gr.Blocks(title="Real-time YOLOv5 Object Detection") as demo:
103
- gr.Markdown("""
104
- # Real-time YOLOv5 Object Detection
105
- - Real-time webcam detection (30+ FPS on GPU)
106
- - Image upload capability
107
- - Performance optimized with half-precision and CUDA acceleration
108
- """)
109
-
110
- with gr.Tab("🎥 Real-time Webcam"):
111
- with gr.Row():
112
- webcam = gr.Image(source="webcam", streaming=True, label="Live Webcam Feed")
113
- live_output = gr.Image(label="Detection Results")
114
- webcam.stream(fn=detect_objects, inputs=webcam, outputs=live_output)
115
-
116
- with gr.Tab("📸 Image Upload"):
117
- with gr.Row():
118
- with gr.Column():
119
- input_image = gr.Image(type="numpy", label="Input Image")
120
- gr.Examples(examples=example_images, inputs=input_image)
121
- with gr.Row():
122
- submit_btn = gr.Button("Detect Objects", variant="primary")
123
- clear_btn = gr.Button("Clear")
124
-
125
- with gr.Column():
126
- output_image = gr.Image(type="numpy", label="Processed Image")
127
-
128
- submit_btn.click(fn=detect_objects, inputs=input_image, outputs=output_image)
129
- clear_btn.click(lambda: (None, None), outputs=[input_image, output_image])
130
 
131
- demo.launch()
 
1
+ import cv2
2
  import torch
3
  import numpy as np
4
  import gradio as gr
5
+ from ultralytics import YOLO
6
+ import threading
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
+ # Load YOLOv5 model (optimized for CUDA if available)
9
+ device = 'cuda' if torch.cuda.is_available() else 'cpu'
10
+ model = YOLO("yolov5s.pt").to(device)
 
 
 
 
11
 
12
  def detect_objects(image):
13
+ """Detect objects in an uploaded image."""
14
+ results = model(image)
15
+ detections = results[0].boxes.data.cpu().numpy() # Get detections
 
 
 
 
 
 
 
 
 
 
16
 
17
+ for box in detections:
18
+ x1, y1, x2, y2, conf, cls = map(int, box[:6])
19
+ label = f"{model.names[cls]} {conf:.2f}"
20
+ cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
21
+ cv2.putText(image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
22
 
23
+ return image
24
+
25
+ # Real-time webcam processing
26
+ cap = cv2.VideoCapture(0) # Capture from webcam
27
+ frame = None
28
+ lock = threading.Lock()
29
+
30
+ def process_webcam():
31
+ global frame
32
+ while True:
33
+ ret, img = cap.read()
34
+ if not ret:
35
+ continue
36
 
37
+ results = model(img)
38
+ detections = results[0].boxes.data.cpu().numpy()
39
 
40
+ for box in detections:
41
+ x1, y1, x2, y2, conf, cls = map(int, box[:6])
42
+ label = f"{model.names[cls]} {conf:.2f}"
43
+ cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
44
+ cv2.putText(img, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
45
+
46
+ with lock:
47
+ frame = img
 
 
 
 
 
 
 
 
 
 
48
 
49
+ # Start the webcam thread
50
+ threading.Thread(target=process_webcam, daemon=True).start()
 
51
 
52
+ def get_webcam_frame():
53
+ """Returns the latest processed webcam frame."""
54
+ with lock:
55
+ return frame if frame is not None else np.zeros((480, 640, 3), dtype=np.uint8)
56
+
57
+ # Gradio UI
58
+ demo = gr.Blocks()
59
+
60
+ with demo:
61
+ gr.Markdown("# YOLOv5 Real-Time Object Detection")
62
+ with gr.Tabs():
63
+ with gr.Tab("Real-Time Webcam"):
64
+ gr.Video(get_webcam_frame, streaming=True)
65
+ with gr.Tab("Upload Image"):
66
+ image_input = gr.Image(type="numpy")
67
+ image_output = gr.Image()
68
+ image_button = gr.Button("Detect Objects")
69
+ image_button.click(detect_objects, inputs=image_input, outputs=image_output)
 
 
 
 
 
 
 
 
 
 
70
 
71
+ demo.launch()