-
Notifications
You must be signed in to change notification settings - Fork 4
/
output.py
160 lines (140 loc) · 4.45 KB
/
output.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import cv2
import datetime
import draw
from hand import Hand
from hand_pos import Outline, HandPos
import logging
class Output(object):
def __init__(self, **kwargs):
defaults = {
# Show estimated hand position in final output
"show_estimate": False,
# Show detector results in final output
"show_detectors": True,
# Show skin contours in final output
"show_skin": True,
# Colorful output
"rainbow_colors": False,
# Store screenshots in separate directory
"screenshot_dir": "assets/screenshots/"
}
# Set default config
for key in defaults:
setattr(self, key, defaults[key])
# Modify with available parameters
for key in kwargs:
setattr(self, key, kwargs[key])
# Some colors for drawing
self.colors = {
"white" : (255,255,255),
"blue" : (233, 183, 47),
"darkblue" : (183, 72, 37),
"green" : (0,182,136),
"yellow" : (0,255,255),
"red" : (67,47,241)
}
self.output_settings = {
# Detector: (color, thickness)
"estimate": (self.colors["white"] , 8),
"haar": (self.colors["green"] , 4),
"camshift": (self.colors["red"] , 4),
"shape": (self.colors["blue"] , 4),
"default": (self.colors["yellow"] , 4)
}
def show(self, img, hand, reference_point = None):
"""
Create output with transparent image overlay
"""
# Store current image
self.img = img
# Output
self.output_positions(hand)
self.output_hand_contour(hand)
self.output_reference_point(reference_point)
cv2.imshow("Tracker", self.img)
def output_reference_point(self, ref):
"""
Draw the reference point for mouse movement
"""
if ref: draw.cross(self.img, ref)
def output_positions(self, hand):
"""
Draw hand estimates of all running detectors
"""
for detector, properties in hand.pos.iteritems():
if detector == "estimate":
if not self.show_estimate:
continue
elif not self.show_detectors:
continue
color, thickness = self.get_output_settings(detector, properties)
if properties:
self.draw_shape(properties.pos, properties.outline, color, thickness)
def output_hand_contour(self, hand):
"""
Draw a hand contour on the image
"""
if self.show_skin:
self.draw_hand(hand)
def get_output_settings(self, detector, properties):
"""
Specify color and thickness of geometric shape for detector output.
"""
if detector == "estimate":
return self.output_settings["estimate"]
if self.rainbow_colors:
return self.output_settings[detector]
else:
return (draw.color_gradient(properties.prob), 4)
def draw_shape(self,pos, outline, color, thickness):
"""
Draw a geometric shape above the image
"""
if pos == None:
return
try:
if outline == Outline.RECT:
draw.draw_rect(self.img, pos, color, thickness)
elif outline == Outline.ELLIPSE:
cv2.ellipse(self.img, pos, color, thickness)
except Exception, e:
logging.exception("Output: Can't show result of %s", detector_name)
def draw_hand(self, hand):
"""
Draw hand contours and finger lines
"""
try:
if hand.contours != None and hand.hierarchy != None:
draw.draw_contours(self.img, hand.contours, hand.hierarchy, self.colors["blue"], 0.7, hand.contours_offset)
self.draw_fingers(hand)
#if hand.contour != None:
# draw.draw_contour(self.img, hand.contour, self.colors["blue"], 0.7, hand.contours_offset)
# self.draw_fingers(hand)
except Exception, e:
logging.exception("Output: Can't draw contours")
def draw_fingers(self, hand):
"""
Draw each finger.
"""
for finger in hand.fingers:
draw.draw_lines(self.img, finger, self.colors["darkblue"])
def toggle_estimate(self):
"""
Show/hide estimate rectangle in final output
"""
self.show_estimate = not self.show_estimate
def toggle_detectors(self):
"""
Show/hide detector rectangles in final output
"""
self.show_detectors = not self.show_detectors
def toggle_skin(self):
"""
Show/hide skin contours in final output
"""
self.show_skin = not self.show_skin
def make_screenshot(self):
timestamp = datetime.datetime.now()
filename = "{0}Screenshot {1}.jpg".format(self.screenshot_dir, timestamp)
logging.info("Writing %s", filename)
cv2.imwrite(filename, self.img)