current position:Home>Python+opencv+dlib to achieve target tracking

Python+opencv+dlib to achieve target tracking

2022-07-25 15:10:26weixin_ forty-two million seven hundred and nine thousand five

Background introduction

Dlib Is a deep learning open source tool , be based on C++ Development , Also support Python Development interface , Function like TensorFlow And PyTorch, because dlib It has good support for facial features , There are many trained face feature extraction models for developers to use , therefore dlib It is very suitable for face project development . Concrete dlib The environment configuration method will not be repeated here , There are many related tutorials on the Internet for reference .

Target tracking

In application ,dlib In most cases, it is used for face detection and face recognition , However ,dlib It can also be used for object tracking , By calling Python API Medium dlib.correlation_tracker Class can implement a preliminary tracking result , It is divided into the following four steps :
(1) Use dlib.correlation_tracker() Create a trace class ;
(2) start_track() Set the box in the picture to track the target object ;
(3) update() Track the next frame in real time ;
(4) get_position() Get the location of the tracked target .

import cv2
import dlib
import glob
# Import face detector , Tracker and required pictures 
detector = dlib.get_frontal_face_detector()
tracker = dlib.correlation_tracker()
paths = sorted(glob.glob('video_frames/*.jpg')) #video_frames Is the folder where the current picture is located 
# Track the target object in the picture 
for i, path in enumerate(paths):
    img = cv2.imread(path)
    if i == 0:
	    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Convert to gray image processing 
	    dets = detector(gray, 1) #1 It means to double the size of the picture , Easy to detect more faces 
	    d=dets[0] # Suppose there is only one face in the picture 
	    tracker.start_track(img, dlib.rectangle(d.left(),d.top(),d.right(),d.bottom()))
	#  Subsequent frames , Automatic tracking 
	else:
		tracker.update(img)
	box_predict = tracker.get_position() # Get the location of the target 
	cv2.rectangle(img,(int(box_predict.left()),int(box_predict.top())),(int(box_predict.right()),int(box_predict.bottom())),(0,255,255),1)
    cv2.imshow('image',img)
	if cv2.waitKey(10) == 27:
	    break
cv2.destroyAllWindows()

After setting up the running code , We can achieve the tracking effect we want .

copyright notice
author[weixin_ forty-two million seven hundred and nine thousand five ],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/206/202207251506571195.html

Random recommended