0

I am using OpenCv and Trained model and trying assign IDs to the cars with the help of Centroid. There are the following scenarios:

  1. at the beginning all recognized cars get centroid and ID

  2. the cars that leave the frame, their IDs should be removed

  3. new cars that enter into the frame (later) should get new IDs

I have the ID code from a youtube Video , but it dosen't work as it should. the Remove method , removes everything.

If I leave the update method on, the Cars get new IDs every frame. that should not happen.
I am new to the programming and I would appreciate if someone could help me here out.

import cv2
import numpy as np
import pandas as pd
import math

video_name = "videos.mp4"
cap = cv2.VideoCapture(video_name)
net = cv2.dnn.readNetFromONNX("best.onnx")
classes = ['car', 'free_space']

count = 0
center_points_prev_frame = []
tracking_objects = {}
track_id = 0

while True:
    ret, img = cap.read()
    # ret, frame1 = cap.read()
    count += 1
    if img is None:
        break
    img = cv2.resize(img, (1500, 1000))
    # frame1 = cv2.resize(frame1, (1500, 1000))

    blob = cv2.dnn.blobFromImage(img, scalefactor=1 / 255,
                                 size=(640, 640),
                                 mean=[0, 0, 0, 0],
                                 swapRB=True,
                                 crop=False)
    net.setInput(blob)
    detections = net.forward()[0]
    # print(detections.shape)

    # Aufbau: cx, cy, w, h, confidence, class_score
    classes_ids = []
    confidences = []
    boxes = []
    rows = detections.shape[0]

    img_width, img_height = img.shape[1], img.shape[0]
    x_scale = img_width / 640
    y_scale = img_height / 640

    # apply Non-Maximum Suppression
    for i in range(rows):
        row = detections[i]
        confidence = row[4]
        if confidence > 0.3:
            classes_score = row[5:]
            ind = np.argmax(classes_score)
            if classes_score[ind] > 0.3:
                classes_ids.append(ind)
                confidences.append(confidence)
                cx, cy, w, h = row[:4]
                x1 = int((cx - w / 2) * x_scale)
                y1 = int((cy - h / 2) * y_scale)
                # print("X1:",x1 ,"Y1",y1)
                width = int(w * x_scale)
                height = int(h * y_scale)
                box = np.array([x1, y1, width, height])
                boxes.append(box)

    indices = cv2.dnn.NMSBoxes(boxes, confidences, 0.3, 0.3)

    # Point current frame
    center_points_cur_frame = []

    for i in indices:
        x1, y1, w, h = boxes[i]
        label = classes[classes_ids[i]]
        conf = confidences[i]
        text = label + "{:.2f}".format(conf)


        if label == 'car':
            car_coordinates = [(x1, y1), (x1 + w, y1 + h)]
            #cv2.rectangle(img, (x1, y1), (x1 + w, y1 + h), (51, 51, 255), 2)
            # center points
            cx = int((x1 + x1 + w) / 2)
            cy = int((y1 + y1 + h) / 2)
            cv2.circle(img, (cx,cy), 3, (255, 0, 255), -1)
            cv2.putText(img, str(track_id), (cx,cy), cv2.FONT_HERSHEY_COMPLEX, 0.3, (255, 0, 255), 1)
            center_points_cur_frame.append((cx, cy))



        # Only at the beginning we compare previous and current frame
        if count <= 2:
            for pt in center_points_cur_frame:
                for pt2 in center_points_prev_frame:
                    distance = math.hypot(pt2[0] - pt[0], pt2[1] - pt[1])

                    if distance < 20:
                        tracking_objects[track_id] = pt
                        track_id += 1
        else:

            tracking_objects_copy = tracking_objects.copy()
            center_points_cur_frame_copy = center_points_cur_frame.copy()

            for object_id, pt2 in tracking_objects_copy.items():
                object_exists = False
                for pt in center_points_cur_frame_copy:
                    distance = math.hypot(pt2[0] - pt[0], pt2[1] - pt[1])

                    # Update IDs position
                    if distance < 20:
                        tracking_objects[object_id] = pt
                        object_exists = True
                        if pt in center_points_cur_frame:
                            center_points_cur_frame.remove(pt)
                        continue
            ############################### Problem ##########################################
                # Remove IDs lost
                if not object_exists:
                    tracking_objects.pop(object_id)

            # Add new IDs found
            for pt in center_points_cur_frame:
                tracking_objects[track_id] = pt
                track_id += 1
            ############################### Problem ##########################################

        for object_id, pt in tracking_objects.items():
            cv2.circle(img, pt, 3, (255, 0, 255), -1)
            cv2.putText(img, str(object_id), (pt[0], pt[1] - 2), cv2.FONT_HERSHEY_COMPLEX, 0.3, (255, 0, 255), 1)

        print("Tracking objects")
        print(tracking_objects)

        print("CUR FRAME LEFT PTS")
        print(center_points_cur_frame)

        # Make a copy of the points
        center_points_prev_frame = center_points_cur_frame.copy()

    cv2.imshow("Video", img)
    cv2.waitKey(1)




# After the loop release the cap object
cap.release()
# Destroy all the windows
cv2.destroyAllWindows()
invisible
  • 3
  • 2

0 Answers0