[Ubuntu] [Python] Object tracking using dlib

OpneCV's object tracking has moved the sample from time to time, I've never run dlib, so give it a try.

I also follow motion blur and it seems that the performance is quite good.

The video is below. https://www.youtube.com/watch?v=ORgMddcNHvU [[Ubuntu] [Python] Object tracking using dlib](https://www.youtube.com/watch?v = ORgMddcNHvU)

The source code is below.


#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
correlation_tracker.py.

Usage:
  correlation_tracker.py [<video source>] [<resize rate>]
'''


import sys
import dlib
import cv2
import time
import copy

#Mouse event handler
mouse_start_x, mouse_start_y = 0, 0
mouse_end_x, mouse_end_y = 0, 0
selecting = False
tracker_start_flag = False
tracking_flag = False
def select_roi(event,x,y,flags,param):
    global selecting, tracker_start_flag
    global mouse_start_x, mouse_start_y
    global mouse_end_x, mouse_end_y
    if event == cv2.EVENT_LBUTTONDOWN:
        selecting = True
        mouse_start_x, mouse_start_y = x,y

    elif event == cv2.EVENT_MOUSEMOVE:
        if selecting == True:
            mouse_end_x, mouse_end_y = x, y
        else:
            pass
    elif event == cv2.EVENT_LBUTTONUP:
        mouse_end_x, mouse_end_y = x, y
        selecting = False
        tracker_start_flag = True

#Argument interpretation
try:
    fn = sys.argv[1]
    if fn.isdigit() == True:
        fn = int(fn)
except:
    fn = 0
try:
    resize_rate = sys.argv[2]
    resize_rate = int(resize_rate)
except:
    resize_rate = 1

#Tracker generation
tracker = dlib.correlation_tracker()

video_input = cv2.VideoCapture(fn)
if (video_input.isOpened() == True):
    ret, frame = video_input.read()
    cv2.imshow('correlation tracker', frame)
    cv2.setMouseCallback('correlation tracker', select_roi)

while(video_input.isOpened() == True):
    ret, frame = video_input.read()
    temp_frame = copy.deepcopy(frame)

    #Target frame reduction to reduce processing load (when argument is specified)
    height, width = frame.shape[:2]
    temp_frame = cv2.resize(frame, (int(width/resize_rate), int(height/resize_rate)))

    if tracker_start_flag == True:
        #Start tracking
        tracker.start_track(temp_frame, dlib.rectangle(mouse_start_x, mouse_start_y, mouse_end_x, mouse_end_y))
        tracking_flag = True
        tracker_start_flag = False
    elif tracking_flag == True:
        #Tracking updates
        tracker.update(temp_frame)

    #drawing
    if selecting == True:
        cv2.rectangle(frame, (mouse_start_x, mouse_start_y), (mouse_end_x, mouse_end_y), (0, 0, 255), 2)
    if tracking_flag == True:
        tracking_point = tracker.get_position()
        tracking_point_x1 = int(tracking_point.left())
        tracking_point_y1 = int(tracking_point.top())
        tracking_point_x2 = int(tracking_point.right())
        tracking_point_y2 = int(tracking_point.bottom())
        cv2.rectangle(frame, (tracking_point_x1, tracking_point_y1), (tracking_point_x2, tracking_point_y2), (0, 0, 255), 2)

    cv2.imshow('correlation tracker', frame)

    c = cv2.waitKey(50) & 0xFF

    if c==27: # ESC
        break

that's all.

Recommended Posts

[Ubuntu] [Python] Object tracking using dlib
[Ubuntu] [Python] Facial organ detection using dlib
Introducing Python using pyenv on Ubuntu 20.04
Preparing python using vscode on ubuntu
Python built-in object
Start using Python
Python built-in object
Scraping using Python
Head orientation estimation using Python and OpenCV + dlib
Build Python3.5 + matplotlib environment on Ubuntu 12 using Anaconda
I tried object detection using Python and OpenCV
Python development on Ubuntu on AWS EC2 (using JupyterLab)
Operate Redmine using Python Redmine
Fibonacci sequence using Python
Data analysis using Python 0
Object tracking using OpenCV3 and Python3 (tracking feature points specified by the mouse using the Lucas-Kanade method)
Data cleaning using Python
[Ubuntu] [Python] Face detection comparison between dlib and OpenCV
Using Python #external packages
Object oriented in python
WiringPi-SPI communication using Python
Age calculation using python
Troublesome story when using Python3 with VScode on ubuntu
Search Twitter using Python
build Python on Ubuntu
Install Python 3.3 on Ubuntu 12.04
About Python3 ... (Ellipsis object)
Name identification using python
Notes using Python subprocesses
Beep in Python (Ubuntu)
Try using Tweepy [Python2.7]
Until building a Python development environment using pyenv on Ubuntu 20.04
Flatten using Python yield from
Using X11 with ubuntu18.04 (C)
Scraping using Python 3.5 async / await
Save images using python3 requests
Use matplotlib on Ubuntu 12 & Python
[S3] CRUD with S3 using Python [Python]
[Python] Try using Tkinter's canvas
String object methods in Python
Using Quaternion with Python ~ numpy-quaternion ~
[python] Value of function object (?)
Windows10: Install python dlib library
Null object comparison in Python
Try using Kubernetes Client -Python-
Python notes using perl-special variables
[Python] Using OpenCV with Python (Basic)
Scraping using Python 3.5 Async syntax
Website change monitoring using python
Face detection with Python + dlib
Post to Twitter using Python
Put Python 3.x on Ubuntu
Build python3 environment with ubuntu 16.04
Start to Selenium using python
Search algorithm using word2vec [python]
Change python version using pyenv
python: Basics of using scikit-learn ①
# 1 [python3] Simple calculation using variables
Create JIRA tickets using Python
Install OpenCV on Ubuntu + python
Instrument control using Python [pyvisa]