From 21c6842337b9ee75a58c0eb2968d9b820df6030d Mon Sep 17 00:00:00 2001 From: "DSTO\\pivatom" Date: Tue, 18 Dec 2018 10:32:17 +1030 Subject: [PATCH] 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. --- GestureRecognition/SimpleHandRecogniser.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/GestureRecognition/SimpleHandRecogniser.py b/GestureRecognition/SimpleHandRecogniser.py index 70e553b..be502c0 100644 --- a/GestureRecognition/SimpleHandRecogniser.py +++ b/GestureRecognition/SimpleHandRecogniser.py @@ -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]: