python dlib学习(四):单目标跟踪

26 篇文章 21 订阅
24 篇文章 5 订阅

前言

dlib提供了dlib.correlation_tracker()类用于跟踪目标。
官方文档入口:http://dlib.net/python/index.html#dlib.correlation_tracker
不复杂,就不介绍了,后面会直接给出两个程序,有注释。

程序1

# -*- coding: utf-8 -*-
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

if __name__ == '__main__':
    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(1):
        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()

注:如果程序卡了,就调一下cv2.waitKey()中的参数,也就是延时时间,调小即可。

运行结果

初始时,窗口中只会显示第一帧的图像;
使用鼠标拖拽一个框,红框中目标后,按回车,设置框内为识别目标;
实时识别,以橙框标出;
按ESC键退出。
这里写图片描述
(csdn只能上传2M的图片,真心难受)

程序2

由于前面那个程序,只是熟悉下函数写的,我觉得用起来蛋疼,所以又重新封装了一下。看起来舒服多了。

# -*- coding: utf-8 -*-
import sys
import dlib
import cv2

class myCorrelationTracker(object):
    def __init__(self, windowName='default window', cameraNum=0):
        # 自定义几个状态标志
        self.STATUS_RUN_WITHOUT_TRACKER = 0     # 不跟踪目标,但是实时显示
        self.STATUS_RUN_WITH_TRACKER = 1    # 跟踪目标,实时显示
        self.STATUS_PAUSE = 2   # 暂停,卡在当前帧
        self.STATUS_BREAK = 3   # 退出
        self.status = self.STATUS_RUN_WITHOUT_TRACKER   # 指示状态的变量

        # 这几个跟前面程序1定义的变量一样
        self.track_window = None  # 实时跟踪鼠标的跟踪区域
        self.drag_start = None   # 要检测的物体所在区域
        self.start_flag = True   # 标记,是否开始拖动鼠标

        # 创建好显示窗口
        cv2.namedWindow(windowName, cv2.WINDOW_AUTOSIZE)
        cv2.setMouseCallback(windowName, self.onMouseClicked)
        self.windowName = windowName

        # 打开摄像头
        self.cap = cv2.VideoCapture(cameraNum)

        # correlation_tracker()类,跟踪器,跟程序1中一样
        self.tracker = dlib.correlation_tracker()

        # 当前帧
        self.frame = None

    # 按键处理函数
    def keyEventHandler(self):
        keyValue = cv2.waitKey(5)  # 每隔5ms读取一次按键的键值
        if keyValue == 27:  # ESC
            self.status = self.STATUS_BREAK
        if keyValue == 32:  # 空格
            if self.status != self.STATUS_PAUSE:    # 按下空格,暂停播放,可以选定跟踪的区域
                #print self.status
                self.status = self.STATUS_PAUSE
                #print self.status
            else:   # 再按次空格,重新播放,但是不进行目标识别
                if self.track_window:
                    self.status = self.STATUS_RUN_WITH_TRACKER
                    self.start_flag = True
                else:
                    self.status = self.STATUS_RUN_WITHOUT_TRACKER
        if keyValue == 13:  # 回车
            #print '**'
            if self.status == self.STATUS_PAUSE:    # 按下空格之后
                if self.track_window:   # 如果选定了区域,再按回车,表示确定选定区域为跟踪目标
                    self.status = self.STATUS_RUN_WITH_TRACKER
                    self.start_flag = True

    # 任务处理函数        
    def processHandler(self):
        # 不跟踪目标,但是实时显示
        if self.status == self.STATUS_RUN_WITHOUT_TRACKER:
            ret, self.frame = self.cap.read()
            cv2.imshow(self.windowName, self.frame)
        # 暂停,暂停时使用鼠标拖动红框,选择目标区域,与程序1类似
        elif self.status == self.STATUS_PAUSE:
            img_first = self.frame.copy()  # 不改变原来的帧,拷贝一个新的变量出来
            if self.track_window:   # 跟踪目标的窗口画出来了,就实时标出来
                cv2.rectangle(img_first, (self.track_window[0], self.track_window[1]), (self.track_window[2], self.track_window[3]), (0,0,255), 1)
            elif self.selection:   # 跟踪目标的窗口随鼠标拖动实时显示
                cv2.rectangle(img_first, (self.selection[0], self.selection[1]), (self.selection[2], self.selection[3]), (0,0,255), 1)
            cv2.imshow(self.windowName, img_first)
        # 退出
        elif self.status == self.STATUS_BREAK:
            self.cap.release()   # 释放摄像头
            cv2.destroyAllWindows()   # 释放窗口
            sys.exit()   # 退出程序
        # 跟踪目标,实时显示
        elif self.status == self.STATUS_RUN_WITH_TRACKER:
            ret, self.frame = self.cap.read()  # 从摄像头读取一帧
            if self.start_flag:   # 如果是第一帧,需要先初始化
                self.tracker.start_track(self.frame, dlib.rectangle(self.track_window[0], self.track_window[1], self.track_window[2], self.track_window[3]))  # 开始跟踪目标
                self.start_flag = False   # 不再是第一帧
            else:
                self.tracker.update(self.frame)   # 更新

                # 得到目标的位置,并显示
                box_predict = self.tracker.get_position()   
                cv2.rectangle(self.frame,(int(box_predict.left()),int(box_predict.top())),(int(box_predict.right()),int(box_predict.bottom())),(0,255,255),1)
                cv2.imshow(self.windowName, self.frame)

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

    def run(self):
        while(1):
            self.keyEventHandler()
            self.processHandler()


if __name__ == '__main__':
    testTracker = myCorrelationTracker(windowName='image', cameraNum=1)
    testTracker.run()

注:如果程序卡了,就调一下cv2.waitKey()中的参数,也就是延时时间,调小即可。

运行结果

操作有一些改变:
初始时,会自动从摄像头采集图像显示;
按下空格,暂停;此时若再按空格,恢复实时显示,但不进行目标跟踪;
暂停时,拖动鼠标会显示红框,按下回车,将红框内物体视为目标进行识别;
随后实时识别,以橙框标出;
按ESC键退出。
这里写图片描述

官方例程

#!/usr/bin/python
# 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()

吐槽:
已经写了四篇有关dlib的学习笔记了。dlib这个库的确很方便,能够很轻松地让我们实现一些基础的识别任务,比如人脸识别。但是如果想要在各种识别任务中有更好的效果,肯定是不能只用他给的模型的。那也就是说需要自己训练了,看到官方文档中也提供了一些训练以及自己构建神经网络等的api接口,下次有时间再来整理一下程序。最近学校又是校运会,又要看数学(矩阵论、凸优化),不过这样抽时间出来写写程序心情也舒畅了不少。
ヽ(・ω・。)ノ

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值