-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample_video.py
94 lines (69 loc) · 2.53 KB
/
sample_video.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import argparse
import numpy as np
import cv2
from PIL import Image, ImageFont, ImageDraw
from platerec import Platerec
def cv2_to_pil(cv2_img):
return Image.fromarray(cv2.cvtColor(cv2_img, cv2.COLOR_BGR2RGB))
def pil_to_cv2(pil_img):
return cv2.cvtColor(np.array(pil_img), cv2.COLOR_RGB2BGR)
def confidence_to_color(confidence):
r = int(255 * (1 - confidence))
g = int(255 * confidence)
return (r, g, 0)
def annotate_images(image, output, font_size):
boxes = output["boxes"]
confidences = output["confidences"]
words = output["words"]
words_confidences = output["words_confidences"]
draw = ImageDraw.Draw(image)
try:
font = ImageFont.truetype("arial.ttf", font_size)
except IOError:
font = ImageFont.load_default()
for i, box in enumerate(boxes):
x1, y1, x2, y2 = box
confidence = confidences[i]
word_confidence = words_confidences[i]
word = words[i]
box_color = confidence_to_color(confidence)
text_color = confidence_to_color(word_confidence)
draw.rectangle([x1, y1, x2, y2], outline=box_color, width=2)
draw.text((x1, y1 - font_size), f"{confidence:.2f}", fill=box_color, font=font)
draw.text((x1, y2), word, fill=text_color, font=font)
draw.text(
(x1, y2 + font_size), f"{word_confidence:.2f}", fill=text_color, font=font
)
return image
def main(video_path, font_size):
platerec = Platerec(providers=["CUDAExecutionProvider"])
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
print("Error: Could not open video.")
exit()
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
pil_image = cv2_to_pil(frame)
output = platerec.detect_read(pil_image)
if len(output["boxes"]) == 0:
annotated_image = pil_image
else:
annotated_image = annotate_images(pil_image, output, font_size)
frame = pil_to_cv2(annotated_image)
cv2.imshow("Frame", frame)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Annotate video frames with detected plates."
)
parser.add_argument("video_path", help="Path to the video file.")
parser.add_argument(
"--font_size", type=int, default=20, help="Font size for annotations."
)
args = parser.parse_args()
main(args.video_path, args.font_size)