Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -222,6 +222,9 @@ def process_video(confidence_threshold=0.5, selected_classes=None, stream_url=No
|
|
| 222 |
# Perform object tracking with confidence threshold on the resized frame
|
| 223 |
results = model.track(resized_frame, persist=True, conf=confidence_threshold)
|
| 224 |
|
|
|
|
|
|
|
|
|
|
| 225 |
if results[0].boxes.id is not None:
|
| 226 |
track_ids = results[0].boxes.id.int().cpu().tolist()
|
| 227 |
clss = results[0].boxes.cls.cpu().tolist()
|
|
@@ -232,6 +235,11 @@ def process_video(confidence_threshold=0.5, selected_classes=None, stream_url=No
|
|
| 232 |
if conf >= confidence_threshold and model.names[cls] in selected_classes:
|
| 233 |
# Scale the bounding box back to the original resolution
|
| 234 |
box = box * (original_width / new_width)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
# Check if the object crosses the line
|
| 236 |
if is_object_crossing_line(box, line_params) and t_id not in crossed_objects:
|
| 237 |
crossed_objects[t_id] = True
|
|
@@ -240,11 +248,8 @@ def process_video(confidence_threshold=0.5, selected_classes=None, stream_url=No
|
|
| 240 |
if len(crossed_objects) > max_tracked_objects:
|
| 241 |
crossed_objects.clear()
|
| 242 |
|
| 243 |
-
# Visualize the results with bounding boxes, masks, and IDs on the original frame
|
| 244 |
-
annotated_frame = results[0].plot(img=frame)
|
| 245 |
-
|
| 246 |
# Draw the angled line on the original frame
|
| 247 |
-
draw_angled_line(
|
| 248 |
|
| 249 |
# Display the count on the frame with a modern look
|
| 250 |
count = len(crossed_objects)
|
|
@@ -252,17 +257,17 @@ def process_video(confidence_threshold=0.5, selected_classes=None, stream_url=No
|
|
| 252 |
|
| 253 |
# Calculate the position for the middle of the top
|
| 254 |
margin = 10 # Margin from the top
|
| 255 |
-
x = (
|
| 256 |
y = text_height + margin # Top-align the text
|
| 257 |
|
| 258 |
# Draw the black background rectangle
|
| 259 |
-
cv2.rectangle(
|
| 260 |
|
| 261 |
# Draw the text
|
| 262 |
-
cv2.putText(
|
| 263 |
|
| 264 |
# Yield the annotated frame to Gradio
|
| 265 |
-
yield
|
| 266 |
|
| 267 |
cap.release()
|
| 268 |
logger.info("Stream processing completed.")
|
|
|
|
| 222 |
# Perform object tracking with confidence threshold on the resized frame
|
| 223 |
results = model.track(resized_frame, persist=True, conf=confidence_threshold)
|
| 224 |
|
| 225 |
+
# Create an annotator for the original frame
|
| 226 |
+
annotator = Annotator(frame, line_width=2)
|
| 227 |
+
|
| 228 |
if results[0].boxes.id is not None:
|
| 229 |
track_ids = results[0].boxes.id.int().cpu().tolist()
|
| 230 |
clss = results[0].boxes.cls.cpu().tolist()
|
|
|
|
| 235 |
if conf >= confidence_threshold and model.names[cls] in selected_classes:
|
| 236 |
# Scale the bounding box back to the original resolution
|
| 237 |
box = box * (original_width / new_width)
|
| 238 |
+
box = box.int().tolist()
|
| 239 |
+
|
| 240 |
+
# Draw the bounding box on the original frame
|
| 241 |
+
annotator.box_label(box, label=f"{model.names[cls]} {conf:.2f}", color=colors(cls))
|
| 242 |
+
|
| 243 |
# Check if the object crosses the line
|
| 244 |
if is_object_crossing_line(box, line_params) and t_id not in crossed_objects:
|
| 245 |
crossed_objects[t_id] = True
|
|
|
|
| 248 |
if len(crossed_objects) > max_tracked_objects:
|
| 249 |
crossed_objects.clear()
|
| 250 |
|
|
|
|
|
|
|
|
|
|
| 251 |
# Draw the angled line on the original frame
|
| 252 |
+
draw_angled_line(frame, line_params, color=(0, 255, 0), thickness=2)
|
| 253 |
|
| 254 |
# Display the count on the frame with a modern look
|
| 255 |
count = len(crossed_objects)
|
|
|
|
| 257 |
|
| 258 |
# Calculate the position for the middle of the top
|
| 259 |
margin = 10 # Margin from the top
|
| 260 |
+
x = (frame.shape[1] - text_width) // 2 # Center-align the text horizontally
|
| 261 |
y = text_height + margin # Top-align the text
|
| 262 |
|
| 263 |
# Draw the black background rectangle
|
| 264 |
+
cv2.rectangle(frame, (x - margin, y - text_height - margin), (x + text_width + margin, y + margin), (0, 0, 0), -1)
|
| 265 |
|
| 266 |
# Draw the text
|
| 267 |
+
cv2.putText(frame, f"COUNT: {count}", (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
|
| 268 |
|
| 269 |
# Yield the annotated frame to Gradio
|
| 270 |
+
yield frame, ""
|
| 271 |
|
| 272 |
cap.release()
|
| 273 |
logger.info("Stream processing completed.")
|