-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathberdiboys.py
More file actions
54 lines (42 loc) · 1.54 KB
/
Copy pathberdiboys.py
File metadata and controls
54 lines (42 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import cv2
from ultralytics import YOLO
from roboflow import Roboflow
from IPython.display import Image, display
# Define model path, video path, and output video path
MODEL_PATH = '/Users/pascal/desktop/yolov8m-football-1.pt'
VIDEO_PATH = '/Users/pascal/Desktop/soccer.mp4'
OUTPUT_VIDEO_PATH = '/Users/pascal/Desktop/soccer_output.mp4'
# Train the YOLO model with the specified dataset
model = YOLO("yolov8m-football.pt")
results = model.train( epochs=10, imgsz=320)
# Load the trained YOLO model
model = YOLO(MODEL_PATH)
# Open the video file
cap = cv2.VideoCapture(VIDEO_PATH)
# Get video properties
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
frame_rate = int(cap.get(cv2.CAP_PROP_FPS))
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(OUTPUT_VIDEO_PATH, fourcc, frame_rate, (frame_width, frame_height))
# Loop through the video frames
while cap.isOpened():
success, frame = cap.read()
if success:
# Perform inference and plot detections
results = model(frame, verbose=False, conf=0.5)
annotated_frame = results[0].plot()
# Write annotated frame to the output video
out.write(annotated_frame)
# Display the annotated frame
cv2.imshow("YOLOv8 Inference", annotated_frame)
# Handle user input for quitting
if cv2.waitKey(1) & 0xFF == ord("q"):
break
else:
break
# Release resources
cap.release()
out.release()
cv2.destroyAllWindows()