I am using cv2 with yolo as well as paddle ocr to recognize license plates directly from the camera, however it is extremely slow and laggy, I wish it could be smoother, although it cannot be achieved in real time, but at least it only delays at an acceptable level (less than 1s). I have read the solutions in stack overflow, opencv forum but have not found a suitable answer, please help me, because if I cannot complete this project, the company will not let me step into AI projects Later, this was considered for me to prove that I had the ability to participate in AI in the future. Here is the code I use:
`def tracking_camera(source, model= YOLO(“./model/best.pt”)):
track_history = defaultdict(lambda: [])
# lấy thông tin về source
w, h, fps = (int(source.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
out = cv2.VideoWriter('output_test_8.avi', cv2.VideoWriter_fourcc(*'MJPG'), fps, (w, h))
# Loop through the video frames
while source.isOpened():
# Read a frame from the video
success, frame = source.read()
if success:
# Run YOLOv8 tracking on the frame, persisting tracks between frames
results = model.track(frame, persist=True)
#gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Visualize the results on the frame
annotated_frame = results[0].plot(labels= False, boxes=False)
if results[0].boxes.id != None:
boxes = results[0].boxes.xywh.cpu().numpy().astype(int)
bboxes = results[0].boxes.xyxy.cpu().numpy().astype(int)
track_ids = results[0].boxes.id.cpu().numpy().astype(int)
confidences = results[0].boxes.conf.cpu().numpy().astype(int)
# Plot the tracks
for box, track_id, bbox in zip(boxes, track_ids, bboxes):
x, y, w, h = box
x1, y1, x2, y2= bbox
track = track_history[track_id]
track.append((float(x), float(y))) # x, y center point
if len(track) > 30: # retain 90 tracks for 90 frames
track.pop(0)
#get license plate
gray_frame= frame[int(y1):int(y2), int(x1):int(x2)]
gray_frame = cv2.cvtColor(gray_frame, cv2.COLOR_BGR2GRAY)
_,_, text= get_license_plate(gray_frame)
auto_save_and_update_excel(text)
# Draw the tracking lines
points = np.hstack(track).astype(np.int32).reshape((-1, 1, 2))
cv2.polylines(annotated_frame, [points], isClosed=False, color=(51,51,102), thickness=4)
# Draw bounding box without displaying confidence
annotated_frame= check_license_plate_and_display_information(text,annotated_frame)
cv2.rectangle(annotated_frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
cv2.putText(annotated_frame, f'#{track_id} {text}', (x1, y1), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 255, 0), 4)
cv2.rectangle(annotated_frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
# Display the annotated frame
out.write(annotated_frame)
cv2.imshow("YOLOv8 Tracking", 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
out.release()`