Skip to content

Commit a008da5

Browse files
committed
python/examples/gesture_rec/process.py: Code cleanup
Added more commentaries and made code look somewhat nicer. Signed-off-by: Daniel Matyas <[email protected]>
1 parent 487f032 commit a008da5

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

bindings/python/examples/gesture_rec/notebook.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def _start_video(self):
122122
distance_scale = 255.0 / camera_range
123123
tof_frame = tof.Frame()
124124
computation_delay_start = time.time()
125-
daniels_process_results = []
125+
process_results = []
126126
executor = concurrent.futures.ProcessPoolExecutor()
127127

128128
while True:
@@ -148,22 +148,22 @@ def _start_video(self):
148148
cv.namedWindow('Depth image', cv.WINDOW_AUTOSIZE)
149149
cv.imshow('Depth image', img)
150150

151-
# if daniels_process_results != []:
152-
daniels_process_results = self.update_display(daniels_process_results)
151+
# if process_results != []:
152+
process_results = self.update_display(process_results)
153153

154154
# Process image every 1s
155155
if (time.time() - computation_delay_start) <= 1: continue
156156

157157
p = executor.submit(calc_process, depth_map)
158-
daniels_process_results.append(p)
158+
process_results.append(p)
159159
computation_delay_start = time.time()
160160

161161
executor.shutdown()
162162
cv.destroyWindow("Depth image")
163163

164-
def update_display(self, daniels_process_results):
164+
def update_display(self, process_results):
165165
to_delete = []
166-
processes = concurrent.futures.as_completed(daniels_process_results)
166+
processes = concurrent.futures.as_completed(process_results)
167167
for p in processes:
168168
try:
169169
result, self.box_start_point, self.box_end_point = p.result()
@@ -175,7 +175,7 @@ def update_display(self, daniels_process_results):
175175
self.update()
176176
to_delete.append(p)
177177

178-
return [p for p in daniels_process_results if p not in to_delete]
178+
return [p for p in process_results if p not in to_delete]
179179

180180

181181
if __name__ == '__main__':

bindings/python/examples/gesture_rec/process.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from scipy.spatial import ConvexHull
44
from scipy.ndimage import distance_transform_edt
55

6+
# These are empirical values, which were found by trial and error
67
pixel_no_threshold = 200
78
analyzed_region_distance = 38.25
89
hand_radius_error = 1.9
@@ -25,17 +26,19 @@ def _display_result(self):
2526
# =============================================================================
2627
def _depth_img_hist(self):
2728
# hist[0] = number of elements
28-
# hist[1] = distance normalized to 255
29+
# hist[1] = distance normalized to 255 (this was done in notebook.py)
2930
counts, bins = np.histogram(self.depth_img.ravel(), 512)
3031
hist = [counts, bins]
32+
3133
start = 1
3234
while start < (len(hist[0]) - 2) and hist[0][start] < pixel_no_threshold:
3335
start += 1
3436
bin_step = hist[1][start + 1] - hist[1][start]
3537

3638
stop = int((hist[1][start] + analyzed_region_distance - hist[1][0]) / bin_step)
3739

38-
self.stop_distance = hist[1][stop]
40+
self.stop_distance = hist[1][stop]
41+
# Analyze the pixel only if it is closer than a threshold
3942
self.binary_img = (self.depth_img < self.stop_distance) * 1
4043

4144
# =============================================================================
@@ -45,6 +48,7 @@ def _detect_hand(self):
4548
props = measure.regionprops(labels)
4649
if len(props) == 0:
4750
raise Exception("No object found.")
51+
4852
props.sort(key=lambda x: x.area, reverse=True)
4953
self.max_area = props[0]
5054
points = props[0].filled_image
@@ -55,8 +59,8 @@ def _detect_hand(self):
5559

5660
# Compute the distance of non-zero points (hand) to the nearest zero point (background)
5761
self.dist_map = distance_transform_edt(points)
62+
# Indices of hand center, i.e. the point farthest from the background
5863
self.hand_center = tuple(arr[0] for arr in np.where(self.dist_map == np.max(self.dist_map)))
59-
6064
self.radius = hand_radius_error * np.max(self.dist_map)
6165

6266
# =============================================================================
@@ -71,21 +75,22 @@ def _count_fingers(self):
7175
[vertices[-1] - vertices[0]], axis=0)
7276

7377
# distance bw 2 consecutive vertices
78+
# In cdist variables the distance units are pixels
7479
cdist = np.sqrt(dist[:, 0] ** 2 + dist[:, 1] ** 2)
7580

76-
# This is just some formula invented by me. It is not optimal.
77-
# It is used to make cdist_threshold inversely proportional to distance_threshold,
78-
# while keeping it lower than 25
81+
# TODO: Use a better formula
82+
# It is used to make cdist_threshold inversely proportional to stop_distance,
83+
# while keeping it between 0 and 25
7984
cdist_threshold = np.sqrt(1 - self.stop_distance / 255) * 25
80-
cdist = (cdist <= cdist_threshold) * 1
85+
cdist_bin = (cdist <= cdist_threshold) * 1
8186

8287
# Used to check whether a cdist smaller than the threshold
8388
# is following a cdist bigger than the threshold
84-
cdist_diff = np.append(cdist[0:len(cdist) - 1] - cdist[1:len(cdist)],
85-
[cdist[-1] - cdist[0]], axis=0)
89+
cdist_diff = np.append(cdist_bin[0:len(cdist_bin) - 1] - cdist_bin[1:len(cdist_bin)],
90+
[cdist_bin[-1] - cdist_bin[0]], axis=0)
8691

8792
# Indices of vertices which correspond to fingertips
88-
dist_idx = np.where((cdist_diff == -1) | ((cdist_diff == 0) & (cdist == 0)))
93+
dist_idx = np.where((cdist_diff == -1) | ((cdist_diff == 0) & (cdist_bin == 0)))
8994
dist_idx = np.array(dist_idx) + 1
9095
# dist_idx is a double list
9196
dist_idx = dist_idx[0]
@@ -123,3 +128,4 @@ def detect_gesture(self):
123128
self.resultVar = "Found " + str(len(self.fingers[0])) + " extended fingers. Rock"
124129
else:
125130
self.resultVar = "Found " + str(len(self.fingers[0])) + " extended fingers. Unknown gesture"
131+

0 commit comments

Comments
 (0)