Aumkeshchy2003 commited on
Commit
6fea677
·
verified ·
1 Parent(s): a186d85

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -19
app.py CHANGED
@@ -95,27 +95,34 @@ def detect_objects(image):
95
  # Get color for this class
96
  color = colors[class_id].tolist()
97
 
98
- # Draw bounding box
99
- cv2.rectangle(output_image, (x1, y1), (x2, y2), color, 2)
100
 
101
  # Create label with class name and confidence score
102
  label = f"{model.names[class_id]} {conf:.2f}"
103
 
104
- # Calculate text size for background rectangle
105
- (w, h), _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)
106
 
107
- # Draw label background
108
- cv2.rectangle(output_image, (x1, y1 - 20), (x1 + w, y1), color, -1)
 
109
 
110
- # Draw label text
111
- cv2.putText(output_image, label, (x1, y1 - 5),
112
- cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
 
113
 
114
  # Calculate FPS
115
  fps = 1 / inference_time
116
 
117
- # Add FPS counter to the image
118
- cv2.putText(output_image, f"FPS: {fps:.2f}", (10, 30),
 
 
 
 
 
 
119
  cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
120
  cv2.putText(output_image, f"Avg FPS: {1/avg_inference_time:.2f}", (10, 70),
121
  cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
@@ -142,16 +149,15 @@ with gr.Blocks(title="Optimized YOLOv5 Object Detection") as demo:
142
  - Processing speed: Optimized for 30+ FPS at 640x640 resolution
143
  - Confidence threshold: 0.3
144
  - IoU threshold: 0.3
145
- - Real-time FPS display
146
 
147
- Simply upload an image or take a photo with your camera to see the detections!
148
  """)
149
 
150
  with gr.Row():
151
  with gr.Column(scale=1):
152
  input_image = gr.Image(label="Input Image", type="numpy")
153
  with gr.Row():
154
- camera_button = gr.Button("Take Photo from Camera")
155
  clear_button = gr.Button("Clear")
156
 
157
  with gr.Column(scale=1):
@@ -166,12 +172,10 @@ with gr.Blocks(title="Optimized YOLOv5 Object Detection") as demo:
166
  cache_examples=True # Cache for faster response
167
  )
168
 
169
- # Set up the inference call
170
- input_image.change(fn=detect_objects, inputs=input_image, outputs=output_image)
171
-
172
- # Event listeners for buttons
173
- camera_button.click(lambda: None, None, input_image, js="() => {document.querySelector('button.webcam').click(); return null}")
174
  clear_button.click(lambda: None, None, [input_image, output_image])
 
175
 
176
  # Launch for Hugging Face Spaces
177
  demo.launch()
 
95
  # Get color for this class
96
  color = colors[class_id].tolist()
97
 
98
+ cv2.rectangle(output_image, (x1, y1), (x2, y2), color, 4)
 
99
 
100
  # Create label with class name and confidence score
101
  label = f"{model.names[class_id]} {conf:.2f}"
102
 
103
+ font_scale = 0.8
104
+ font_thickness = 2
105
 
106
+ (w, h), _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, font_scale, font_thickness)
107
+
108
+ cv2.rectangle(output_image, (x1, y1 - h - 10), (x1 + w + 10, y1), color, -1)
109
 
110
+ cv2.putText(output_image, label, (x1 + 5, y1 - 5),
111
+ cv2.FONT_HERSHEY_SIMPLEX, font_scale, (0, 0, 0), font_thickness + 1)
112
+ cv2.putText(output_image, label, (x1 + 5, y1 - 5),
113
+ cv2.FONT_HERSHEY_SIMPLEX, font_scale, (255, 255, 255), font_thickness)
114
 
115
  # Calculate FPS
116
  fps = 1 / inference_time
117
 
118
+ fps_overlay = output_image.copy()
119
+ cv2.rectangle(fps_overlay, (5, 5), (250, 80), (0, 0, 0), -1)
120
+ # Apply the overlay with transparency
121
+ alpha = 0.7
122
+ output_image = cv2.addWeighted(fps_overlay, alpha, output_image, 1 - alpha, 0)
123
+
124
+ # Display FPS with larger font
125
+ cv2.putText(output_image, f"FPS: {fps:.2f}", (10, 35),
126
  cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
127
  cv2.putText(output_image, f"Avg FPS: {1/avg_inference_time:.2f}", (10, 70),
128
  cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
 
149
  - Processing speed: Optimized for 30+ FPS at 640x640 resolution
150
  - Confidence threshold: 0.3
151
  - IoU threshold: 0.3
 
152
 
153
+ Simply upload an image and click Submit to see the detections!
154
  """)
155
 
156
  with gr.Row():
157
  with gr.Column(scale=1):
158
  input_image = gr.Image(label="Input Image", type="numpy")
159
  with gr.Row():
160
+ submit_button = gr.Button("Submit", variant="primary")
161
  clear_button = gr.Button("Clear")
162
 
163
  with gr.Column(scale=1):
 
172
  cache_examples=True # Cache for faster response
173
  )
174
 
175
+ # Set up button event handlers
176
+ submit_button.click(fn=detect_objects, inputs=input_image, outputs=output_image)
 
 
 
177
  clear_button.click(lambda: None, None, [input_image, output_image])
178
+
179
 
180
  # Launch for Hugging Face Spaces
181
  demo.launch()