-
Notifications
You must be signed in to change notification settings - Fork 2
/
frames.py
43 lines (28 loc) · 1.14 KB
/
frames.py
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
import os
from ultralytics import YOLO
import cv2
VIDEOS_DIR = os.path.join('.', 'videos')
# input videp path (Kindly Enter Your Own path)
video_path = input("Kindly Enter the Video path")
# output video path
video_path_out = '{}_out.mp4'.format(video_path)
cap = cv2.VideoCapture(video_path)
ret, video_frame = cap.read()
H, W, _ = video_frame.shape
out = cv2.VideoWriter(video_path_out, cv2.VideoWriter_fourcc(*'MP4V'), int(cap.get(cv2.CAP_PROP_FPS)), (W, H))
# Load a model
model = YOLO('Model2(Large).pt') # load a custom model
threshold = 0.35
while ret:
results = model(video_frame)[0]
for result in results.boxes.data.tolist():
x1, y1, x2, y2, score, class_id = result
if score > threshold:
cv2.rectangle(video_frame, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 4)
cv2.putText(video_frame, results.names[int(class_id)].upper(), (int(x1), int(y1 - 10)),
cv2.FONT_HERSHEY_SIMPLEX, 1.3, (0, 255, 0), 3, cv2.LINE_AA)
out.write(video_frame)
ret, video_frame = cap.read()
cap.release()
out.release()
cv2.destroyAllWindows()