mikel-brostrom / boxmot

BoxMOT: pluggable SOTA tracking modules for segmentation, object detection and pose estimation models
GNU Affero General Public License v3.0
6.58k stars 1.7k forks source link

Bounding boxes mismatch #228

Closed prashant-py-debug closed 2 years ago

prashant-py-debug commented 2 years ago

hi , i am trying to use the deepsort with yolo5s to get the bboxes for downstream use like showing the trajectory of the object. what i am doing:

  1. using yolov5s to get the bboxes , classes and confidence score.
  2. converting bboxes xyxy format to xywh for deepsort.
  3. sending the bboxes, confs,classes,frame to the deepsort.
  4. using received tracker id and bbox to project the trajectory.

My code: ` import os os.environ["OMP_NUM_THREADS"] = "1" os.environ["OPENBLAS_NUM_THREADS"] = "1" os.environ["MKL_NUM_THREADS"] = "1" os.environ["VECLIB_MAXIMUM_THREADS"] = "1" os.environ["NUMEXPR_NUM_THREADS"] = "1"

import sys sys.path.insert(0, './yolov5')

from yolov5.models.experimental import attempt_load from yolov5.utils.downloads import attempt_download from yolov5.models.common import DetectMultiBackend from yolov5.utils.datasets import LoadImages, LoadStreams from yolov5.utils.general import LOGGER, check_img_size, non_max_suppression, scale_coords, check_imshow, xyxy2xywh from yolov5.utils.torch_utils import select_device, time_sync from yolov5.utils.plots import Annotator, colors from deep_sort_pytorch.utils.parser import get_config from deep_sort_pytorch.deep_sort import DeepSort import argparse import os import platform import shutil import time from pathlib import Path import cv2 import torch import torch.backends.cudnn as cudnn import numpy as np

path = "road.mp4" deep_sort_weights = 'deep_sort_pytorch/deep_sort/deep/checkpoint/ckpt.t7' config_deepsort="deep_sort_pytorch/configs/deep_sort.yaml" font = cv2.FONT_HERSHEY_SIMPLEX

names = ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush']

initialize deepsort

cfg = get_config() cfg.merge_from_file(config_deepsort) attempt_download(deep_sort_weights, repo='mikel-brostrom/Yolov5_DeepSort_Pytorch') deepsort = DeepSort(cfg.DEEPSORT.REID_CKPT, max_dist=cfg.DEEPSORT.MAX_DIST, min_confidence=cfg.DEEPSORT.MIN_CONFIDENCE, max_iou_distance=cfg.DEEPSORT.MAX_IOU_DISTANCE, max_age=cfg.DEEPSORT.MAX_AGE, n_init=cfg.DEEPSORT.N_INIT, nn_budget=cfg.DEEPSORT.NN_BUDGET, use_cuda=True)

model = torch.hub.load('ultralytics/yolov5', 'yolov5s')

def x1y1x2y2_to_xywh(x1y1x2y2): xywhs = torch.zeros_like(x1y1x2y2) for i,xyxy in enumerate(x1y1x2y2): x1,y1,x2,y2 = xyxy[0],xyxy[1],xyxy[2],xyxy[3] w = x2 - x1 h = y2 - y1 xywhs[i,0] = x1 xywhs[i,1] = y1 xywhs[i,2] = w xywhs[i,3] = h return xywhs

cap = cv2.VideoCapture(path) while True:

success, frame = cap.read()
frame = cv2.resize(frame,(640,640), interpolation = cv2.INTER_AREA)
if not success:
    break

results = model(frame)

det = results.xyxy[0]
if det is not None and len(det):
    x1y1x2y2 = det[:,0:4]
    print(x1y1x2y2)
    xywhs = x1y1x2y2_to_xywh(x1y1x2y2)
    print(xywhs)

    confs = det[:,4]
    clss = det[:,5]
    print(clss)
    outputs = deepsort.update(xywhs, confs, clss, frame)
    print(outputs)
    #draw boxes for visulization

    if len(outputs) > 0:
        for j, (output, conf) in enumerate(zip(outputs, confs)): 

            bboxes = output[0:4]
            x1,y1,x2,y2 = bboxes[0],bboxes[1],bboxes[2],bboxes[3]
            id = output[4]
            cls = output[5]
            c = int(cls)  # integer class
            label = f'{id} {names[c]} {conf:.2f}'
            print("bboxes:",bboxes ,"tracked id:", id ,"class:",c ,"labels:",label)

            frame = cv2.rectangle(frame, (x1,y1),(x2,y2),(0,255,0),2)
            frame = cv2.putText(frame,label,(bboxes[0]-1,bboxes[1]-1),font,0.5,(255,0,255),1)
else:
     deepsort.increment_ages()

cv2.imshow("Object detection",frame)
cv2.waitKey(1)

Screenshot (57)

`

prashant-py-debug commented 2 years ago

I have solved the above issue. but after running the video for a while i am receiving this error message. outputs = deepsort.update(xywhs, confs, clss, frame) File "C:\Third_eye_approach3_yolov5\tracking\Yolov5_DeepSort_Pytorch\deep_sort_pytorch\deep_sort\deep_sort.py", line 39, in update self.tracker.update(detections, classes) File "C:\Third_eye_approach3_yolov5\tracking\Yolov5_DeepSort_Pytorch\deep_sort_pytorch\deep_sort\sort\tracker.py", line 78, in update self.tracks[track_idx].update( File "C:\Third_eye_approach3_yolov5\tracking\Yolov5_DeepSort_Pytorch\deep_sort_pytorch\deep_sort\sort\track.py", line 155, in update self.mean, self.covariance = kf.update( File "C:\Third_eye_approach3_yolov5\tracking\Yolov5_DeepSort_Pytorch\deep_sort_pytorch\deep_sort\sort\kalman_filter.py", line 174, in update chol_factor, lower = scipy.linalg.cho_factor( File "C:\Users\prash\AppData\Local\Programs\Python\Python39\lib\site-packages\scipy\linalg\decomp_cholesky.py", line 152, in cho_factor c, lower = _cholesky(a, lower=lower, overwrite_a=overwrite_a, clean=False, File "C:\Users\prash\AppData\Local\Programs\Python\Python39\lib\site-packages\scipy\linalg\decomp_cholesky.py", line 37, in _cholesky raise LinAlgError("%d-th leading minor of the array is not positive " numpy.linalg.LinAlgError: 2-th leading minor of the array is not positive definite