Aumkeshchy2003 commited on
Commit
0e19825
·
verified ·
1 Parent(s): 3e3644e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -34
app.py CHANGED
@@ -3,58 +3,112 @@ import numpy as np
3
  import gradio as gr
4
  import cv2
5
  import time
6
- from PIL import Image
7
 
 
8
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 
9
 
10
- # Load YOLOv5 model
11
  model = torch.hub.load("ultralytics/yolov5", "yolov5x", pretrained=True).to(device)
12
 
 
 
 
 
 
 
13
  if device.type == "cuda":
14
  model.half() # Use FP16 for performance boost
15
 
16
- # Print available object classes
17
- print(f"Model loaded with {len(model.names)} classes: {model.names}")
18
 
19
- # Assign random colors to each class for bounding boxes
20
- colors = {i: [int(c) for c in np.random.randint(0, 255, 3)] for i in range(len(model.names))}
 
21
 
22
  def detect_objects(image):
23
- start_time = time.time() # Start FPS measurement
24
-
25
-
26
- img_tensor = torch.from_numpy(image).permute(2, 0, 1).float().to(device) / 255.0
27
- img_tensor = img_tensor.unsqueeze(0)
28
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  with torch.no_grad():
30
- results = model(img_tensor)
31
 
32
- detections = results.xyxy[0].cpu().numpy()
33
-
34
- img_cv = image.copy()
35
-
36
- for det in detections:
37
- x1, y1, x2, y2, conf, cls = map(int, det[:6])
38
- label = f"{model.names[cls]}: {conf:.2f}"
39
-
40
- cv2.rectangle(img_cv, (x1, y1), (x2, y2), colors[cls], 2)
41
- cv2.putText(img_cv, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, colors[cls], 2)
42
-
43
- # FPS Calculation
44
- end_time = time.time()
45
- fps = 1 / (end_time - start_time)
46
- print(f"FPS: {fps:.2f}")
47
-
48
- return img_cv
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
- # Gradio interface
51
  iface = gr.Interface(
52
  fn=detect_objects,
53
  inputs=gr.Image(type="numpy", label="Upload Image"),
54
  outputs=gr.Image(type="numpy", label="Detected Objects"),
55
- title="Object Detection with YOLOv5",
56
- description="Optimized for 30+ FPS real-time object detection!",
 
 
 
 
 
 
57
  allow_flagging="never",
 
58
  )
59
 
60
- iface.launch()
 
 
 
3
  import gradio as gr
4
  import cv2
5
  import time
 
6
 
7
+ # Check device availability
8
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
9
+ print(f"Using device: {device}")
10
 
11
+ # Load YOLOv5x model (larger model for better accuracy)
12
  model = torch.hub.load("ultralytics/yolov5", "yolov5x", pretrained=True).to(device)
13
 
14
+ # Optimization configurations
15
+ model.conf = 0.3 # Confidence threshold of 0.3 as specified
16
+ model.iou = 0.3 # NMS IoU threshold of 0.3 as specified
17
+ model.classes = None # Detect all 80+ COCO classes
18
+
19
+ # Enable half-precision for GPU acceleration
20
  if device.type == "cuda":
21
  model.half() # Use FP16 for performance boost
22
 
23
+ # Set model to evaluation mode for inference
24
+ model.eval()
25
 
26
+ # Assign fixed colors to each class for consistent visualization
27
+ np.random.seed(42) # For reproducible colors
28
+ colors = np.random.uniform(0, 255, size=(len(model.names), 3))
29
 
30
  def detect_objects(image):
31
+ """
32
+ Process input image for object detection using YOLOv5
33
+
34
+ Args:
35
+ image: Input image as numpy array
36
+
37
+ Returns:
38
+ output_image: Image with detection results visualized
39
+ """
40
+ start_time = time.time()
41
+
42
+ # Convert image to RGB if it's in BGR format
43
+ if image.shape[2] == 3 and image[0,0,0] == image[0,0,2]:
44
+ image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
45
+
46
+ # Create a copy for drawing results
47
+ output_image = image.copy()
48
+
49
+ # Resize input to 640x640 for optimal processing speed
50
+ input_size = 640
51
+
52
+ # Perform inference with no gradient calculation
53
  with torch.no_grad():
54
+ results = model(image, size=input_size)
55
 
56
+ # Extract detections from first (and only) image
57
+ detections = results.pred[0].cpu().numpy()
58
+
59
+ # Draw each detection on the output image
60
+ for *xyxy, conf, cls in detections:
61
+ # Extract coordinates and convert to integers
62
+ x1, y1, x2, y2 = map(int, xyxy)
63
+ class_id = int(cls)
64
+
65
+ # Get color for this class
66
+ color = colors[class_id].tolist()
67
+
68
+ # Draw bounding box
69
+ cv2.rectangle(output_image, (x1, y1), (x2, y2), color, 2)
70
+
71
+ # Create label with class name and confidence score
72
+ label = f"{model.names[class_id]} {conf:.2f}"
73
+
74
+ # Calculate text size for background rectangle
75
+ (w, h), _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)
76
+
77
+ # Draw label background
78
+ cv2.rectangle(output_image, (x1, y1 - 20), (x1 + w, y1), color, -1)
79
+
80
+ # Draw label text
81
+ cv2.putText(output_image, label, (x1, y1 - 5),
82
+ cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
83
+
84
+ # Calculate and display FPS
85
+ fps = 1 / (time.time() - start_time)
86
+
87
+ # Add FPS counter to the image
88
+ cv2.putText(output_image, f"FPS: {fps:.2f}", (10, 30),
89
+ cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
90
+
91
+ print(f"Detection complete - FPS: {fps:.2f}")
92
+
93
+ return output_image
94
 
95
+ # Create Gradio interface
96
  iface = gr.Interface(
97
  fn=detect_objects,
98
  inputs=gr.Image(type="numpy", label="Upload Image"),
99
  outputs=gr.Image(type="numpy", label="Detected Objects"),
100
+ title="Optimized Object Detection with YOLOv5x",
101
+ description="""
102
+ This system utilizes YOLOv5x to detect 80+ object types from the COCO dataset.
103
+ - Processing speed: Optimized for 30+ FPS at 640x640 resolution
104
+ - Confidence threshold: 0.3
105
+ - IoU threshold: 0.3
106
+ - Color-coded bounding boxes with confidence scores
107
+ """,
108
  allow_flagging="never",
109
+ examples=["spring_street_after.jpg", "pexels-hikaique-109919.jpg"],
110
  )
111
 
112
+ # Launch the interface
113
+ if __name__ == "__main__":
114
+ iface.launch()