Skip to content

Commit 7783ac1

Browse files
committed
add demo
1 parent 07c7af2 commit 7783ac1

19 files changed

+3055
-388
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
training_data/*
44
validation_data/*
55

6-
*.whl
6+
*.whl
7+
pyenv/

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"python.pythonPath": "pyenv/bin/python3"
3+
}

__pycache__/utils.cpython-36.pyc

649 Bytes
Binary file not shown.
655 Bytes
Binary file not shown.

demos/background_subtraction.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import sys # system functions (ie. exiting the program)
2+
import os # operating system functions (ie. path building on Windows vs. MacOs)
3+
import time # for time operations
4+
import uuid # for generating unique file names
5+
import math # math functions
6+
import argparse
7+
8+
from IPython.display import display as ipydisplay, Image, clear_output, HTML # for interacting with the notebook better
9+
10+
import numpy as np # matrix operations (ie. difference between two matricies)
11+
import cv2 # (OpenCV) computer vision functions (ie. tracking)
12+
(major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')
13+
print('OpenCV Version: {}.{}.{}'.format(major_ver, minor_ver, subminor_ver))
14+
15+
from utils import setup_tracker
16+
17+
# Helper function for applying a mask to an array
18+
def mask_array(array, imask):
19+
if array.shape[:2] != imask.shape:
20+
raise Exception("Shapes of input and imask are incompatible")
21+
output = np.zeros_like(array, dtype=np.uint8)
22+
for i, row in enumerate(imask):
23+
output[i, row] = array[i, row]
24+
return output
25+
26+
27+
# Begin capturing video
28+
video = cv2.VideoCapture(0)
29+
if not video.isOpened():
30+
print("Could not open video")
31+
sys.exit()
32+
33+
34+
# Read first frame
35+
ok, frame = video.read()
36+
if not ok:
37+
print("Cannot read video")
38+
sys.exit()
39+
# Use the first frame as an initial background frame
40+
bg = frame.copy()
41+
42+
43+
# Kernel for erosion and dilation of masks
44+
kernel = np.ones((3,3),np.uint8)
45+
46+
47+
# Capture, process, display loop
48+
while True:
49+
# Read a new frame
50+
ok, frame = video.read()
51+
display = frame.copy()
52+
if not ok:
53+
break
54+
55+
56+
# Start timer
57+
timer = cv2.getTickCount()
58+
59+
# Processing
60+
# First find the absolute difference between the two images
61+
diff = cv2.absdiff(bg, frame)
62+
mask = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
63+
# Threshold the mask
64+
th, thresh = cv2.threshold(mask, 10, 255, cv2.THRESH_BINARY)
65+
# Opening, closing and dilation
66+
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
67+
closing = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel)
68+
img_dilation = cv2.dilate(closing, kernel, iterations=2)
69+
# Get mask indexes
70+
imask = img_dilation > 0
71+
# Get foreground from mask
72+
foreground = mask_array(frame, imask)
73+
74+
75+
# Calculate Frames per second (FPS)
76+
fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer)
77+
# Display FPS on frame
78+
79+
80+
# Display diff
81+
cv2.imshow("diff", diff)
82+
# Display thresh
83+
cv2.imshow("thresh", thresh)
84+
# Display mask
85+
cv2.imshow("img_dilation", img_dilation)
86+
# Display foreground
87+
cv2.imshow("foreground", foreground)
88+
89+
cv2.putText(display, "FPS : " + str(int(fps)), (100, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (50, 170, 50), 2)
90+
# Display result
91+
cv2.imshow("original", display)
92+
93+
k = cv2.waitKey(1) & 0xff
94+
if k == 27: break # ESC pressed
95+
elif k == 114 or k == 112:
96+
# r pressed
97+
bg = frame.copy()
98+
elif k != 255: print(k)
99+
100+
cv2.destroyAllWindows()
101+
video.release()

gesture-recognizer.py renamed to demos/gesture_recognizer.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ class GestureRecognizer:
1818
3: 'swing'
1919
}
2020

21+
positions = {
22+
'hand_pose': (15, 40), # hand pose text
23+
'fps': (15, 20), # fps counter
24+
'null_pos': (200, 200) # used as null point for mouse control
25+
}
26+
2127
def __init__(self, capture=0, model_path=MODEL_PATH):
2228
self.bg = None
2329
self.frame = None # current frame
@@ -27,21 +33,14 @@ def __init__(self, capture=0, model_path=MODEL_PATH):
2733
self.is_tracking = False
2834
self.hand_bbox = (116, 116, 170, 170)
2935

30-
# Display positions (pixel coordinates)
31-
self.positions = {
32-
'hand_pose': (15, 40), # hand pose text
33-
'fps': (15, 20), # fps counter
34-
'null_pos': (200, 200) # used as null point for mouse control
35-
}
36-
3736
# Begin capturing video
38-
self.video = cv2.VideoCapture(1)
37+
self.video = cv2.VideoCapture(capture)
3938
if not self.video.isOpened():
4039
print("Could not open video")
4140

42-
def __del__(self):
43-
cv2.destroyAllWindows()
44-
self.video.release()
41+
# def __del__(self):
42+
# cv2.destroyAllWindows()
43+
# self.video.release()
4544

4645
# Helper function for applying a mask to an array
4746
def mask_array(self, array, imask):
@@ -71,7 +70,7 @@ def extract_foreground(self):
7170
# Get foreground from mask
7271
foreground = self.mask_array(self.frame, imask)
7372

74-
return foreground, mask
73+
return foreground, img_dilation
7574

7675
def run(self):
7776
# Capture, process, display loop
@@ -210,6 +209,7 @@ def run(self):
210209
# Display foreground_display
211210
cv2.imshow("foreground_display", foreground_display)
212211
cv2.imshow("display", display)
212+
cv2.imshow("hand_crop", hand_crop)
213213
# Display result
214214
cv2.imshow("data", data_display)
215215

@@ -221,6 +221,7 @@ def run(self):
221221
self.is_tracking = False
222222
elif k == 116: # t pressed
223223
# Initialize tracker with first frame and bounding box
224+
self.is_tracking = True
224225
self.tracker = cv2.TrackerKCF_create()
225226
self.tracker.init(self.frame, self.hand_bbox)
226227
elif k == 112: # p pressed
@@ -231,5 +232,5 @@ def run(self):
231232
elif k != 255: print(k)
232233

233234
if __name__ == "__main__":
234-
recognizer = GestureRecognizer(1)
235+
recognizer = GestureRecognizer(0)
235236
recognizer.run()
File renamed without changes.
File renamed without changes.

demos/images/running.mp4

18.3 MB
Binary file not shown.

demos/model/adder_model.hdf5

41.9 KB
Binary file not shown.

0 commit comments

Comments
 (0)