def extract_core_point_position(img, block_size, tolerance): im = Image.fromarray(np.array(img_as_ubyte(img))) im = im.convert("L") f = lambda x, y: 2 * x * y g = lambda x, y: x**2 - y**2 angles = utils.calculate_angles(im, block_size, f, g) angles = utils.smooth_angles(angles) singularities_positions = poincare.calculate_singularities( im, angles, tolerance, block_size) core_point_position = compute_core_point_position(singularities_positions) return core_point_position
def calculate_vision_board(self, robot): """Calculate a list of all obejcts seen by a robot. The objects are reduced to their center points for this calculation. Returns a list of tuple values for obejcts seen: (index in obstacle_Array, obstacle type, distance from robot's center) """ # get the objects representative points points = self.obstacle_list * 10 + 5 point = (robot.x, robot.y) # use calculate_angles for the maths diffs, dists = utils.calculate_angles(points, point, robot.alpha, robot.fov_angle) out = [] for obst, dif, dist in zip(self.obstacle_list, diffs, dists): # if angle difference is greater zero, the obejct will not be seen if dif <= 0: x, y = obst data = (obst, self.obstacleArray[x][y], dist) out.append(data) return out
if __name__ == "__main__": parser = argparse.ArgumentParser(description="Gabor filter applied") parser.add_argument("image", nargs=1, help="Path to image") parser.add_argument("block_size", nargs=1, help="Block size") parser.add_argument("--save", action='store_true', help="Save result image as src_image_enhanced.gif") args = parser.parse_args() im = Image.open(args.image[0]) im = im.convert("L") # covert to grayscale im.show() W = int(args.block_size[0]) f = lambda x, y: 2 * x * y g = lambda x, y: x**2 - y**2 angles = utils.calculate_angles(im, W, f, g) print "calculating orientation done" angles = utils.smooth_angles(angles) print "smoothing angles done" result = gabor(im, W, angles) result.show() if args.save: base_image_name = os.path.splitext(os.path.basename(args.image[0]))[0] im.save(base_image_name + "_enhanced.gif", "GIF")
def calculate_vision_robots(self, robot): """Calculate a list of robots seen by a robot. A robot (a) can be seen by robot (x) if: - (a) touches (x) - (a)s center is in the direct FoV-angle of (x) - a point of (a)s radius is in the direct FoV-angle of (x) For the last criteria, we check, if (a) intersects one of the rays, marking the outline of the FoV. Returns an array with entries for each robot: The array index equals the robot's position in the server's array. Array entries: False, if the robot can not be seen. A tuple, if the robot is seen: (position, distance between the robot's centers) """ point = (robot.x, robot.y) # no robot is seen per default. result = [False] * len(self.robots) point_list = [] # robots in this list must undergo the angle-check # since they don't overlap. # this also stops invalid point values # from being inserted in calculate_angle. calc_list = [] calc_indices = [] # distance-check for index, rb in enumerate(self.robots): # for each robot, get its distance to (x) and calculate, # wheather they overlap. pos = (rb.x, rb.y) check, d = utils.overlap_check(pos, point, rb.radius, robot.radius) # create a list of position and distance for EVERY robot. point_list.append((pos, d)) # the actual overlap-check: if check: result[index] = (pos, d) # add more cases, if you want to propagate the angles as well else: calc_list.append(pos) calc_indices.append(index) # angle-check angles = [] if calc_list: angles, _ = utils.calculate_angles(calc_list, point, robot.alpha, robot.fov_angle) for index, dif in zip(calc_indices, angles): # if the difference value is positive, the center is not seen. if dif <= 0: result[index] = point_list[index] # ray-check # calculate the two border rays of the fov ray1 = utils.vector_from_angle(robot.alpha - robot.fov_angle/2) ray2 = utils.vector_from_angle(robot.alpha + robot.fov_angle/2) for index, val in enumerate(result): # only check robots that are not already seen if not val: rb = self.robots[index] circle = (rb.x, rb.y, rb.radius) # again, python helps us out! if (utils.ray_check(point, ray1, circle) or utils.ray_check(point, ray2, circle)): result[index] = point_list[index] # now the list is complete return result
for i in range(1, x / W - 1): for j in range(1, y / W - 1): box = (i * W, j * W, min(i * W + W, x), min(j * W + W, y)) freq_img.paste(freqs[i][j] * 255.0 * 1.2, box) return freq_img if __name__ == "__main__": parser = argparse.ArgumentParser(description="Image frequency") parser.add_argument("image", nargs=1, help = "Path to image") parser.add_argument("block_size", nargs=1, help = "Block size") parser.add_argument('--smooth', "-s", action='store_true', help = "Use Gauss for smoothing") args = parser.parse_args() im = Image.open(args.image[0]) im = im.convert("L") # covert to grayscale im.show() W = int(args.block_size[0]) f = lambda x, y: 2 * x * y g = lambda x, y: x ** 2 - y ** 2 angles = utils.calculate_angles(im, W, f, g) if args.smooth: angles = utils.smooth_angles(angles) freq_img = freq_img(im, W, angles) freq_img.show()
def load_skeleton(VIDEO_ROOT_DIR, folder, video): file_dir = os.path.join(VIDEO_ROOT_DIR, folder, video[:-4]) joint_data = pd.read_csv(os.path.join(file_dir, "jointdata.csv")) joint_data["data"] = joint_data["data"].apply(json.loads) index = pd.read_csv(file_dir + "primer.csv", header=None)[0][0] index_1 = len(joint_data) // 3 index_2 = len(joint_data) // 3 * 2 if joint_data.iloc[index_1]["data"][8][0] > joint_data.iloc[index_2][ "data"][8][0]: # walking towards image left Direction = "L" else: Direction = "R" # flip the x coordinate, according to walk direction if Direction == "L": for i in range(len(joint_data)): for j in range(25): joint_data["data"][i][j][ 0] = IMAGE_W - joint_data["data"][i][j][0] # calculate speed pixel/second Neck_1 = joint_data["data"][index_1][1] Neck_2 = joint_data["data"][index_2][1] MidHip_1 = joint_data["data"][index_1][8] MidHip_2 = joint_data["data"][index_2][8] delta_x_OP = (Neck_2[0] + MidHip_2[0]) / 2 - (Neck_1[0] + MidHip_1[0]) / 2 delta_t = abs(joint_data["time"][index_2] - joint_data["time"][index_1]) / 1000 if delta_t == 0: print("error in speed calculation") return -1 else: SpeedOPose = abs(delta_x_OP / delta_t) # calculate 3 angles Neck = joint_data["data"][index][1] MidHip = joint_data["data"][index][8] Nose = joint_data["data"][index][0] Body_Lean = calculate_angles(Nose, MidHip) Back_Lean = calculate_angles(Neck, MidHip) Neck_Lean = calculate_angles(Nose, Neck) # index in the original when it starts to have valid skeleton data idx_start = int(round(joint_data["time"][0] * 30 / 1000)) # index of the last frame with valid skeleton data idx_end = int( round(joint_data["time"][joint_data.shape[0] - 1] * 30 / 1000)) seq_len = idx_end - idx_start + 1 left_seq = [None] * seq_len right_seq = [None] * seq_len index_seq = [None] * seq_len for i in range(joint_data.shape[0]): # calculate the index of the original video, according to the "time" time stamp original_index = int(round(joint_data["time"][i] * 30 / 1000)) # left lower leg's sequence (KNEE_LEFT, ANKLE_LEFT) left_seq[original_index - idx_start] = calculate_angles( joint_data["data"][i][13], joint_data["data"][i][14]) # right lower leg's sequence (KNEE_RIGHT, ANKLE_RIGHT) right_seq[original_index - idx_start] = calculate_angles( joint_data["data"][i][10], joint_data["data"][i][11]) # save the relative index compared to idx_start index_seq[original_index - idx_start] = i imputer = InterpolationImputer() impute_index = list(range(idx_start, idx_end + 1)) left = np.array(imputer.transform([impute_index, left_seq])[1]) right = np.array(imputer.transform([impute_index, right_seq])[1]) # plt.plot(impute_index,left_seq) # plt.show() # plt.plot(impute_index,left) # plt.show() peaks_left, bottom_left = sequence_extrema(left) peaks_right, bottom_right = sequence_extrema(right) left_stance, left_swing = stance_swing(peaks_left, bottom_left) right_stance, right_swing = stance_swing(peaks_right, bottom_right) # Asymmetry Stance phase AStP = calculate_asymmetry(left_stance, right_stance) # Asymmetry Swing phase ASwP = calculate_asymmetry(left_swing, right_swing) cadence = calculate_cadence(peaks_left, peaks_right) left_peak_amp = np.mean([left[i] for i in peaks_left]) left_bottom_amp = np.mean([left[i] for i in bottom_left]) right_peak_amp = np.mean([right[i] for i in peaks_right]) right_bottom_amp = np.mean([right[i] for i in bottom_right]) # Asymmetry Peak Amplitude APA = calculate_asymmetry(left_peak_amp, right_peak_amp) # Asymmetry Bottom Amplitude ABA = calculate_asymmetry(left_bottom_amp, right_bottom_amp) L_index = ceil(len(peaks_left) / 2) - 1 R_index = ceil(len(peaks_right) / 2) - 1 # left lower leg: peaks_left[L_index] to peaks_left[L_index+1] # right lower leg: peaks_right[R_index] to peaks_right[R_index+1] # step length stride length: distance between the heel contact point of one foot and that of the other foot. left_step_length = abs( joint_data["data"][index_seq[peaks_left[L_index]]][21][0] - joint_data["data"][index_seq[peaks_left[L_index]]][24][0]) right_step_length = abs( joint_data["data"][index_seq[peaks_left[R_index]]][21][0] - joint_data["data"][index_seq[peaks_left[R_index + 1]]][24][0]) # Asymmetry Step length ASl = calculate_asymmetry(left_step_length, right_step_length) stride_length = abs( joint_data["data"][index_seq[peaks_left[L_index]]][21][0] - joint_data["data"][index_seq[peaks_right[L_index]]][21][0]) falling_risk = abs( joint_data["data"][index_seq[peaks_left[L_index]]][0][0] - (joint_data["data"][index_seq[peaks_left[L_index]]][21][0] + joint_data["data"][index_seq[peaks_left[L_index]]][24][0]) / 2 ) / (abs(joint_data["data"][index_seq[peaks_left[L_index]]][19][0] - joint_data["data"][index_seq[peaks_left[L_index]]][24][0]) / 2) features = [ SpeedOPose, Body_Lean, Back_Lean, Neck_Lean, left_stance, left_swing, right_stance, right_swing, AStP, ASwP, cadence, left_peak_amp, right_peak_amp, left_bottom_amp, right_bottom_amp, APA, ABA, left_step_length, right_step_length, ASl, stride_length, falling_risk ] return features
def main2(): # alignMinutias((2,8, 'loop'), [[40, 120, 'bip']]) parser = argparse.ArgumentParser( description= "Gabor, thinning, extract minutias, calculate poincare and align minutias." ) parser.add_argument("image", nargs=1, help="Path to image") # parser.add_argument("block_size", nargs=1, help = "Block size") parser.add_argument("tolerance", nargs=1, help="Tolerance for Poincare index") parser.add_argument("block_size", nargs=1, help="Blocksize") parser.add_argument( '--preprocess', "-p", action='store_true', help="Preprocess the image with: Gabor filtering and thinning") parser.add_argument('--smooth', "-s", action='store_true', help="Use Gauss for smoothing") parser.add_argument("--save", action='store_true', help="Save result image as src_image_enhanced.gif") args = parser.parse_args() imagepath = args.image[0] im = None #im = Image.open(args.image[0]) # im = Image.open("ppf-test_enhanced_thinned.gif") #im = im.convert("L") # covert to grayscale # im.show() W = int(args.block_size[0]) #W = 16 print "Block-size: ", W singularity_type = None f = lambda x, y: 2 * x * y g = lambda x, y: x**2 - y**2 #print "Gabor filter and thinning, pointcare, extract minutias, alignment, exporting minutias to file ..." print "image: ", os.path.splitext(imagepath) base_image_name = os.path.splitext(os.path.basename(imagepath))[0] image_enhanced_loc = os.path.splitext( imagepath)[0] + "_enhanced_thinned.gif" image_enhanced_minutiae_loc = os.path.splitext( imagepath)[0] + "_enhanced_thinned_minutiaes.png" image_enhanced_minutiae_aligned_loc = os.path.splitext( imagepath)[0] + "_enhanced_thinned_minutiaes_aligned.png" dumpfilename = os.path.splitext(imagepath)[0] + ".yaml" print "image enhanced: ", image_enhanced_loc # f['image'] if args.preprocess or os.path.isfile(image_enhanced_loc) == False: print "Enhanced image does not exists. Enhancing." print "Gabor filtering and thinning ..." im = Image.open(imagepath) im = im.convert("L") # covert to grayscale # gabor filter angles = utils.calculate_angles(im, W, f, g) print "calculating orientation done" angles = utils.smooth_angles(angles) print "smoothing angles done" im = gabor(im, W, angles) # im.show() # thinning im = make_thin(im) # im.show() if args.save: im.save(image_enhanced_loc, "GIF") if args.preprocess: im.close() return else: print "Processing enhanced image." if im is not None: im.close() im = Image.open(image_enhanced_loc) im = im.convert("L") # covert to grayscale # continue # get image bounds (maxx, maxy) = im.size bounds = (0, maxx, 0, maxy) print "bounds: ", bounds angles = utils.calculate_angles(im, W, f, g) if args.smooth: angles = utils.smooth_angles(angles) singularity_type = None result, cores = calculate_singularities(im, angles, int(args.tolerance[0]), W, singularity_type=singularity_type) print "cores: ", cores core = chooseCore(cores, W) print "Selected core: ", core # result.show() # align bounds with respect to the core # bounds2 = alignBounds(core, bounds) # calculate minutias im, minutiaes = calculate_minutiaes(im) im.show() print "minutias: ", minutiaes # plotAndSaveMinutiae(minutiaes, image_enhanced_minutiae_loc) plotAndSaveMinutiasPIL(minutiaes, image_enhanced_minutiae_loc, im.size) minutiaes = alignMinutias(core, minutiaes) # aligning print "aminutia: ", minutiaes # plotAndSaveMinutiae(minutiaes, image_enhanced_minutiae_aligned_loc) plotAndSaveMinutiasPIL(minutiaes, image_enhanced_minutiae_aligned_loc, im.size) des = {'minutiaes': minutiaes, 'core': core, 'bounds': bounds} with open(dumpfilename, 'wb') as handle: json.dump(des, handle) print ""
def main1(): # alignMinutias((2,8, 'loop'), [[40, 120, 'bip']]) parser = argparse.ArgumentParser( description= "Gabor, thinning, extract minutias, calculate poincare and align minutias." ) parser.add_argument("image", nargs=1, help="Path to image") # parser.add_argument("block_size", nargs=1, help = "Block size") parser.add_argument("tolerance", nargs=1, help="Tolerance for Poincare index") parser.add_argument("block_size", nargs=1, help="Blocksize") parser.add_argument( '--preprocess', "-p", action='store_true', help="Preprocess the image with: Gabor filtering and thinning") parser.add_argument('--smooth', "-s", action='store_true', help="Use Gauss for smoothing") parser.add_argument("--save", action='store_true', help="Save result image as src_image_enhanced.gif") args = parser.parse_args() #im = Image.open(args.image[0]) # im = Image.open("ppf-test_enhanced_thinned.gif") #im = im.convert("L") # covert to grayscale # im.show() W = int(args.block_size[0]) #W = 16 print "Block-size: ", W singularity_type = None f = lambda x, y: 2 * x * y g = lambda x, y: x**2 - y**2 files = [] for i in range(1, 4): for j in range(1, 4): if i < 10: finputminutia = '/Users/Admin/fvs/samples/DB3_B/10' + str( i) + '_' + str(j) + '.tif' else: finputminutia = '/Users/Admin/fvs/samples/DB3_B/1' + str( i) + '_' + str(j) + '.tif' files.append({'i': i, 'j': j, 'image': finputminutia}) ffirst_singularity = {} print "Gabor filter, thinning, pointcare, extract minutias, alignment, exporting minutias to file ..." for file in files: i = file['i'] j = file['j'] print "image: ", os.path.splitext(file['image']) base_image_name = os.path.splitext(os.path.basename(file['image']))[0] image_enhanced_loc = "/Users/Admin/fvs/samples/dumps2/" + base_image_name + "_enhanced.gif" image_enhanced_minutiae_loc = "/Users/Admin/fvs/samples/dumps2/" + base_image_name + "_enhanced_minutiaes.png" image_enhanced_minutiae_aligned_loc = "/Users/Admin/fvs/samples/dumps2/" + base_image_name + "_enhanced_minutiaes_aligned.png" print "image enhanced: ", image_enhanced_loc # f['image'] if args.preprocess or os.path.isfile(image_enhanced_loc) == False: print "Enhanced image does not exists. Enhancing." im = Image.open(file['image']) im = im.convert("L") # covert to grayscale # gabor filter angles = utils.calculate_angles(im, W, f, g) print "calculating orientation done" angles = utils.smooth_angles(angles) print "smoothing angles done" im = gabor(im, W, angles) # im.show() # thinning im = make_thin(im) # im.show() if args.save: im.save(image_enhanced_loc, "GIF") if args.preprocess: continue else: print "Processing enhanced image." im = Image.open(image_enhanced_loc) im = im.convert("L") # covert to grayscale # continue # get image bounds (maxx, maxy) = im.size bounds = (0, maxx, 0, maxy) print "bounds: ", bounds angles = utils.calculate_angles(im, W, f, g) if args.smooth: angles = utils.smooth_angles(angles) if i in ffirst_singularity: singularity_type = ffirst_singularity[i] else: singularity_type = None result, cores = calculate_singularities( im, angles, int(args.tolerance[0]), W, singularity_type=singularity_type) print "cores: ", cores core = chooseCore(cores, W) print "Selected core: ", core if j == 1: ffirst_singularity[i] = core[3] # result.show() # align bounds with respect to the core # bounds2 = alignBounds(core, bounds) # calculate minutias im, minutiaes = calculate_minutiaes(im) im.show() print "minutias: ", minutiaes # plotAndSaveMinutiae(minutiaes, image_enhanced_minutiae_loc) plotAndSaveMinutiasPIL(minutiaes, image_enhanced_minutiae_loc, im.size) minutiaes = alignMinutias(core, minutiaes) # aligning print "aminutia: ", minutiaes # plotAndSaveMinutiae(minutiaes, image_enhanced_minutiae_aligned_loc) plotAndSaveMinutiasPIL(minutiaes, image_enhanced_minutiae_aligned_loc, im.size) des = {'minutiaes': minutiaes, 'core': core, 'bounds': bounds} print os.path.splitext(file['image'])[0] dumpfilename = "/Users/Admin/fvs/samples/dumps2/" + os.path.basename( file['image']) + ".yaml" with open(dumpfilename, 'wb') as handle: json.dump(des, handle) print "" if im is not None: im.close()