def test_subtract(self): """Expectation: subtract one number from another using C++ lambda function""" with self.subTest("Testing subtract() using ints"): computed = utils.subtract(self.i3, self.i4) self.assertIsInstance(computed, int) self.assertEqual(computed, int(3 - 4)) with self.subTest("Testing subtract() using floats"): computed = utils.subtract(self.f3, self.f4) self.assertIsInstance(computed, float) self.assertEqual(computed, self.f3 - self.f4)
def length(frames, amount_used=.1): """ Feature based on the average distance each finger moves in the gesture """ num_frames = len(frames) num_used = int(amount_used * num_frames) firsts = [[] for i in range(num_used)] lasts = [[] for i in range(num_used)] for f, first in zip(frames[0:num_used], firsts): positions = get_positions(f) for position in positions: first.append(position) for f, last in zip(frames[-num_used:], lasts): positions = get_positions(f) for position in positions: last.append(position) lengths = [ utils.norm( utils.subtract(utils.average_position(first), utils.average_position(last))) for first, last in zip(firsts, lasts) ] return lengths
def palm_position_histogram(frames, bins=8, range=(-1., 1.)): """ Feature based on a 3d histogram of the normalized positions of each palm """ length = range[1] - range[0] bin_size = length / bins def hround(v): return min(bins - 1, int((v - range[0]) / bin_size)) l = list_3d(bins) positions = [] for frame in frames: for hand in frame.hands(): palm = hand.palm() if palm: p = palm.position positions.append(p) average_p = utils.ave_v(positions) for index, position in enumerate(positions): positions[index] = utils.subtract(position, average_p) v = positions[index] x = v.x / utils.norm(v) y = v.y / utils.norm(v) z = v.z / utils.norm(v) l[hround(x)][hround(y)][hround(z)] += 1 return l
def palm_position_histogram(frames,bins = 8,range=(-1.,1.)): """ Feature based on a 3d histogram of the normalized positions of each palm """ length = range[1]-range[0] bin_size = length/bins def hround(v): return min(bins-1,int((v-range[0])/bin_size)) l = list_3d(bins) positions = [] for frame in frames: for hand in frame.hands(): palm = hand.palm() if palm: p = palm.position positions.append(p) average_p = utils.ave_v(positions) for index,position in enumerate(positions): positions[index] = utils.subtract(position,average_p) v = positions[index] x = v.x/utils.norm(v) y = v.y/utils.norm(v) z = v.z/utils.norm(v) l[hround(x)][hround(y)][hround(z)] += 1 return l
def length(frames,amount_used=.1): """ Returns the average distance each finger moves in the gesture """ num_frames = len(frames) num_used = int(amount_used*num_frames) firsts = [[] for i in range(num_used)] lasts = [[] for i in range(num_used)] for f,first in zip(frames[0:num_used],firsts): positions = get_positions(f) for position in positions: first.append(position) for f,last in zip(frames[-num_used:],lasts): positions = get_positions(f) for position in positions: last.append(position) lengths = [utils.norm(utils.subtract(utils.average_position(first),utils.average_position(last))) for first,last in zip(firsts,lasts)] return lengths
def position_histogram(frames,bins = 4,range=(-1.,1.)): length = range[1]-range[0] bin_size = length/bins def hround(v): return min(bins-1,int((v-range[0])/bin_size)) l = list_3d(bins) positions = [] for frame in frames: for hand in frame.hands(): for finger in hand.fingers(): p = finger.tip().position positions.append(p) average_p = utils.ave_v(positions) for index,position in enumerate(positions): positions[index] = utils.subtract(position,average_p) v = positions[index] x = v.x/utils.norm(v) y = v.y/utils.norm(v) z = v.z/utils.norm(v) l[hround(x)][hround(y)][hround(z)] += 1 return l
def position_histogram(frames, bins=8, range=(-1., 1.)): length = range[1] - range[0] bin_size = length / bins def hround(v): return min(bins - 1, int((v - range[0]) / bin_size)) l = list_3d(bins) positions = [] for frame in frames: for hand in frame.hands(): for finger in hand.fingers(): p = finger.tip().position positions.append(p) average_p = utils.ave_v(positions) for index, position in enumerate(positions): positions[index] = utils.subtract(position, average_p) v = positions[index] x = v.x / utils.norm(v) y = v.y / utils.norm(v) z = v.z / utils.norm(v) l[hround(x)][hround(y)][hround(z)] += 1 return l
def test_subtraction(self): assert 2 == utils.subtract(4, 2)