0

can someone tell me which second parameter I should pass to tracker.init() after detection a pedestrian? there are only parameters with already selected ROI in the internet. I tried to pass a variable rects,but it gives me an error

import numpy as np
import cv2
import sys
from imutils.object_detection import non_max_suppression

cap = cv2.VideoCapture("video.mp4")
hog_descriptor = cv2.HOGDescriptor()
hog_descriptor.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())

tracker = cv2.TrackerKCF_create()
ret, frame = cap.read()

while (1):
    ret, frame = cap.read()
    (rects, weights) = hog_descriptor.detectMultiScale(frame, winStride=(16, 16),
                                                       padding=(8, 8), scale=1.05)




    rects = np.array([[x, y, x + w, y + h] for (x, y, w, h) in rects])
    pick = non_max_suppression(rects, probs=None, overlapThresh=0.65)
    for (xA, yA, xB, yB) in rects:
        cv2.rectangle(frame, (xA, yA), (xB, yB), (0, 255, 0), 2)
    ret = tracker.init(frame)

    ret, = tracker.update(frame)
cv2.imshow("video", frame)
if cv2.waitKey(1) == 27:
    break

cap.release()
cv2.destroyAllWindows()
AinurZhappass
  • 85
  • 1
  • 2
  • 7

1 Answers1

1

According to opencv sample code https://github.com/opencv/opencv_contrib/blob/master/modules/tracking/samples/tracker.py, second parameter of tracker.init is bounding box:

cv.namedWindow("tracking")
camera = cv.VideoCapture(sys.argv[1])
ok, image=camera.read()
...
bbox = cv.selectROI("tracking", image)
tracker = cv.TrackerMIL_create()
...
ok = tracker.init(image, bbox)

So, if you want to use detected pedestrian as ROI, you should use:

bbox = rects[index]
tracker.init(image, bbox)

where index is the number of detected pedestrian that you want to track

Aleksey Petrov
  • 370
  • 2
  • 15
  • okey, I checked it,but when I print bbox it gives me an array with 4 numbers (coordinates of bounding box), however applying to init it gives me an error "TypeError: function takes exactly 4 arguments (1 given)" ?? do you know where is a problem?? if (rects.size): for rect in range(len(rects)): bbox.append(rects[rect]) ret = tracker.init(frame, tuple(bbox)) – AinurZhappass Feb 21 '18 at 13:04
  • I think this error is somewhere else in your code, because it says that 1 argument is given, but you provide 2 arguments for tracker.init. I think you sholud make another question with code and error message. This question was about second parameter of the tracker.init function. – Aleksey Petrov Feb 21 '18 at 14:15