dlib物品追踪

dlib物品追踪

dlib真是一个宝藏库,有很多实例可以让我们学着玩。
其中就有一个物体追踪功能,下面是官方给提供的例子

#!/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
#
#   Compiling dlib should work on any operating system so long as you have
#   CMake installed.  On Ubuntu, this can be done easily by running the
#   command:
#       sudo apt-get install cmake
#
#   Also note that this example requires Numpy which can be installed
#   via the command:
#       pip install numpy

import os
import glob

import dlib

# 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 = dlib.load_rgb_image(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()

当然我们还可以玩点追踪汽车的游戏
在这里插入图片描述
下面是源码:

# -*- coding: utf-8 -*-
"""
Created on Thu Jan 14 10:29:45 2021

@author: yuyanchuan
"""

import os
import glob

import dlib

import cv2

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
        
# Path to the video frames
src_path=r'F:\code\Pythoncode\dataset'
video_folder = os.path.join(src_path, "MVI_20011")

# Create the correlation tracker - the object needs to be initialized
# before it can be used
tracker = dlib.correlation_tracker()
 
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):
        # 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 = dlib.load_rgb_image(f)

            # We need to initialize the tracker on the first frame
            if k == 0:
                if start_flag == True:   # 如果是第一帧,需要先初始化
                    # 这里是初始化,窗口中会停在当前帧,用鼠标拖拽一个框来指定区域,随后会跟踪这个目标;我们需要先找到目标才能跟踪不是吗?
                    while True:
                        img_first = img.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(img, dlib.rectangle(track_window[0], track_window[1], track_window[2], track_window[3]))   # 跟踪目标,目标就是选定目标窗口中的
            else:
                tracker.update(img)  # 更新,实时跟踪
 
            box_predict = tracker.get_position()  # 得到目标的位置
            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)
            # 如果按下ESC键,就退出
            if cv2.waitKey(10) == 27:
                break
    cv2.destroyAllWindows()

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值