ElodieA commited on
Commit
c786020
·
verified ·
1 Parent(s): 7b00379

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import tempfile
4
+ import numpy as np
5
+ from ultralytics import YOLO
6
+
7
+ # Load the YOLOv8 model
8
+ model = YOLO('yolov8m.pt') # Ensure you have the correct model path
9
+
10
+ def process_video(video_file):
11
+ # Create a temporary directory to store processed frames
12
+ temp_dir = tempfile.TemporaryDirectory()
13
+
14
+ # Open the video file
15
+ cap = cv2.VideoCapture(video_file.name)
16
+
17
+ # Get video properties
18
+ fps = int(cap.get(cv2.CAP_PROP_FPS))
19
+ width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
20
+ height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
21
+ codec = cv2.VideoWriter_fourcc(*'mp4v')
22
+
23
+ # Create a VideoWriter object to save the processed video
24
+ output_path = f"{temp_dir.name}/output.mp4"
25
+ out = cv2.VideoWriter(output_path, codec, fps, (width, height))
26
+
27
+ while cap.isOpened():
28
+ ret, frame = cap.read()
29
+ if not ret:
30
+ break
31
+
32
+ # Use YOLO model to detect objects in the frame
33
+ results = model(frame)
34
+
35
+ # Draw bounding boxes and labels on the frame
36
+ annotated_frame = results[0].plot()
37
+
38
+ # Write the frame to the output video
39
+ out.write(annotated_frame)
40
+
41
+ # Release the VideoCapture and VideoWriter objects
42
+ cap.release()
43
+ out.release()
44
+
45
+ return output_path
46
+
47
+ # Define the Gradio interface
48
+ iface = gr.Interface(
49
+ fn=process_video,
50
+ inputs=gr.inputs.Video(type="file"),
51
+ outputs=gr.outputs.Video(),
52
+ title="YOLOv8 Video Object Detection",
53
+ description="Upload a video and apply YOLOv8 object detection."
54
+ )
55
+
56
+ # Launch the Gradio app
57
+ if __name__ == "__main__":
58
+ iface.launch()