RizwanMunawar commited on
Commit
783e9d0
Β·
1 Parent(s): 8ff935e

add support for video inference

Browse files
Files changed (1) hide show
  1. app.py +58 -8
app.py CHANGED
@@ -1,11 +1,12 @@
1
  import gradio as gr
2
  import PIL.Image as Image
3
-
 
4
  from ultralytics import ASSETS, YOLO
5
 
 
6
  model = YOLO("yolov8n.pt")
7
 
8
-
9
  def predict_image(img, conf_threshold, iou_threshold):
10
  """Predicts objects in an image using a YOLOv8 model with adjustable confidence and IOU thresholds."""
11
  results = model.predict(
@@ -23,19 +24,68 @@ def predict_image(img, conf_threshold, iou_threshold):
23
 
24
  return im
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  iface = gr.Interface(
27
- fn=predict_image,
28
  inputs=[
29
- gr.Image(type="pil", label="Upload Image"),
 
30
  gr.Slider(minimum=0, maximum=1, value=0.25, label="Confidence threshold"),
31
  gr.Slider(minimum=0, maximum=1, value=0.45, label="IoU threshold"),
 
32
  ],
33
- outputs=gr.Image(type="pil", label="Result"),
34
  title="Ultralytics Gradio Application πŸš€",
35
- description="Upload images for inference. The Ultralytics YOLOv8n model is used by default.",
36
  examples=[
37
- [ASSETS / "bus.jpg", 0.25, 0.45],
38
- [ASSETS / "zidane.jpg", 0.25, 0.45],
39
  ],
40
  )
 
41
  iface.launch(share=True)
 
1
  import gradio as gr
2
  import PIL.Image as Image
3
+ import tempfile
4
+ import cv2
5
  from ultralytics import ASSETS, YOLO
6
 
7
+ # Load YOLOv8 model
8
  model = YOLO("yolov8n.pt")
9
 
 
10
  def predict_image(img, conf_threshold, iou_threshold):
11
  """Predicts objects in an image using a YOLOv8 model with adjustable confidence and IOU thresholds."""
12
  results = model.predict(
 
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))
38
+ height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
39
+ fps = int(cap.get(cv2.CAP_PROP_FPS))
40
+
41
+ # Set up VideoWriter to save output video
42
+ out = cv2.VideoWriter(temp_output.name, cv2.VideoWriter_fourcc(*"mp4v"), fps, (width, height))
43
+
44
+ while cap.isOpened():
45
+ ret, frame = cap.read()
46
+ if not ret:
47
+ break
48
+
49
+ # Perform inference on each frame
50
+ results = model.predict(
51
+ source=frame,
52
+ conf=conf_threshold,
53
+ iou=iou_threshold,
54
+ show_labels=True,
55
+ show_conf=True,
56
+ imgsz=640,
57
+ )
58
+
59
+ # Draw the results on the frame
60
+ for r in results:
61
+ frame = r.plot()
62
+
63
+ # Write the frame to the output video
64
+ out.write(frame)
65
+
66
+ # Release resources
67
+ cap.release()
68
+ out.release()
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
+
91
  iface.launch(share=True)