cap = cv2.VideoCapture("../Videos/3 way traffic.mp4") # For Videomodel = YOLO("../Yolo-Weights/yolov8l.pt")classNames = ["person", "bicycle", "car", "motorbike", "aeroplane", "bus", "train", "truck"]mask = cv2.imread("mask.png")tracker = Sort(max_age=20, min_hits=3, iou_threshold=0.3) # creating instancetrajectories = {}p=0q=1while True: success, img = cap.read() if not success: break imgRegion = cv2.bitwise_and(img, mask) results = model(imgRegion, stream=True) detections = np.empty((0, 5)) for r in results: boxes = r.boxes for box in boxes: x1, y1, x2, y2 = box.xyxy[0] x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2) w, h = x2 - x1, y2 - y1 conf: float = math.ceil((box.conf[0] * 100)) / 100 cls = int(box.cls[0]) currentClass: Union[str, Any] = classNames[cls] if currentClass == "car" or currentClass == "truck" or currentClass == "bus" or currentClass == "motorbike" and conf > 0.3: currentArray = np.array([x1, y1, x2, y2, conf]) detections = np.vstack((detections, currentArray)) cvzone.putTextRect(img, f' {currentClass}', (x1 + 10, y1 + 20), colorT=(255, 255, 255), colorR=(60, 20, 220), font=cv2.FONT_HERSHEY_TRIPLEX, scale=0.75, thickness=1, offset=2) resultsTracker = tracker.update(detections) for result in resultsTracker: x1, y1, x2, y2, id = result x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2) w, h = x2 - x1, y2 - y1 cvzone.cornerRect(img, (x1, y1, w, h), l=17, t=2, rt=1, colorR=(255, 228, 181), colorC=(60, 20, 220)) cx, cy = x1 + w // 2, y1 + h // 2 cv2.circle(img, (cx, cy), 5, (255, 0, 255), cv2.FILLED) print("cx,cy :", cx,cy) print("id:", id) if id in trajectories: trajectories[id].extend([cx, cy]) else: trajectories.update({id: [cx,cy]}) print("Trajectories:", trajectories) key_number = list(trajectories.keys()) length_dictionary = len(trajectories[id]) if length_dictionary>=4: first_coordinate = trajectories[id][p] second_coordinate = trajectories[id][q] third_coordinate = trajectories[id][p + 2] fourth_coordinate = trajectories[id][q + 2] print("First coordinate:", first_coordinate) print("Second coordinate:", second_coordinate) print("Third coordinate:", third_coordinate) print("Fourth coordinate:", fourth_coordinate) p += 2 q += 2 else: pass cv2.imshow("Image", img) cv2.waitKey(0)
The code above is for drawing the trajectory lines of vehicle paths. The lines are supposed to stay there for every vehicle that has passed from the video till the end.
I was adding the coordinates of the center of each vehicle detected into a dictionary. So that I'll create lines from the coordinates of previous center points to the current center points, by taking the last four points of the value of trajectories. The trajectories dictionary of the second frame of video looks like:
0: 384x640 4 cars, 1 truck, 299.2msSpeed: 15.6ms preprocess, 299.2ms inference, 0.0ms postprocess per image at shape (1, 3, 384, 640)cx,cy : 371 132id: 5.0Trajectories: {5.0: [372, 130, 371, 132], 4.0: [541, 140], 3.0: [309, 120], 2.0: [398, 348], 1.0: [249, 345]}cx,cy : 542 141id: 4.0Trajectories: {5.0: [372, 130, 371, 132], 4.0: [541, 140, 542, 141], 3.0: [309, 120], 2.0: [398, 348], 1.0: [249, 345]}cx,cy : 308 120id: 3.0Trajectories: {5.0: [372, 130, 371, 132], 4.0: [541, 140, 542, 141], 3.0: [309, 120, 308, 120], 2.0: [398, 348], 1.0: [249, 345]}cx,cy : 394 358id: 2.0Trajectories: {5.0: [372, 130, 371, 132], 4.0: [541, 140, 542, 141], 3.0: [309, 120, 308, 120], 2.0: [398, 348, 394, 358], 1.0: [249, 345]}cx,cy : 244 352id: 1.0Trajectories: {5.0: [372, 130, 371, 132], 4.0: [541, 140, 542, 141], 3.0: [309, 120, 308, 120], 2.0: [398, 348, 394, 358], 1.0: [249, 345, 244, 352]}
Problem starts when I try to retrive the co ordinates. It only gives the co ordinates of first id and from the second id it shows following error:
third_coordinate = trajectories[id][p + 2]
IndexError: list index out of range
I have tried putting p+=2 after the else statement like below:
else: pass p += 2 q += 2 cv2.imshow("Image", img) cv2.waitKey(0)
But the same error appears. The only thing that resolves the error is by removing the p & q incremental lines but then it is only picking the first 4 values instead of the last.