Add additional comments, and other small changes.

Removes the print statement in the get_gesture as this
dramatically reduces performance. Also made circle method take a
parameter for percentage size of the circle.
This commit is contained in:
DSTO\pivatom
2018-12-18 10:32:17 +10:30
parent eb784f2f27
commit 21c6842337

View File

@@ -7,6 +7,10 @@ class SimpleHandRecogniser(HandRecogniser):
self._image_path = image_path
def load_image(self, image_path = None):
"""
Loads the given image path into memory. This must be called before
any other operations can be completed.
"""
if image_path is not None:
self._image_path = image_path
self.img = cv2.imread(self._image_path, 1)
@@ -42,10 +46,12 @@ class SimpleHandRecogniser(HandRecogniser):
"""
image = cv2.GaussianBlur(image,(5,5),0)
def __calc_circle(self, image):
def __calc_circle(self, image, radius_percent = 0.55):
"""
Calculates the equation of the circle (radius, centre) from a given
threshold image.
threshold image, so that the circle is the center of gravity of the
given threshold pixels, and the radius is by default 55% of the total
size.
"""
k = np.sum(self.mask) / 255
@@ -81,13 +87,14 @@ class SimpleHandRecogniser(HandRecogniser):
if new_distance > radius:
radius = new_distance
radius = int(radius * 0.55)
radius = int(radius * radius_percent)
return radius, centre
def get_gesture(self):
"""
Calculates the actual gesture.
Calculates the actual gesture, returning the number of fingers
seen in the image.
"""
if not self.img:
return 0
@@ -101,17 +108,12 @@ class SimpleHandRecogniser(HandRecogniser):
# First just do it the naive way with loops.
# Equation of the circle:
# y = sqrt(r2 - (x-c)2) + c
# This is extremely slow, need to speed it up by removing for loop.
# Brings speed down to 20 fps.
# Could try a kerel method?
# Also can try contour detection.
prev_x = centre[0] - radius
prev_y = [self.__calc_pos_y(centre[0] - radius, radius, centre), self.__calc_pos_y(centre[0] - radius, radius, centre)]
num_change = 0
for x in range(centre[0] - radius + 1, centre[0] + radius):
ypos = self.__calc_pos_y(x, radius, centre)
y = [ypos, centre[1] - (ypos-centre[1])]
print(y)
if(self.mask[y[0], x] != self.mask[prev_y[0], prev_x]):
num_change += 1
if self.mask[y[1], x] != self.mask[prev_y[1], prev_x] and y[0] != y[1]: