-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject tracking.py
77 lines (67 loc) · 2.45 KB
/
object tracking.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import numpy as np
import cv2
col, width, row, height = -1, -1, -1, -1
frame = None
frame2 = None
inputmode = False
rectangle = False
trackWindow = None
roi_hist = None
def onMouse(event, x, y, flags, param):
global col, width, row, height, frame, frame2, inputmode
global rectangle, roi_hist, trackWindow
if inputmode:
if event == cv2.EVENT_LBUTTONDOWN:
rectangle = True
col, row = x, y
elif event == cv2.EVENT_MOUSEMOVE:
if rectangle:
frame = frame2.copy()
cv2.rectangle(frame, (col, row), (x, y), (0, 255, 0), 2)
cv2.imshow('frame', frame)
elif event == cv2.EVENT_LBUTTONUP:
inputmode = False
rectangle = False
cv2.rectangle(frame, (col, row), (x, y), (0, 255, 0), 2)
height, width = abs(row-y), abs(col-x)
trackWindow = (col, row, width, height)
roi = frame[row:row+height, col:col+height]
roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
roi_hist = cv2.calcHist([roi], [0], None, [180], [0, 180])
cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX)
return
def camShift():
global frame, frame2, inputmode, trackWindow, roi_hist
try:
cap = cv2.VideoCapture(0)
except Exception as e:
print(e)
return
ret, frame = cap.read()
cv2.namedWindow('frame')
cv2.setMouseCallback('frame', onMouse, param = (frame, frame2))
termination = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)
while True:
ret, frame = cap.read()
if not ret:
break
if trackWindow is not None:
hsv = cv2.cvtColor(frame, cv2.COLOR_RGB2HSV)
dst = cv2.calcBackProject([hsv], [0], roi_hist, [0,180],1)
ret, trackWindow = cv2.CamShift(dst, trackWindow, termination)
x, y, w, h = trackWindow
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.imshow('frame', frame)
k = cv2.waitKey(60) & 0xFF
if k == 27:
break
if k == ord('i'):
print('Select Area for CamShift and Enter a key')
inputmode = True
frame2 = frame.copy()
while inputmode:
cv2.imshow('frame',frame)
cv2.waitKey(0)
cap.release()
cv2.destroyAllWindows()
camShift()