Reputation: 705
I am using an OpenCV tracker to perform face tracking in videos, together with a face detector every few frames. If a face is detected by the face detector, I would like to update the tracker with the "detected" bounding box. I see that there is an option to enter a Rect in the C++ implementation but for some reason not in the python implementation as written in the opencv documentation. This is also an option when using dlib's correlation_tracker.
Currently, I can only initialize the tracker with a bounding box, but not update it with one in Python. if my tracker has drifted off the initial face it was tracking, I can't "bring it back" even if I know where the face is now (using my face detector). Is there a way to do this in python (e.g. should I kill the current tracker and init another with detected bounding box)?
Upvotes: 9
Views: 7818
Reputation: 137
I was looking for the very same thing and I found myself the solution to the problem by re-creating tracker each time successful detection occur. Please check following code. If something is not clear, feel free to ask for details:
import cv2 as cv
cap = cv.VideoCapture(0)
face_front_cascade = cv.CascadeClassifier("haarcascade_frontalface_alt.xml")
tracker = cv.TrackerKCF_create()
bbox = ()
while True:
ret,frame = cap.read()
#press S to capture the face
if cv.waitKey(20) & 0xFF == ord("s"):
frame_gray = cv.cvtColor(frame,cv.COLOR_BGR2GRAY)
face = face_front_cascade.detectMultiScale(frame_gray, scaleFactor=1.5, minNeighbors=3)
for (x, y, w, h) in face:
colour = (0,0,255)
stroke = 20
cv.rectangle(frame,(x,y),(x+w,y+h), colour, stroke)
bbox = (x,y,w,h)
tracker = cv.TrackerKCF_create() #overwrite old tracker
#trace face and draw box around it
if bbox:
tracker.init(frame, bbox)
ret, bbox = tracker.update(frame)
if ret:
p1 = (int(bbox[0]), int(bbox[1]))
p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3]))
cv.rectangle(frame, p1, p2, (255, 0, 0), 2, 1)
#show result
cv.imshow("frame",frame)
#press ESC to exit
if cv.waitKey(20) & 0xFF ==27:
break
cap.release()
cv.destroyAllWindows()
Upvotes: 6