Skip to content

Commit fd1da1d

Browse files
authored
Add files via upload
1 parent 1cd02e2 commit fd1da1d

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed

run.py

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import numpy as np
2+
import cv2
3+
import sys
4+
from time import time
5+
6+
import KCF
7+
8+
9+
selectingObject = False
10+
initTracking = False
11+
onTracking = False
12+
ix, iy, cx, cy = -1, -1, -1, -1
13+
w, h = 0, 0
14+
15+
inteval = 1
16+
duration = 0.01
17+
18+
19+
# mouse callback function
20+
def draw_boundingbox(event, x, y, flags, param):
21+
global selectingObject, initTracking, onTracking, ix, iy, cx,cy, w, h
22+
23+
if event == cv2.EVENT_LBUTTONDOWN:
24+
selectingObject = True
25+
onTracking = False
26+
ix, iy = x, y
27+
cx, cy = x, y
28+
29+
elif event == cv2.EVENT_MOUSEMOVE:
30+
cx, cy = x, y
31+
32+
elif event == cv2.EVENT_LBUTTONUP:
33+
selectingObject = False
34+
if(abs(x-ix)>10 and abs(y-iy)>10):
35+
w, h = abs(x - ix), abs(y - iy)
36+
ix, iy = min(x, ix), min(y, iy)
37+
initTracking = True
38+
else:
39+
onTracking = False
40+
41+
elif event == cv2.EVENT_RBUTTONDOWN:
42+
onTracking = False
43+
if(w>0):
44+
ix, iy = x-w/2, y-h/2
45+
initTracking = True
46+
47+
48+
49+
if __name__ == '__main__':
50+
51+
if(len(sys.argv)==1):
52+
cap = cv2.VideoCapture(0)
53+
elif(len(sys.argv)==2):
54+
if(sys.argv[1].isdigit()): # True if sys.argv[1] is str of a nonnegative integer
55+
cap = cv2.VideoCapture(int(sys.argv[1]))
56+
else:
57+
cap = cv2.VideoCapture(sys.argv[1])
58+
inteval = 30
59+
else: assert(0), "too many arguments"
60+
61+
tracker = KCF.kcftracker(False, True, False, False) # hog, fixed_window, multiscale, lab
62+
63+
cv2.namedWindow('tracking')
64+
cv2.setMouseCallback('tracking',draw_boundingbox)
65+
66+
while(cap.isOpened()):
67+
ret, frame = cap.read()
68+
if not ret:
69+
break
70+
71+
if(selectingObject):
72+
cv2.rectangle(frame,(ix,iy), (cx,cy), (0,255,255), 1)
73+
elif(initTracking):
74+
cv2.rectangle(frame,(ix,iy), (ix+w,iy+h), (0,255,255), 2)
75+
76+
tracker.init([ix,iy,w,h], frame)
77+
78+
initTracking = False
79+
onTracking = True
80+
elif(onTracking):
81+
t0 = time()
82+
boundingbox = tracker.update(frame) #frame had better be contiguous
83+
t1 = time()
84+
85+
boundingbox = map(int, boundingbox)
86+
cv2.rectangle(frame,(boundingbox[0],boundingbox[1]), (boundingbox[0]+boundingbox[2],boundingbox[1]+boundingbox[3]), (0,255,255), 1)
87+
88+
duration = 0.8*duration + 0.2*(t1-t0)
89+
#duration = t1-t0
90+
cv2.putText(frame, 'FPS: '+str(1/duration)[:4].strip('.'), (8,20), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,255), 2)
91+
92+
cv2.imshow('tracking', frame)
93+
c = cv2.waitKey(inteval) & 0xFF
94+
if c==27 or c==ord('q'):
95+
break
96+
97+
cap.release()
98+
cv2.destroyAllWindows()

setup.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from distutils.core import setup, Extension
2+
from Cython.Distutils import build_ext
3+
import numpy
4+
5+
libdr = ['/usr/local/lib']
6+
incdr = [numpy.get_include(), '/usr/local/include/']
7+
8+
ext = [
9+
Extension('cvt', ['python/cvt.pyx'],
10+
language = 'c++',
11+
extra_compile_args = ['-std=c++11'],
12+
include_dirs = incdr,
13+
library_dirs = libdr,
14+
libraries = ['opencv_core']),
15+
Extension('KCF', ['python/KCF.pyx', 'src/kcftracker.cpp', 'src/fhog.cpp'],
16+
language = 'c++',
17+
extra_compile_args = ['-std=c++11'],
18+
include_dirs = incdr,
19+
library_dirs = libdr,
20+
libraries = ['opencv_core', 'opencv_imgproc'])
21+
]
22+
23+
setup(
24+
name = 'app',
25+
cmdclass = {'build_ext':build_ext},
26+
ext_modules = ext
27+
)
28+
29+
#python setup.py build_ext --inplace

0 commit comments

Comments
 (0)