【技术分享】Python3 单目标跟踪 opencv + dlib实现 ( 第三课 )
【技术分享】Python3 单目标跟踪 opencv + dlib实现 ( 第三课 )
2022-07-22 / 0 评论 / 123 阅读 / 50 点赞

【技术分享】Python3 单目标跟踪 opencv + dlib实现 ( 第三课 )

发光的神
2022-07-22 / 0 评论 / 123 阅读 / 正在检测是否收录...

简介

dlib提供了dlib.correlation_tracker()类用于跟踪目标,效果一般般,没训练的模型好。
官方文档入口:http://dlib.net/python/index.html#dlib.correlation_tracker

完整源码(直接绘制完成后按下enter键即可跟踪)

import sys
import dlib
import cv2

tracker = dlib.correlation_tracker()   # 导入correlation_tracker()类
cap = cv2.VideoCapture(0)   # OpenCV打开摄像头
start_flag = True   # 标记,是否是第一帧,若在第一帧需要先初始化
selection = None   # 实时跟踪鼠标的跟踪区域
track_window = None   # 要检测的物体所在区域
drag_start = None   # 标记,是否开始拖动鼠标

# 鼠标点击事件回调函数
def onMouseClicked(event, x, y, flags, param):
    global selection, track_window, drag_start  # 定义全局变量
    if event == cv2.EVENT_LBUTTONDOWN:  # 鼠标左键按下
        drag_start = (x, y)
        track_window = None
    if drag_start:   # 是否开始拖动鼠标,记录鼠标位置
        xMin = min(x, drag_start[0])
        yMin = min(y, drag_start[1])
        xMax = max(x, drag_start[0])
        yMax = max(y, drag_start[1])
        selection = (xMin, yMin, xMax, yMax)
    if event == cv2.EVENT_LBUTTONUP:   # 鼠标左键松开
        drag_start = None
        track_window = selection
        selection = None

cv2.namedWindow("image", cv2.WINDOW_AUTOSIZE)
cv2.setMouseCallback("image", onMouseClicked)

# opencv的bgr格式图片转换成rgb格式
# b, g, r = cv2.split(frame)
# frame2 = cv2.merge([r, g, b])

while True:
    ret, frame = cap.read()   # 从摄像头读入1帧

    if start_flag == True:   # 如果是第一帧,需要先初始化
        # 这里是初始化,窗口中会停在当前帧,用鼠标拖拽一个框来指定区域,随后会跟踪这个目标;我们需要先找到目标才能跟踪不是吗?
        while True:
            img_first = frame.copy()   # 不改变原来的帧,拷贝一个新的出来
            if track_window:  # 跟踪目标的窗口画出来了,就实时标出来
                cv2.rectangle(img_first, (track_window[0], track_window[1]), (track_window[2], track_window[3]), (0,0,255), 1)
            elif selection:   # 跟踪目标的窗口随鼠标拖动实时显示
                cv2.rectangle(img_first, (selection[0], selection[1]), (selection[2], selection[3]), (0,0,255), 1)
            cv2.imshow("image", img_first)
            # 按下回车,退出循环
            if cv2.waitKey(5) == 13:
                break
        start_flag = False   # 初始化完毕,不再是第一帧了
        tracker.start_track(frame, dlib.rectangle(track_window[0], track_window[1], track_window[2], track_window[3]))   # 跟踪目标,目标就是选定目标窗口中的
    else:
        tracker.update(frame)  # 更新,实时跟踪

    box_predict = tracker.get_position()  # 得到目标的位置
    cv2.rectangle(frame,(int(box_predict.left()),int(box_predict.top())),(int(box_predict.right()),int(box_predict.bottom())),(0,255,255),1)  # 用矩形框标注出来
    cv2.imshow("image", frame)
    # 如果按下ESC键,就退出
    if cv2.waitKey(10) == 27:
        break
    
cap.release()
cv2.destroyAllWindows()

视频效果

官方示例

# The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
#
# This example shows how to use the correlation_tracker from the dlib Python
# library.  This object lets you track the position of an object as it moves
# from frame to frame in a video sequence.  To use it, you give the
# correlation_tracker the bounding box of the object you want to track in the
# current video frame.  Then it will identify the location of the object in
# subsequent frames.
#
# In this particular example, we are going to run on the
# video sequence that comes with dlib, which can be found in the
# examples/video_frames folder.  This video shows a juice box sitting on a table
# and someone is waving the camera around.  The task is to track the position of
# the juice box as the camera moves around.
#
#
# COMPILING/INSTALLING THE DLIB PYTHON INTERFACE
#   You can install dlib using the command:
#       pip install dlib
#
#   Alternatively, if you want to compile dlib yourself then go into the dlib
#   root folder and run:
#       python setup.py install
#   or
#       python setup.py install --yes USE_AVX_INSTRUCTIONS
#   if you have a CPU that supports AVX instructions, since this makes some
#   things run faster.  
#
#   Compiling dlib should work on any operating system so long as you have
#   CMake and boost-python installed.  On Ubuntu, this can be done easily by
#   running the command:
#       sudo apt-get install libboost-python-dev cmake
#
#   Also note that this example requires scikit-image which can be installed
#   via the command:
#       pip install scikit-image
#   Or downloaded from http://scikit-image.org/download.html. 

import os
import glob

import dlib
from skimage import io

# Path to the video frames
video_folder = os.path.join("..", "examples", "video_frames")

# Create the correlation tracker - the object needs to be initialized
# before it can be used
tracker = dlib.correlation_tracker()

win = dlib.image_window()
# We will track the frames as we load them off of disk
for k, f in enumerate(sorted(glob.glob(os.path.join(video_folder, "*.jpg")))):
    print("Processing Frame {}".format(k))
    img = io.imread(f)

    # We need to initialize the tracker on the first frame
    if k == 0:
        # Start a track on the juice box. If you look at the first frame you
        # will see that the juice box is contained within the bounding
        # box (74, 67, 112, 153).
        tracker.start_track(img, dlib.rectangle(74, 67, 112, 153))
    else:
        # Else we just attempt to track from the previous frame
        tracker.update(img)

    win.clear_overlay()
    win.set_image(img)
    win.add_overlay(tracker.get_position())
    dlib.hit_enter_to_continue()

50

评论 (0)

取消
0:00