pythonlessons/TensorFlow-2.x-YOLOv3

Object tracking does not work properly

soroushhashemifar opened this issue · 3 comments

I have ran object tracking on multiple videos recorded from streets, but the object tracker does not draw any bounding box on people and cars. The default configuration is used.

After a few investigation, I've found that the YOLO object does not predict bounding boxes properly while loading an image in a "while True" loop. In fact, in a "while True" I load a fixed image and predict the boxes by YOLO. But, it only predicts the first time it sees the image, and for next times it does not predict the same bounding boxes! This is so weird and I have no idea where I should search for the problem.

Hi, how I can reproduce your testing results?

import os
os.environ['CUDA_VISIBLE_DEVICES'] = ''
import cv2
import numpy as np
import tensorflow as tf
from yolov3.utils import Load_Yolo_model, image_preprocess, postprocess_boxes, nms, draw_bbox, read_class_names
from yolov3.configs import *
import time
from deep_sort import nn_matching
from deep_sort.detection import Detection
from deep_sort.tracker import Tracker
from deep_sort import generate_detections as gdet


os.environ['DISPLAY'] = ':0'

def Object_tracking(Yolo, video_path, output_path, input_size=416, show=False, CLASSES=YOLO_COCO_CLASSES, score_threshold=0.3, iou_threshold=0.45, rectangle_colors='', Track_only = []):

    while True:
        original_image = cv2.imread("./demo3.png")
        original_frame = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)
        original_frame = cv2.cvtColor(original_frame, cv2.COLOR_BGR2RGB)

        image_data = image_preprocess(np.copy(original_frame), [input_size, input_size])
        image_data = image_data[np.newaxis, ...].astype(np.float32)

        t1 = time.time()
        if YOLO_FRAMEWORK == "tf":
            pred_bbox = Yolo.predict(image_data)
        elif YOLO_FRAMEWORK == "trt":
            batched_input = tf.constant(image_data)
            result = Yolo(batched_input)
            pred_bbox = []
            for key, value in result.items():
                value = value.numpy()
                pred_bbox.append(value)
                
        pred_bbox = [tf.reshape(x, (-1, tf.shape(x)[-1])) for x in pred_bbox]
        pred_bbox = tf.concat(pred_bbox, axis=0)

        bboxes = postprocess_boxes(pred_bbox, original_frame, input_size, score_threshold)
        bboxes = nms(bboxes, iou_threshold, method='nms')

        image = draw_bbox(original_frame, bboxes, CLASSES=CLASSES, rectangle_colors=rectangle_colors)
        
        if show:
            cv2.imshow('output', image)
            
            if cv2.waitKey(25) & 0xFF == ord("q"):
                cv2.destroyAllWindows()
                break
            
    cv2.destroyAllWindows()


yolo = Load_Yolo_model()
Object_tracking(yolo, "./object_tracking_demo2.mp4", "detection.mp4", input_size=YOLO_INPUT_SIZE, show=True, iou_threshold=0.45, rectangle_colors=(255,0,0), Track_only=["person"])

here's a sample code I used. I figured out the problem appears when I use GPU. On CPU, it works fine, but slow.