Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/samples/python/camshift.py
16337 views
1
#!/usr/bin/env python
2
3
'''
4
Camshift tracker
5
================
6
7
This is a demo that shows mean-shift based tracking
8
You select a color objects such as your face and it tracks it.
9
This reads from video camera (0 by default, or the camera number the user enters)
10
11
http://www.robinhewitt.com/research/track/camshift.html
12
13
Usage:
14
------
15
camshift.py [<video source>]
16
17
To initialize tracking, select the object with mouse
18
19
Keys:
20
-----
21
ESC - exit
22
b - toggle back-projected probability visualization
23
'''
24
25
# Python 2/3 compatibility
26
from __future__ import print_function
27
import sys
28
PY3 = sys.version_info[0] == 3
29
30
if PY3:
31
xrange = range
32
33
import numpy as np
34
import cv2 as cv
35
36
# local module
37
import video
38
from video import presets
39
40
41
class App(object):
42
def __init__(self, video_src):
43
self.cam = video.create_capture(video_src, presets['cube'])
44
_ret, self.frame = self.cam.read()
45
cv.namedWindow('camshift')
46
cv.setMouseCallback('camshift', self.onmouse)
47
48
self.selection = None
49
self.drag_start = None
50
self.show_backproj = False
51
self.track_window = None
52
53
def onmouse(self, event, x, y, flags, param):
54
if event == cv.EVENT_LBUTTONDOWN:
55
self.drag_start = (x, y)
56
self.track_window = None
57
if self.drag_start:
58
xmin = min(x, self.drag_start[0])
59
ymin = min(y, self.drag_start[1])
60
xmax = max(x, self.drag_start[0])
61
ymax = max(y, self.drag_start[1])
62
self.selection = (xmin, ymin, xmax, ymax)
63
if event == cv.EVENT_LBUTTONUP:
64
self.drag_start = None
65
self.track_window = (xmin, ymin, xmax - xmin, ymax - ymin)
66
67
def show_hist(self):
68
bin_count = self.hist.shape[0]
69
bin_w = 24
70
img = np.zeros((256, bin_count*bin_w, 3), np.uint8)
71
for i in xrange(bin_count):
72
h = int(self.hist[i])
73
cv.rectangle(img, (i*bin_w+2, 255), ((i+1)*bin_w-2, 255-h), (int(180.0*i/bin_count), 255, 255), -1)
74
img = cv.cvtColor(img, cv.COLOR_HSV2BGR)
75
cv.imshow('hist', img)
76
77
def run(self):
78
while True:
79
_ret, self.frame = self.cam.read()
80
vis = self.frame.copy()
81
hsv = cv.cvtColor(self.frame, cv.COLOR_BGR2HSV)
82
mask = cv.inRange(hsv, np.array((0., 60., 32.)), np.array((180., 255., 255.)))
83
84
if self.selection:
85
x0, y0, x1, y1 = self.selection
86
hsv_roi = hsv[y0:y1, x0:x1]
87
mask_roi = mask[y0:y1, x0:x1]
88
hist = cv.calcHist( [hsv_roi], [0], mask_roi, [16], [0, 180] )
89
cv.normalize(hist, hist, 0, 255, cv.NORM_MINMAX)
90
self.hist = hist.reshape(-1)
91
self.show_hist()
92
93
vis_roi = vis[y0:y1, x0:x1]
94
cv.bitwise_not(vis_roi, vis_roi)
95
vis[mask == 0] = 0
96
97
if self.track_window and self.track_window[2] > 0 and self.track_window[3] > 0:
98
self.selection = None
99
prob = cv.calcBackProject([hsv], [0], self.hist, [0, 180], 1)
100
prob &= mask
101
term_crit = ( cv.TERM_CRITERIA_EPS | cv.TERM_CRITERIA_COUNT, 10, 1 )
102
track_box, self.track_window = cv.CamShift(prob, self.track_window, term_crit)
103
104
if self.show_backproj:
105
vis[:] = prob[...,np.newaxis]
106
try:
107
cv.ellipse(vis, track_box, (0, 0, 255), 2)
108
except:
109
print(track_box)
110
111
cv.imshow('camshift', vis)
112
113
ch = cv.waitKey(5)
114
if ch == 27:
115
break
116
if ch == ord('b'):
117
self.show_backproj = not self.show_backproj
118
cv.destroyAllWindows()
119
120
121
if __name__ == '__main__':
122
import sys
123
try:
124
video_src = sys.argv[1]
125
except:
126
video_src = 0
127
print(__doc__)
128
App(video_src).run()
129
130