-
Notifications
You must be signed in to change notification settings - Fork 0
/
Train_license_plate.py
64 lines (50 loc) · 1.71 KB
/
Train_license_plate.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import cv2
from ultralytics import YOLO
# Load the YOLOv8 model
model = YOLO('/runs/detect/train5/weights/best.pt')
# Open the video file
video_path = "/Users/rewatiramansingh/Downloads/Mumbai Highway Running car view | HD Footage | No Copyright.mp4"
cap = cv2.VideoCapture(video_path)
# Get the frame rate and size of the video
fps = cap.get(cv2.CAP_PROP_FPS)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# Define the output video writer
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
output_path = 'output.mp4'
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
# Loop through the video frames
while cap.isOpened():
# Read a frame from the video
success, frame = cap.read()
if success:
# Run YOLOv8 inference on the frame
results = model(frame)
# Visualize the results on the frame
annotated_frame = results[0].plot()
# Write the annotated frame to the output video
out.write(annotated_frame)
# Display the annotated frame
cv2.imshow("YOLOv8 Inference", annotated_frame)
# Break the loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord("q"):
break
else:
# Break the loop if the end of the video is reached
break
# Release the video capture object and close the display window
cap.release()
cv2.destroyAllWindows()
# Release the output video writer
out.release()
# Play the output video
cap = cv2.VideoCapture(output_path)
while cap.isOpened():
success, frame = cap.read()
if success:
cv2.imshow('Output Video', frame)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
else:
break
cv2.destroyAllWindows()