Update app.py
Browse files
app.py
CHANGED
|
@@ -16,10 +16,12 @@ def yolo_inference(input_file):
|
|
| 16 |
results = model(img)
|
| 17 |
annotated_img = results[0].plot()
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
cv2.
|
| 22 |
-
|
|
|
|
|
|
|
| 23 |
|
| 24 |
elif input_file.endswith((".mp4", ".avi", ".mov")):
|
| 25 |
# Process as a video
|
|
@@ -30,7 +32,8 @@ def yolo_inference(input_file):
|
|
| 30 |
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 31 |
|
| 32 |
# Create a temporary output video path
|
| 33 |
-
|
|
|
|
| 34 |
out = cv2.VideoWriter(output_video_path, fourcc, fps, (width, height))
|
| 35 |
|
| 36 |
while cap.isOpened():
|
|
@@ -41,11 +44,20 @@ def yolo_inference(input_file):
|
|
| 41 |
# Run YOLO on each frame
|
| 42 |
results = model(frame)
|
| 43 |
annotated_frame = results[0].plot()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
out.write(annotated_frame)
|
| 45 |
|
| 46 |
cap.release()
|
| 47 |
out.release()
|
| 48 |
-
|
|
|
|
|
|
|
| 49 |
|
| 50 |
else:
|
| 51 |
raise ValueError("Unsupported file format. Please upload an image or video.")
|
|
@@ -54,9 +66,9 @@ def yolo_inference(input_file):
|
|
| 54 |
interface = gr.Interface(
|
| 55 |
fn=yolo_inference,
|
| 56 |
inputs=gr.File(label="Upload an Image or Video"),
|
| 57 |
-
outputs=
|
| 58 |
title="YOLO Object Detection",
|
| 59 |
-
description="Upload an image or video for object detection
|
| 60 |
)
|
| 61 |
|
| 62 |
# Launch the app
|
|
|
|
| 16 |
results = model(img)
|
| 17 |
annotated_img = results[0].plot()
|
| 18 |
|
| 19 |
+
# Display the annotated image in a window
|
| 20 |
+
cv2.imshow("YOLO Detection", annotated_img)
|
| 21 |
+
cv2.waitKey(0)
|
| 22 |
+
cv2.destroyAllWindows()
|
| 23 |
+
|
| 24 |
+
return input_file # Return the original file for consistency (can be adjusted)
|
| 25 |
|
| 26 |
elif input_file.endswith((".mp4", ".avi", ".mov")):
|
| 27 |
# Process as a video
|
|
|
|
| 32 |
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 33 |
|
| 34 |
# Create a temporary output video path
|
| 35 |
+
temp_dir = tempfile.mkdtemp()
|
| 36 |
+
output_video_path = os.path.join(temp_dir, "output.mp4")
|
| 37 |
out = cv2.VideoWriter(output_video_path, fourcc, fps, (width, height))
|
| 38 |
|
| 39 |
while cap.isOpened():
|
|
|
|
| 44 |
# Run YOLO on each frame
|
| 45 |
results = model(frame)
|
| 46 |
annotated_frame = results[0].plot()
|
| 47 |
+
|
| 48 |
+
# Display the annotated frame in a window
|
| 49 |
+
cv2.imshow("YOLO Detection", annotated_frame)
|
| 50 |
+
if cv2.waitKey(1) & 0xFF == ord('q'): # Press 'q' to quit early
|
| 51 |
+
break
|
| 52 |
+
|
| 53 |
+
# Save the annotated frame to the video
|
| 54 |
out.write(annotated_frame)
|
| 55 |
|
| 56 |
cap.release()
|
| 57 |
out.release()
|
| 58 |
+
cv2.destroyAllWindows()
|
| 59 |
+
|
| 60 |
+
return input_file # Return the original video file for consistency (can be adjusted)
|
| 61 |
|
| 62 |
else:
|
| 63 |
raise ValueError("Unsupported file format. Please upload an image or video.")
|
|
|
|
| 66 |
interface = gr.Interface(
|
| 67 |
fn=yolo_inference,
|
| 68 |
inputs=gr.File(label="Upload an Image or Video"),
|
| 69 |
+
outputs="text", # Display a message about console output
|
| 70 |
title="YOLO Object Detection",
|
| 71 |
+
description="Upload an image or video for object detection. The results are displayed on the console."
|
| 72 |
)
|
| 73 |
|
| 74 |
# Launch the app
|