keffy commited on
Commit
dae4253
1 Parent(s): 08ef8fa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -8
app.py CHANGED
@@ -16,10 +16,12 @@ def yolo_inference(input_file):
16
  results = model(img)
17
  annotated_img = results[0].plot()
18
 
19
- # Save the annotated image
20
- output_path = tempfile.NamedTemporaryFile(suffix=".png", delete=False).name
21
- cv2.imwrite(output_path, annotated_img)
22
- return output_path
 
 
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
- output_video_path = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False).name
 
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
- return output_video_path
 
 
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=gr.File(label="Processed Image/Video"),
58
  title="YOLO Object Detection",
59
- description="Upload an image or video for object detection using YOLO."
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