RizwanMunawar commited on
Commit
637e0b8
β€’
1 Parent(s): 783e9d0

add support for video inference

Browse files
Files changed (1) hide show
  1. app.py +16 -10
app.py CHANGED
@@ -24,14 +24,14 @@ def predict_image(img, conf_threshold, iou_threshold):
24
 
25
  return im
26
 
27
- def predict_video(video, conf_threshold, iou_threshold):
28
  """Predicts objects in a video using a YOLOv8 model with adjustable confidence and IOU thresholds."""
29
  # Create a temporary file to save the processed video
30
  temp_output = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
31
  temp_output.close()
32
 
33
  # Load video
34
- cap = cv2.VideoCapture(video)
35
 
36
  # Get video properties
37
  width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
@@ -69,22 +69,28 @@ def predict_video(video, conf_threshold, iou_threshold):
69
 
70
  return temp_output.name
71
 
72
- # Create a Gradio interface with support for both images and videos
 
 
 
 
 
 
 
73
  iface = gr.Interface(
74
- fn=lambda img, conf_threshold, iou_threshold, is_video: predict_video(img, conf_threshold, iou_threshold) if is_video else predict_image(img, conf_threshold, iou_threshold),
75
  inputs=[
76
- gr.Video(type="file", optional=True, label="Upload Video"),
77
- gr.Image(type="pil", optional=True, label="Upload Image"),
78
  gr.Slider(minimum=0, maximum=1, value=0.25, label="Confidence threshold"),
79
  gr.Slider(minimum=0, maximum=1, value=0.45, label="IoU threshold"),
80
- gr.Checkbox(label="Is Video?", default=False),
81
  ],
82
- outputs=gr.Image(type="pil", label="Result") if not gr.Checkbox else gr.Video(type="file", label="Result"),
83
  title="Ultralytics Gradio Application πŸš€",
84
  description="Upload images or videos for inference. The Ultralytics YOLOv8n model is used by default.",
85
  examples=[
86
- [ASSETS / "bus.jpg", 0.25, 0.45, False],
87
- [ASSETS / "zidane.jpg", 0.25, 0.45, False],
88
  ],
89
  )
90
 
 
24
 
25
  return im
26
 
27
+ def predict_video(video_path, conf_threshold, iou_threshold):
28
  """Predicts objects in a video using a YOLOv8 model with adjustable confidence and IOU thresholds."""
29
  # Create a temporary file to save the processed video
30
  temp_output = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
31
  temp_output.close()
32
 
33
  # Load video
34
+ cap = cv2.VideoCapture(video_path)
35
 
36
  # Get video properties
37
  width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
 
69
 
70
  return temp_output.name
71
 
72
+ def process_input(input_file, conf_threshold, iou_threshold, mode):
73
+ """Handles both image and video inference based on the selected mode."""
74
+ if mode == "Image":
75
+ return predict_image(input_file, conf_threshold, iou_threshold)
76
+ elif mode == "Video":
77
+ return predict_video(input_file.name, conf_threshold, iou_threshold)
78
+
79
+ # Create Gradio interface
80
  iface = gr.Interface(
81
+ fn=process_input,
82
  inputs=[
83
+ gr.File(label="Upload Image or Video File"), # Use a generic File input for both image and video
 
84
  gr.Slider(minimum=0, maximum=1, value=0.25, label="Confidence threshold"),
85
  gr.Slider(minimum=0, maximum=1, value=0.45, label="IoU threshold"),
86
+ gr.Radio(choices=["Image", "Video"], label="Select Mode", value="Image"),
87
  ],
88
+ outputs=gr.Image(type="pil", label="Result") if gr.Radio else gr.Video(type="file", label="Result"),
89
  title="Ultralytics Gradio Application πŸš€",
90
  description="Upload images or videos for inference. The Ultralytics YOLOv8n model is used by default.",
91
  examples=[
92
+ [ASSETS / "bus.jpg", 0.25, 0.45, "Image"],
93
+ [ASSETS / "zidane.jpg", 0.25, 0.45, "Image"],
94
  ],
95
  )
96