vukasin-stanojevic / BoostTrack

MIT License
65 stars 5 forks source link

when i am trying to plot the bounding boxes using tlwhs they are not being plotted that the right places #4

Closed mrigankEvig closed 1 month ago

mrigankEvig commented 2 months ago

This is the code function i am using to plot bounding boxes

`def draw_bounding_boxes(image, online_tlwhs, online_ids, online_conf,scale): height, width = image.shape[:2]

for tlwh, tid, conf in zip(online_tlwhs, online_ids, online_conf):
    t, l, w, h = tlwh  # Extract tlwh
    print(t,l,w,h)

    # for normalisation 
    # t, l, w, h = t * width, l * height, w * width, h * height

    # Convert to integer
    t, l, w, h = int(t), int(l), int(w), int(h)

    # Print for debugging
    print(f'Bounding Box - t: {t}, l: {l}, w: {w}, h: {h}')

    # Ensure coordinates are within the image dimensions
    t = max(0, min(t, width - 1))
    l = max(0, min(l, height - 1))
    w = max(0, min(w, width - t))
    h = max(0, min(h, height - l))

    top_left = (t, l)
    bottom_right = (t + w, l + h)
    color = (0, 255, 0)  # Green color for bounding boxes
    thickness = 2  # Thickness of the bounding box

    # Draw the bounding box
    cv2.rectangle(image, top_left, bottom_right, color, thickness)

    # Put the ID and confidence score on the bounding box
    label = f'ID: {tid}, Conf: {conf:.2f}'
    cv2.putText(image, label, (t, l - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)

return image`

'det' i pass to tracker.update are normalized targets = tracker.update(det, img, frame[0], tag)

vukasin-stanojevic commented 1 month ago

Hello.
If you are having problems with rectangle drawing it must be either the wrong bounding box format that you are using or you made some mistake with the scaling. If you scale the detections as done in line 192 of boost_track.py file, the results which you get do not require further scaling. Here is an example of method which can be added to KalmanBoxTracker class which draws the bounding box:

    def draw_bbox(self, img: np.ndarray) -> np.ndarray:
        box = self.get_state().reshape((4, ))
        x0 = int(box[0])
        y0 = int(box[1])
        x1 = int(box[2])
        y1 = int(box[3])
        #
        text = "{}".format(self.id)
        font = cv2.FONT_HERSHEY_SIMPLEX
        txt_size = cv2.getTextSize(text, font, 0.4, 1)[0]
        cv2.rectangle(img, (x0, y0), (x1, y1), self.color, 2)
        txt_bk_color = (np.array(self.color) * 0.7).astype(np.uint8).tolist()
        cv2.rectangle(
            img,
            (x0, y0 + 1),
            (x0 + txt_size[0] + 1, y0 + int(1.5 * txt_size[1])),
            txt_bk_color,
            -1,
        )
        cv2.putText(
            img, text, (x0, y0 + txt_size[1]), font, 0.4, self.color, thickness=1
        )
        return img

The img parameter used is img_numpy (see the definition of update method in line 173: def update(self, dets, img_tensor, img_numpy, tag):) @mrigankEvig