forked from richzeng/asterisk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fingertracker.py
99 lines (83 loc) · 2.78 KB
/
fingertracker.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from SimpleCV import Camera, VirtualCamera, Display
import time
import sys
class FingerTracker(object):
"""Finger tracking base class
"""
def __init__(self, camera=None):
"""Initialize the finger tracker
:param TrackerIn ti: Tracker input
:param camera: Camera index, filename, or None
"""
if camera is None:
self.cam = Camera(0)
elif type(camera) == int:
self.cam = Camera(camera)
else:
self.cam = VirtualCamera(camera, "video")
@staticmethod
def get_tracker(algorithm):
"""Get the tracker for a given algorithm.
Supported algorithms:
default: Same as 'skeleton'
peaks: peak-finding
skeleton: skeleton-finding
"""
if algorithm == "default":
algorithm = "peaks"
if algorithm == "peaks":
from fingertracker_peaks import FingerTrackerPeaks
return FingerTrackerPeaks
elif algorithm == "skeleton":
from fingertracker_skeleton import FingerTrackerSkeleton
return FingerTrackerSkeleton
else:
print "Unknown algorithm: {}".format(algorithm)
def add_two_positions(self, ti, positions):
"""Adds two finger positions to ti
:param ti: TrackerIn object
:param list positions: List of (x,y) coordinates
"""
pos = []
last_x = -100
for x, y in sorted(positions):
if x - last_x > 10:
pos.append((x, y))
last_x = x
if len(pos) == 1:
pos *= 2
elif len(pos) > 2:
return
ti.add_positions(pos)
def add_positions(self, ti, positions):
"""Adds two finger positions to ti
:param ti: TrackerIn object
:param list positions: List of (x,y) coordinates
"""
ti.add_positions(positions)
def run_frame(self, ti, img):
"""Run the algorithm for one frame
:param TrackerIn ti: TrackerIn object to send events to
:return: True if I should be called with the next frame
"""
pass
def finish(self):
"""Finish tracking (called when the tracker is interrupted or closed"""
pass
def run(self, ti):
"""Run the algorithm
:param TrackerIn ti: TrackerIn object to send events to
"""
if sys.platform == "darwin":
dis = Display() # needed to work on macs
time.sleep(2) # needed to work on macs
try:
loop = True
while loop:
img = self.cam.getImage()
if img.getBitmap() == '':
break
loop = self.run_frame(ti, img)
except KeyboardInterrupt:
pass
self.finish()