Spaces:
Runtime error
Runtime error
Commit
·
816ef5d
1
Parent(s):
d79bac4
process video
Browse files
camera.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
import numpy as np
|
| 3 |
+
import cv2
|
| 4 |
+
from norfair import Detection, Tracker
|
| 5 |
+
from detector.utils import detect_plates, detect_chars, imcrop, draw_text
|
| 6 |
+
from threading import Thread
|
| 7 |
+
|
| 8 |
+
DISTANCE_THRESHOLD_BBOX: float = 0.7
|
| 9 |
+
DISTANCE_THRESHOLD_CENTROID: int = 50
|
| 10 |
+
MAX_DISTANCE: int = 10000
|
| 11 |
+
HIT_COUNTER_MAX: int = 30
|
| 12 |
+
|
| 13 |
+
# SOURCE = 'rtsp://admin:PRE001@10.0.0.194:554/profile1'
|
| 14 |
+
# SOURCE = './costarica/videos/video4.mp4'
|
| 15 |
+
SOURCE = '/home/prevantec/Downloads/video.mp4'
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def yolo_to_norfair(yolo_detections):
|
| 19 |
+
norfair_detections = []
|
| 20 |
+
detections_as_xyxy = yolo_detections.xyxy[0]
|
| 21 |
+
for detection_as_xyxy in detections_as_xyxy:
|
| 22 |
+
bbox = np.array(
|
| 23 |
+
[
|
| 24 |
+
[detection_as_xyxy[0].item(), detection_as_xyxy[1].item()],
|
| 25 |
+
[detection_as_xyxy[2].item(), detection_as_xyxy[3].item()],
|
| 26 |
+
]
|
| 27 |
+
)
|
| 28 |
+
scores = np.array(
|
| 29 |
+
[detection_as_xyxy[4].item(), detection_as_xyxy[4].item()]
|
| 30 |
+
)
|
| 31 |
+
norfair_detections.append(
|
| 32 |
+
Detection(
|
| 33 |
+
points=bbox, scores=scores, label=int(detection_as_xyxy[-1].item())
|
| 34 |
+
)
|
| 35 |
+
)
|
| 36 |
+
return norfair_detections
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def cut_top_bottom(image, top=0.1, bottom=0):
|
| 40 |
+
height = image.shape[0]
|
| 41 |
+
return image[int(height * top):int(height * (1 - bottom)), :]
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def run(source=):
|
| 45 |
+
tracker = Tracker(
|
| 46 |
+
distance_function="iou_opt",
|
| 47 |
+
distance_threshold=DISTANCE_THRESHOLD_BBOX,
|
| 48 |
+
hit_counter_max=HIT_COUNTER_MAX,
|
| 49 |
+
)
|
| 50 |
+
cap = cv2.VideoCapture(source)
|
| 51 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 52 |
+
image_size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
|
| 53 |
+
plates = {}
|
| 54 |
+
final_video = cv2.VideoWriter('output.mp4', cv2.VideoWriter_fourcc(*'h264'), fps, image_size)
|
| 55 |
+
while cap.isOpened():
|
| 56 |
+
try:
|
| 57 |
+
ret, frame = cap.read()
|
| 58 |
+
if not ret:
|
| 59 |
+
break
|
| 60 |
+
# frame = cut_top_bottom(frame)
|
| 61 |
+
except Exception as e:
|
| 62 |
+
print(e)
|
| 63 |
+
continue
|
| 64 |
+
yolo_detections = detect_plates(frame)
|
| 65 |
+
detections = yolo_to_norfair(yolo_detections)
|
| 66 |
+
tracked_objects = tracker.update(detections=detections)
|
| 67 |
+
for obj in tracked_objects:
|
| 68 |
+
if obj.last_detection is not None and obj.age > 10:
|
| 69 |
+
bbox = obj.last_detection.points
|
| 70 |
+
bbox = int(bbox[0][0]), int(bbox[0][1]), int(bbox[1][0]), int(bbox[1][1])
|
| 71 |
+
if obj.id not in plates.keys():
|
| 72 |
+
crop = imcrop(frame, bbox)
|
| 73 |
+
text = detect_chars(crop)
|
| 74 |
+
plates[obj.id] = text
|
| 75 |
+
# thread = Thread(target=send_request, args=(frame_copy, text, bbox))
|
| 76 |
+
# thread.start()
|
| 77 |
+
|
| 78 |
+
cv2.rectangle(
|
| 79 |
+
frame,
|
| 80 |
+
(bbox[0], bbox[1]),
|
| 81 |
+
(bbox[2], bbox[3]),
|
| 82 |
+
(0, 255, 0),
|
| 83 |
+
2,
|
| 84 |
+
)
|
| 85 |
+
draw_text(frame, plates[obj.id], (bbox[0], bbox[1]))
|
| 86 |
+
cv2.putText(
|
| 87 |
+
frame,
|
| 88 |
+
plates[obj.id],
|
| 89 |
+
(bbox[0], bbox[1]),
|
| 90 |
+
cv2.FONT_HERSHEY_SIMPLEX,
|
| 91 |
+
1,
|
| 92 |
+
(0, 255, 0),
|
| 93 |
+
2,
|
| 94 |
+
)
|
| 95 |
+
cv2.imshow("frame", frame)
|
| 96 |
+
final_video.write(frame)
|
| 97 |
+
if cv2.waitKey(1) & 0xFF == ord("q"):
|
| 98 |
+
break
|
| 99 |
+
cap.release()
|
| 100 |
+
final_video.release()
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
def parse_opt():
|
| 104 |
+
parser = argparse.ArgumentParser()
|
| 105 |
+
opt = parser.parse_args()
|
| 106 |
+
return opt
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
def main(opt):
|
| 110 |
+
run(**vars(opt))
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
if __name__ == "__main__":
|
| 114 |
+
opt = parse_opt()
|
| 115 |
+
main(opt)
|