def get_transformed_points(): min_score = 0.05 instance_pose = ClassOpenPose() res = ClassDescriptors.load_images_comparision_ext(instance_pose, min_score, load_one_img=True) person_arr = res['pose1'] descriptors = ClassDescriptors.get_person_descriptors(person_arr, min_score, cam_number=0, image=None, calib_params=None, decode_img=False, instance_nn_pose=None) transformed_points = descriptors['transformedPoints'] print(transformed_points) # Save pose for debugging purposes re_scale_factor = 100 new_points = ClassDescriptors.re_scale_pose_factor(transformed_points, re_scale_factor, min_score) img_pose = ClassDescriptors.draw_pose_image(new_points, min_score, is_transformed=True) cv2.namedWindow('main_window', cv2.WINDOW_AUTOSIZE) cv2.imshow('main_window', img_pose) cv2.waitKey() cv2.destroyAllWindows() print('Done!')
def test_full_color_compare(): print('Process lab adjustment') # Loading instances instance_pose = ClassOpenPose() # Loading images image1, image2, pose1, pose2 = ClassDescriptors.load_images_comparision( instance_pose, min_score) # Performing color comparision upper1, lower1 = ClassDescriptors.process_colors_person(pose1, min_score, image1, decode_img=False) upper2, lower2 = ClassDescriptors.process_colors_person(pose2, min_score, image2, decode_img=False) # Performing custom comparison first diff_upper, diff_lower, delta = ClassUtils.compare_colors( upper1, upper2, lower1, lower2) print('Diff upper: {0}'.format(diff_upper)) print('Diff lower: {0}'.format(diff_lower)) print('Delta: {0}'.format(delta)) # Showing images cv2.namedWindow('main_window', cv2.WND_PROP_AUTOSIZE) cv2.imshow('main_window', np.hstack((image1, image2))) cv2.waitKey(0) print('Done!')
def evaluating_fast_nn(): print('Initializing evaluating fast nn') classes = 8 hidden_layers = 40 instance_nn = ClassNN(model_dir=ClassNN.model_dir_pose, classes=classes, hidden_number=hidden_layers) instance_pose = ClassOpenPose() info = ClassDescriptors.load_images_comparision_ext(instance_pose, min_score=0.05, load_one_img=True) pose1 = info['pose1'] items = ClassDescriptors.get_person_descriptors(pose1, 0.05) # Valid pose for detection data_to_add = list() data_to_add += items['angles'] data_to_add += ClassUtils.get_flat_list(items['transformedPoints']) data_np = np.asanyarray(data_to_add, dtype=np.float) result = instance_nn.predict_model_fast(data_np) print(result) key_pose = result['classes'] probability = result['probabilities'][key_pose] print('Key pose: {0}'.format(key_pose)) print('Probability: {0}'.format(probability)) print('Done!')
def main(): print('Initializing main function') # Initialing instances instance_pose = ClassOpenPose() filename = '/home/mauricio/Pictures/walk.jpg' image = cv2.imread(filename) arr = instance_pose.recognize_image(image) if len(arr) != 1: raise Exception( 'There is more than one person in the image: {0}'.format(len(arr))) per_arr = arr[0] print(per_arr) ClassDescriptors.draw_pose(image, per_arr, min_score) cv2.namedWindow('main_window', cv2.WINDOW_AUTOSIZE) cv2.imshow('main_window', image) cv2.waitKey(0) cv2.destroyAllWindows() print('Done!')
def test_color_compare(): print('Test color comparision') print('Loading image comparision') # Loading instances instance_pose = ClassOpenPose() # Avoid to open two prompts obj_img = ClassDescriptors.load_images_comparision_ext(instance_pose, min_score, load_one_img=True) hist_pose1 = obj_img['listPoints1'] list_process = list() # Iterating in examples folder for root, _, files in os.walk(EXAMPLES_FOLDER): for file in files: full_path = os.path.join(root, file) extension = ClassUtils.get_filename_extension(full_path) if extension == '.jpg': list_process.append(full_path) # Sorting list list_process.sort() list_result = list() score_max_pt = -1 for full_path in list_process: print('Processing file: {0}'.format(full_path)) json_path = full_path.replace('.jpg', '.json') with open(json_path, 'r') as f: obj_json = json.loads(f.read()) his_pose2 = obj_json['histPose'] diff = ClassDescriptors.get_kmeans_diff(hist_pose1, his_pose2) print('Diff {0}'.format(diff)) if diff <= 15: res = True else: res = False list_result.append({ 'filename': ClassUtils.get_filename_no_extension(full_path), 'score': res }) # list_result.sort(key=lambda x: x['score']) print('Printing list result') print(json.dumps(list_result, indent=2)) print('min_score: {0}'.format(score_max_pt)) print('Done!')
def reprocess_images(list_folder_data): print('Init reprocess_images') for index, item in enumerate(list_folder_data): folder = item[0] min_score = item[1] print('Processing folder: {0}'.format(folder)) list_files = os.listdir(folder) random.Random(seed).shuffle(list_files) for num_file, filename in enumerate(list_files): file = os.path.join(folder, filename) extension = ClassUtils.get_filename_extension(file) if extension == '.json': with open(file, 'r') as f: data_str = f.read() data_json = json.loads(data_str) if 'vectors' in data_json: print('Processing json file with new format: {0}'.format(file)) person_arr = data_json['vectors'] else: print('Processing json file: {0}'.format(file)) person_arr = data_json valid = ClassUtils.check_vector_integrity_pos(person_arr, min_score) only_pos = ClassUtils.check_vector_only_pos(person_arr, min_score) if not valid: raise Exception('Vector integrity not valid for file: {0}'.format(file)) if only_pos: raise Exception('Invalid vector to perform detection') descriptors = ClassDescriptors.get_person_descriptors(person_arr, min_score, cam_number=0, image=None, calib_params=None, decode_img=False, instance_nn_pose=None) with open(file, 'w') as f: f.write(json.dumps(descriptors)) transformed_points = descriptors['transformedPoints'] # Save pose for debugging purposes re_scale_factor = 100 new_points = ClassDescriptors.re_scale_pose_factor(transformed_points, re_scale_factor, min_score) img_pose = ClassDescriptors.draw_pose_image(new_points, min_score, is_transformed=True) new_file_name = ClassUtils.get_filename_no_extension(file) + '_1.jpg' cv2.imwrite(new_file_name, img_pose) print('Done!')
def process_btf(): print('Processing BTF transformation') # Loading instances instance_pose = ClassOpenPose() image1, image2, pose1, pose2 = ClassDescriptors.load_images_comparision( instance_pose, min_score) hists1 = ClassDescriptors.get_color_histograms(pose1, min_score, image1, decode_img=False) hists2 = ClassDescriptors.get_color_histograms(pose2, min_score, image2, decode_img=False) # Plotting # plot_histograms(hists1) # Showing first images without transformation cv2.namedWindow('main_window', cv2.WINDOW_AUTOSIZE) upper1, lower1 = ClassDescriptors.process_colors_person(pose1, min_score, image1, decode_img=False) upper2, lower2 = ClassDescriptors.process_colors_person(pose2, min_score, image2, decode_img=False) print('Diff1: {0}'.format(ClassUtils.get_color_diff_rgb(upper1, upper2))) print('Diff2: {0}'.format(ClassUtils.get_color_diff_rgb(lower1, lower2))) cv2.imshow('main_window', np.hstack((image1, image2))) print('Press any key to continue') cv2.waitKey(0) # Perform image transformation image_tr = ClassDescriptors.transform_image(image2, hists2, hists1) upper1, lower1 = ClassDescriptors.process_colors_person(pose1, min_score, image1, decode_img=False) upper2, lower2 = ClassDescriptors.process_colors_person(pose2, min_score, image_tr, decode_img=False) print('Diff1: {0}'.format(ClassUtils.get_color_diff_rgb(upper1, upper2))) print('Diff2: {0}'.format(ClassUtils.get_color_diff_rgb(lower1, lower2))) cv2.imshow('main_window', np.hstack((image1, image_tr))) print('Press any key to continue') cv2.waitKey(0) cv2.destroyAllWindows() print('Done!')
def main(): print('Initializing main function') # Loading base points point_1 = [2, 2, 1] point_2 = [0, 3, 1] point_3 = [2, 0, 1] point_4 = [1, 0, 1] # Ignore protected warnings point_g1 = ClassDescriptors._guess_leg_point(point_1, point_2) point_g2 = ClassDescriptors._guess_leg_point(point_3, point_4) print('Point g1: {0} - Point g2: {1}'.format(point_g1, point_g2)) print('Done!')
def get_people_diff(cls, person1: 'ClassPeopleReId', person2: 'ClassPeopleReId'): # Comparing people between list diff_upper = ClassUtils.get_color_diff_rgb(person1.color_upper, person2.color_upper) diff_lower = ClassUtils.get_color_diff_rgb(person1.color_lower, person2.color_lower) diff_colors_1 = ClassUtils.get_color_diff_rgb(person1.color_upper, person1.color_lower) diff_colors_2 = ClassUtils.get_color_diff_rgb(person2.color_upper, person2.color_lower) diff_colors = math.fabs(diff_colors_1 - diff_colors_2) diff_k_means = ClassDescriptors.get_kmeans_diff( person1.hist_pose, person2.hist_pose) distance = cls.get_person_distance(person1, person2) return_data = { 'diffUpper': diff_upper, 'diffLower': diff_lower, 'diffColors': diff_colors, 'distance': distance, 'diffKMeans': diff_k_means } return return_data
def get_poses_seq(folder: str, instance_nn: ClassNN, instance_pose: ClassOpenPose, only_json=False): # List all folders list_files = [] for file in os.listdir(folder): list_files.append(os.path.join(folder, file)) # Sorting elements list_files.sort() # Get elements list_desc = list() for path in list_files: ext = ClassUtils.get_filename_extension(path) if only_json: if ext != '.json': print('Ignoring file: {0}'.format(path)) continue with open(path, 'r') as file: person_arr_str = file.read() person_arr = json.loads(person_arr_str) else: if ext != '.jpg': print('Ignoring file: {0}'.format(path)) continue print('Processing file {0}'.format(path)) image = cv2.imread(path) arr = instance_pose.recognize_image(image) arr_pass = [] for person_arr in arr: if ClassUtils.check_vector_integrity_part( person_arr, min_score): arr_pass.append(person_arr) if len(arr_pass) != 1: print('Ignoring file {0} - Len arr_pass: {1}'.format( path, len(arr_pass))) continue person_arr = arr_pass[0] result_desc = ClassDescriptors.get_person_descriptors( person_arr, min_score) list_desc.append(result_desc['fullDesc']) list_desc_np = np.asarray(list_desc, np.float) results = instance_nn.predict_model_array(list_desc_np) list_classes = [] for result in results: list_classes.append(result['classes']) return list_classes
def generate_descriptors(person_array, image, processed_img, min_score): results = ClassDescriptors.get_person_descriptors(person_array, min_score) print('Results: {0}'.format(results)) # Generate color histograms hists = ClassDescriptors.get_color_histograms(person_array, min_score, image, decode_img=False) red_hist_cum = hists[0] green_hist_cum = hists[1] blue_hist_cum = hists[2] cv2.imshow('main_window', processed_img) cv2.waitKey(0) plot_histograms(red_hist_cum) print('Done!')
def process_lab_adj(): print('Process lab adjustment') # Loading instances instance_pose = ClassOpenPose() # Loading images image1, image2, pose1, pose2 = ClassDescriptors.load_images_comparision( instance_pose, min_score) # Performing color comparision upper1, lower1 = ClassDescriptors.process_colors_person(pose1, min_score, image1, decode_img=False) upper2, lower2 = ClassDescriptors.process_colors_person(pose2, min_score, image2, decode_img=False) # Performing custom comparison first diff_upper = ClassUtils.get_color_diff_rgb(upper1, upper2) diff_lower = ClassUtils.get_color_diff_rgb(lower1, lower2) print('Diff upper: {0}'.format(diff_upper)) print('Diff lower: {0}'.format(diff_lower)) # Performing lab conversion and equal L values diff_upper_lum = ClassUtils.get_color_diff_rgb_lum(upper1, upper2) diff_lower_lum = ClassUtils.get_color_diff_rgb_lum(lower1, lower2) print('Diff upper lum: {0}'.format(diff_upper_lum)) print('Diff lower lum: {0}'.format(diff_lower_lum)) # Showing images cv2.namedWindow('main_window', cv2.WND_PROP_AUTOSIZE) cv2.imshow('main_window', np.hstack((image1, image2))) cv2.waitKey(0) print('Done!')
def main(): print('Initializing main function') Tk().withdraw() # Loading instances instance_pose = ClassOpenPose() # Loading filename init_dir = '/home/mauricio/Pictures/TestPlumb' options = {'initialdir': init_dir} filename = askopenfilename(**options) if not filename: filename = '/home/mauricio/Pictures/419.jpg' print('Filename: {0}'.format(filename)) image = cv2.imread(filename) arr = instance_pose.recognize_image(image) valid_arr = list() for person_arr in arr: if ClassUtils.check_vector_integrity_pos(person_arr, min_score): valid_arr.append(person_arr) if len(valid_arr) != 1: raise Exception('Invalid len for arr: {0}'.format(len(valid_arr))) person_arr = valid_arr[0] ClassDescriptors.draw_pose(image, person_arr, min_score) cv2.namedWindow('main_window', cv2.WINDOW_AUTOSIZE) cv2.imshow('main_window', image) print_distances(person_arr) cv2.waitKey() cv2.destroyAllWindows() print('Done!')
def test_view_histogram(): print('Test view histogram') # Loading instances instance_pose = ClassOpenPose() # Loading images image1, _, pose1, _ = ClassDescriptors.load_images_comparision( instance_pose, min_score, load_one_img=True) # Drawing poses ClassDescriptors.draw_pose(image1, pose1, min_score) # Showing image cv2.namedWindow('main_window', cv2.WND_PROP_AUTOSIZE) cv2.imshow('main_window', image1) print('Press a key to continue') cv2.waitKey(0) cv2.destroyAllWindows() print('Done!')
def re_process_folder(): print('Init folder processing') init_dir = '/home/mauricio/Pictures/BTF' options = {'initialdir': init_dir} folder = filedialog.askdirectory(**options) if folder is None: raise Exception('Folder not selected!') files = os.listdir(folder) for file in files: full_path = os.path.join(folder, file) ext = ClassUtils.get_filename_extension(full_path) if ext == '.jpg': print('Processing file: {0}'.format(full_path)) file_json = ClassUtils.get_filename_no_extension( full_path) + '.json' with open(file_json, 'r') as f: json_txt = f.read() json_data = json.loads(json_txt) if 'vectors' in json_data: vectors = json_data['vectors'] elif 'vector' in json_data: vectors = json_data['vector'] else: raise Exception('Vector not found!') img_cv = cv2.imread(full_path) param = ClassDescriptors.get_person_descriptors(vectors, min_score, image=img_cv, decode_img=False) with open(file_json, 'w') as f: f.write(json.dumps(param, indent=2)) print('Done!')
def main(): print('Initializing main function') Tk().withdraw() # Selection init_dir = '/home/mauricio/Pictures/CNN' if platform == 'win32': init_dir = 'C:\\SharedFTP' # Base folder base_folder = '/home/mauricio/Pictures/BasePoses' if platform == 'win32': base_folder = 'C:\\SharedFTP\\BaseFolder' options = {'initialdir': init_dir} filename = askopenfilename(**options) if not filename: raise Exception('Filename not selected!') print('Selected filename: {0}'.format(filename)) with open(filename, 'r') as f: json_str = f.read() list_poses = json.loads(json_str)['listPoses'] cv2.namedWindow('main_window', cv2.WINDOW_AUTOSIZE) index = 0 while True: elem = list_poses[index] image = np.zeros((480, 640, 3), dtype=np.uint8) image.fill(255) vectors = elem['vectors'] pose_guid = elem['poseGuid'] min_score = 0.05 ClassDescriptors.draw_pose(image, vectors, min_score) cv2.imshow('main_window', image) print( 'Press esc to quit. S to save image. Left arrow to go left. Right arrow to go right.' ) key = cv2.waitKey(0) if key != -1: print('Key pressed: {0}'.format(key)) if key == 27: break elif key == 52: index -= 1 if index < 0: index = 0 elif key == 54: index += 1 if index > len(list_poses) - 1: index = len(list_poses) - 1 elif key == 115: # Saving image image_path = os.path.join(base_folder, pose_guid + '.jpg') data_path = os.path.join(base_folder, pose_guid + '.json') # Checking if directory exists if not os.path.exists(base_folder): os.makedirs(base_folder) cv2.imwrite(image_path, image) with open(data_path, 'w') as f: f.write(json.dumps(elem)) print('Image saved: {0}'.format(data_path)) index += 1 elif key == 97: # Loading image again options = {'initialdir': init_dir} filename = askopenfilename(**options) if not filename: raise Exception('Filename not selected!') print('Selected filename: {0}'.format(filename)) with open(filename, 'r') as f: json_str = f.read() list_poses = json.loads(json_str)['listPoses'] index = 0 print('Loading folder: {0}') cv2.destroyAllWindows() print('Done!')
def main(): Tk().withdraw() instance_pose = ClassOpenPose() print('Initializing main function') list_files1 = os.listdir(base1_folder) hists_1 = read_hists(base1_folder, list_files1) list_files2 = os.listdir(base2_folder) hists_2 = read_hists(base2_folder, list_files2) cum_hists_1 = ClassDescriptors.get_cumulative_hists(hists_1) cum_hists_2 = ClassDescriptors.get_cumulative_hists(hists_2) # Processing img filename1 = '/home/mauricio/Pictures/Poses/bend_left/636453039344460032_1.jpg' filename2 = '/home/mauricio/Pictures/Poses/left_walk/636550795366450048_420.jpg' init_dir = '/home/mauricio/Pictures' options = {'initialdir': init_dir} filename1 = askopenfilename(**options) if not filename1: filename1 = '/home/mauricio/Pictures/2_1.jpg' ext1 = os.path.splitext(filename1)[1] if ext1 != '.jpg' and ext1 != '.jpeg': raise Exception('Extension1 is not jpg or jpeg') print('Loading filename 2') filename2 = askopenfilename(**options) if not filename2: filename2 = '/home/mauricio/Pictures/2_2.jpg' ext2 = os.path.splitext(filename2)[1] if ext2 != '.jpg' and ext2 != '.jpeg': raise Exception('Extension2 is not jpg or jpeg') image1 = cv2.imread(filename1) if image1 is None: raise Exception('Invalid image in filename {0}'.format(filename1)) image2 = cv2.imread(filename2) if image2 is None: raise Exception('Invalid image in filename {0}'.format(filename2)) is_json = True new_file_1 = filename1.replace('.jpeg', '.json') new_file_1 = new_file_1.replace('.jpg', '.json') new_file_2 = filename2.replace('.jpeg', '.json') new_file_2 = new_file_2.replace('.jpg', '.json') if not os.path.exists(new_file_1): print('File not found: {0}'.format(new_file_1)) is_json = False if not os.path.exists(new_file_2): print('File not found: {0}'.format(new_file_2)) is_json = False if not is_json: poses1 = instance_pose.recognize_image(image1) poses2 = instance_pose.recognize_image(image2) if len(poses1) != 1: raise Exception('Invalid len for pose1: {0}'.format(len(poses1))) if len(poses2) != 1: raise Exception('Invalid len for pose2: {0}'.format(len(poses2))) if not ClassUtils.check_vector_integrity_pos(poses1[0], min_score): raise Exception('Pose 1 not valid') if not ClassUtils.check_vector_integrity_pos(poses2[0], min_score): raise Exception('Pose 2 not valid') pose1 = poses1[0] pose2 = poses2[0] else: with open(new_file_1, 'r') as f: obj_json1 = json.loads(f.read()) with open(new_file_2, 'r') as f: obj_json2 = json.loads(f.read()) pose1 = obj_json1['vector'] pose2 = obj_json2['vector'] if not ClassUtils.check_vector_integrity_pos(pose1, min_score): raise Exception('Pose 1 not valid') if not ClassUtils.check_vector_integrity_pos(pose2, min_score): raise Exception('Pose 2 not valid') # Plotting # plot_histograms(hists1) # Showing first images without transformation cv2.namedWindow('main_window', cv2.WINDOW_AUTOSIZE) upper1, lower1 = ClassDescriptors.process_colors_person(pose1, min_score, image1, decode_img=False) upper2, lower2 = ClassDescriptors.process_colors_person(pose2, min_score, image2, decode_img=False) print('Upper1 {0} - Lower1 {0}'.format(upper1, lower1)) print('Upper2 {0} - Lower2 {0}'.format(upper2, lower2)) print('Diff1: {0}'.format(ClassUtils.get_color_diff_rgb(upper1, upper2))) print('Diff2: {0}'.format(ClassUtils.get_color_diff_rgb(lower1, lower2))) cv2.imshow('main_window', np.hstack((image1, image2))) print('Press any key to continue') cv2.waitKey(0) # Perform image transformation image_tr = ClassDescriptors.transform_image(image2, cum_hists_2, cum_hists_1) upper1, lower1 = ClassDescriptors.process_colors_person(pose1, min_score, image1, decode_img=False) upper2, lower2 = ClassDescriptors.process_colors_person(pose2, min_score, image_tr, decode_img=False) print('Diff1: {0}'.format(ClassUtils.get_color_diff_rgb(upper1, upper2))) print('Diff2: {0}'.format(ClassUtils.get_color_diff_rgb(lower1, lower2))) cv2.imshow('main_window', np.hstack((image1, image_tr))) print('Press any key to continue') cv2.waitKey(0) cv2.destroyAllWindows() print('Done!')
def main(): print('Initializing main function') # Withdrawing tkinter interface Tk().withdraw() # Loading elements from list init_dir = ClassUtils.activity_base_path options = { 'initialdir': init_dir, 'filetypes': (("JSON Files", "*.json"), ("All files", "*.*")) } filename = askopenfilename(**options) if filename is None: raise Exception('Filename not selected!!!') print('Filename selected: {0}'.format(filename)) # Redrawing trajectory # Re-scale trajectory to known points min_x = -900 max_x = 1200 min_y = -900 max_y = 1200 delta_x = max_x - min_x delta_y = max_y - min_y width_plane = 800 height_plane = 800 img_plane = np.zeros((height_plane, width_plane, 3), np.uint8) # Blank image img_plane[:, :] = (255, 255, 255) # Opening filename with open(filename, 'r') as f: json_txt = f.read() json_data = json.loads(json_txt) # Iterating over data selection - list poses list_poses = json_data["listPoses"] list_rp, list_action_poses = ClassDescriptors.get_moving_action_poses( list_poses) # Only for drawing! for i in range(1, len(list_poses)): pose1 = list_poses[i - 1] pose2 = list_poses[i] # Extracting position from pose global_pos1 = pose1['globalPosition'] global_pos2 = pose2['globalPosition'] # Transforming points pt_plane1 = transform_point(global_pos1, min_x, min_y, width_plane, height_plane, delta_x, delta_y) pt_plane2 = transform_point(global_pos2, min_x, min_y, width_plane, height_plane, delta_x, delta_y) # Draw line using OpenCV library thickness = 3 cv2.line(img_plane, pt_plane1, pt_plane2, (0, 0, 0), thickness) # Now that works, detect loitering using Ko Method # Link: http://ijssst.info/Vol-15/No-2/data/3251a254.pdf # Calculating distance if i == 0: rect_rad = 6 # Draw RP point into image pt1 = pt_plane1[0] - rect_rad, pt_plane1[1] - rect_rad pt2 = pt_plane1[0] + rect_rad, pt_plane1[1] + rect_rad cv2.rectangle(img_plane, pt1, pt2, (0, 0, 255), -1) # Check if index is in point_rp is_rp = False for rp in list_rp: if 'index' not in rp: print('Hello!') if rp['index'] == i: is_rp = True break if is_rp: rect_rad = 6 pt1 = pt_plane2[0] - rect_rad, pt_plane2[1] - rect_rad pt2 = pt_plane2[0] + rect_rad, pt_plane2[1] + rect_rad cv2.rectangle(img_plane, pt1, pt2, (0, 0, 255), -1) else: rect_rad = 3 pt1 = pt_plane2[0] - rect_rad, pt_plane2[1] - rect_rad pt2 = pt_plane2[0] + rect_rad, pt_plane2[1] + rect_rad cv2.rectangle(img_plane, pt1, pt2, (0, 255, 0), -1) print('Total trajectories: {0}'.format(len(list_action_poses))) # Showing image result cv2.namedWindow('main_window', cv2.WINDOW_AUTOSIZE) cv2.imshow('main_window', img_plane) print('Image Loaded! Press a key to continue!') cv2.waitKey() print('Done')
def process_test_color_eq(): print('Processing color eq') # Loading instances instance_pose = ClassOpenPose() im1, im2, pose1, pose2 = ClassDescriptors.load_images_comparision( instance_pose, min_score) upper1, lower1 = ClassDescriptors.process_colors_person(pose1, min_score, im1, decode_img=False) upper2, lower2 = ClassDescriptors.process_colors_person(pose2, min_score, im2, decode_img=False) img_size = 100 image1 = np.zeros((img_size, img_size, 3), np.uint8) image2 = np.zeros((img_size, img_size, 3), np.uint8) image3 = np.zeros((img_size, img_size, 3), np.uint8) image4 = np.zeros((img_size, img_size, 3), np.uint8) # Generating elements in list cv2.namedWindow('aux_window', cv2.WND_PROP_AUTOSIZE) cv2.imshow('aux_window', np.hstack((im1, im2))) # BGR format -> Generating image1[:, :] = (upper1[2], upper1[1], upper1[0]) image2[:, :] = (lower1[2], lower1[1], lower1[0]) image3[:, :] = (upper2[2], upper2[1], upper2[0]) image4[:, :] = (lower2[2], lower2[1], lower2[0]) # Performing custom comparison first diff_upper = ClassUtils.get_color_diff_rgb(upper1, upper2) diff_lower = ClassUtils.get_color_diff_rgb(lower1, lower2) print('Diff upper: {0}'.format(diff_upper)) print('Diff lower: {0}'.format(diff_lower)) cv2.namedWindow('main_window', cv2.WND_PROP_AUTOSIZE) cv2.imshow( 'main_window', np.hstack((np.vstack((image1, image2)), np.vstack((image3, image4))))) cv2.waitKey(0) # Performing lab conversion and equal L values diff_upper_lum = ClassUtils.get_color_diff_rgb_lum(upper1, upper2) diff_lower_lum = ClassUtils.get_color_diff_rgb_lum(lower1, lower2) # Transform point upper2_eq = ClassUtils.eq_lum_rgb_colors(upper1, upper2) lower2_eq = ClassUtils.eq_lum_rgb_colors(lower1, lower2) print('Diff upper lum: {0}'.format(diff_upper_lum)) print('Diff lower lum: {0}'.format(diff_lower_lum)) image3[:, :] = (upper2_eq[2], upper2_eq[1], upper2_eq[0]) image4[:, :] = (lower2_eq[2], lower2_eq[1], lower2_eq[0]) # Showing again image cv2.imshow( 'main_window', np.hstack((np.vstack((image1, image2)), np.vstack((image3, image4))))) cv2.waitKey(0) # Destroying all windows cv2.destroyAllWindows() print('Done!')
def test_color_compare_hist(perform_eq=False): print('Test color comparision') print('Loading image comparision') # Loading instances instance_pose = ClassOpenPose() ignore_json_color = False if perform_eq: ignore_json_color = True # Avoid to open two prompts obj_img = ClassDescriptors.load_images_comparision_ext( instance_pose, min_score, load_one_img=True, perform_eq=perform_eq, ignore_json_color=ignore_json_color) # Extract color comparision from image hist1 = obj_img['listPoints1'] # Generating examples folder list_process = list() for root, _, files in os.walk(EXAMPLES_FOLDER): for file in files: full_path = os.path.join(root, file) extension = ClassUtils.get_filename_extension(full_path) if extension == '.jpg': list_process.append(full_path) # Sorting list list_process.sort() """ list_result = list() score_max_pt = -1 """ for full_path in list_process: print('Processing file: {0}'.format(full_path)) json_path = full_path.replace('.jpg', '.json') with open(json_path, 'r') as f: obj_json = json.loads(f.read()) image2 = cv2.imread(full_path) if perform_eq: image2 = ClassUtils.equalize_hist(image2) if 'vector' in obj_json: pose2 = obj_json['vector'] elif 'vectors' in obj_json: pose2 = obj_json['vectors'] else: raise Exception('Invalid vector property for vector custom') hist2 = ClassDescriptors.get_points_by_pose(image2, pose2, min_score) diff = ClassDescriptors.get_kmeans_diff(hist1, hist2) # Getting mean y from image 2 - discarding purposes pt1, pt2 = ClassUtils.get_rectangle_bounds(pose2, min_score) image2_crop = image2[pt1[1]:pt2[1], pt1[0]:pt2[0]] image2_ycc = cv2.cvtColor(image2_crop, cv2.COLOR_BGR2YCrCb) mean_y = np.mean(image2_ycc[:, :, 0]) print('Diff color: {0} - Mean y: {1}'.format(diff, mean_y)) """ list_result.sort(key=lambda x: x['score']) print('Printing list result') print(list_result) print('min_score: {0}'.format(score_max_pt)) """ print('Done!')
def convert_video_mjpegx(cls, file_path: str, cam_number: str, instance_nn_pose: ClassNN = None): extension = os.path.splitext(file_path)[1] if ClassUtils.cam_calib_exists(cam_number): calib_params = ClassUtils.load_cam_calib_params(cam_number) else: print('Warning: Calib params not found for camera {0}'.format( cam_number)) calib_params = None if extension != '.mjpegx': raise Exception('file_path must have extension .mjpegx') print('Reading image') output_video = file_path + '_1' images = ClassMjpegReader.process_video_mjpegx(file_path) print('Len images: ' + str(len(images))) with open(output_video, 'wb') as newFile: counter = 0 for elem in images: # Avoid previous conversion -> only reading image = elem[0] ticks = elem[1] json_dict = elem[2] vectors = json_dict['vectors'] # Avoid over processing - Only process images with poses image_cv = None if len(vectors) != 0: image_np = np.frombuffer(image, dtype=np.uint8) image_cv = cv2.imdecode(image_np, cv2.IMREAD_ANYCOLOR) params = list() for vector in vectors: # Check if vector is bodypart 25 # Avoid confusions with COCO processing if len(vector) != 25: raise Exception( 'Vector is not bodypart 25 - File: {0} Len Vectors: {1}' .format(file_path, len(vector))) params.append( ClassDescriptors.get_person_descriptors( vector, cls.min_percent, cam_number=cam_number, image=image_cv, calib_params=calib_params, decode_img=False, instance_nn_pose=instance_nn_pose)) # Save object globally # CamNumber new_json_dict = { 'vectors': vectors, 'params': params, 'camNumber': cam_number } # Write in new file - New json dict ClassUtils.write_in_file(newFile, ticks, image, new_json_dict) counter += 1 if counter % 100 == 0: print('Counter: ' + str(counter)) if calib_params is None: print('Warning: Calib params not found for camera {0}'. format(cam_number)) # Delete old os.remove(file_path) # Naming new os.rename(output_video, file_path)
def convert_video_mjpegx_reid(cls, file_path: str, list_people_reid=None, save_img=False, option=None): extension = os.path.splitext(file_path)[1] if extension != '.mjpegx': raise Exception('file_path must have extension .mjpegx') print('Reading image') output_video = file_path + '_1' images = ClassMjpegReader.process_video_mjpegx(file_path) print('Len images: ' + str(len(images))) with open(output_video, 'wb') as newFile: counter = 0 for elem in images: # Avoid previous conversion -> only reading image = elem[0] ticks = elem[1] json_dict = elem[2] params = json_dict['params'] # Update elements for param in params: found = False for person_reid in list_people_reid: for item in person_reid.list_poses: if item['poseGuid'] == param['poseGuid']: param['personGuid'] = person_reid.person_guid found = True break if found: # Saving image with pose identified if save_img: cnn_folder = ClassUtils.cnn_folder if option is not None: cnn_folder = os.path.join( cnn_folder, option) path_img = os.path.join( cnn_folder, param['personGuid']) if not os.path.exists(path_img): os.makedirs(path_img) filename = os.path.join( path_img, '{0}.jpg'.format(ticks)) image_arr = np.frombuffer(image, dtype=np.uint8) image_cv = cv2.imdecode( image_arr, cv2.IMREAD_ANYCOLOR) ClassDescriptors.draw_pose( image_cv, param['vectors'], cls.min_percent, param['keyPose']) cv2.imwrite(filename, image_cv) break if not found: param['personGuid'] = '' # Saving data in json dict ClassUtils.write_in_file(newFile, ticks, image, json_dict) counter += 1 if counter % 100 == 0: print('Counter: ' + str(counter)) # Delete old os.remove(file_path) # Naming new os.rename(output_video, file_path)
def save_list_poses_action(filename, list_poses_action, zone_data): folder = os.path.dirname(filename) for index, list_poses in enumerate(list_poses_action): path_actions = os.path.join(folder, 'action_' + str(index)) if os.path.exists(path_actions): print('Path action exists. Taking next: {0}'.format(path_actions)) continue if not os.path.exists(path_actions): os.makedirs(path_actions) moving = list_poses['moving'] in_zone = list_poses['inZone'] if moving: if in_zone: color_back = (200, 255, 255) else: color_back = (255, 255, 255) else: if in_zone: color_back = (255, 200, 255) else: color_back = (255, 255, 200) for pose in list_poses['listPoses']: transformed_points = pose['transformedPoints'] global_position = pose['globalPosition'] key_pose = pose['keyPose'] ticks = pose['ticks'] re_scale_factor = 100 scaled_points = ClassDescriptors.re_scale_pose_factor( transformed_points, re_scale_factor, min_score) person_array = list() person_array.append([0, 0, 0]) person_array.append([0, 0, 1]) for point in scaled_points: person_array.append([point[0], point[1], 1]) for _ in range(10): person_array.append([0, 0, 0]) local_position = ClassDescriptors.get_local_position_point( person_array, min_score, key_pose) # Draw global position min_x = -200 max_x = 1000 min_y = -900 max_y = 1200 delta_x = max_x - min_x delta_y = max_y - min_y width_plane = 1000 height_plane = 500 width_rect = 10 img_plane = np.zeros((height_plane, width_plane, 3), np.uint8) new_person_array = list() # Height plane must be set into list pt_plane_x = int( (global_position[0] - min_x) * width_plane / delta_x) pt_plane_y = height_plane - int( (global_position[1] - min_y) * height_plane / delta_y) pt1 = (pt_plane_x - width_rect, pt_plane_y - width_rect) pt2 = (pt_plane_x + width_rect, pt_plane_y + width_rect) delta_x_pos = pt_plane_x - local_position[0] delta_y_pos = pt_plane_y - local_position[1] # Blank image img_plane[:, :] = color_back # Transform local vector coordinates for point in person_array: if ClassUtils.check_point_integrity(point, min_score): new_person_array.append( [point[0] + delta_x_pos, point[1] + delta_y_pos, 1]) else: new_person_array.append(point) # Draw pose ClassDescriptors.draw_pose(img_plane, new_person_array, min_score, key_pose) # Draw points zone list_rect_points = zone_data['listRectanglePoints'] for point in list_rect_points: pt1_x = int((point[0][0] - min_x) * width_plane / delta_x) pt1_y = height_plane - int( (point[0][1] - min_y) * height_plane / delta_y) pt2_x = int((point[1][0] - min_x) * width_plane / delta_x) pt2_y = height_plane - int( (point[1][1] - min_y) * height_plane / delta_y) cv2.rectangle(img_plane, (pt1_x, pt1_y), (pt2_x, pt2_y), (0, 0, 255), thickness=3) # Fill rectangle cv2.rectangle(img_plane, pt1, pt2, (255, 0, 255), thickness=-1) # Save global position info font = cv2.FONT_HERSHEY_SIMPLEX font_scale = 0.6 font_color = (0, 0, 0) line_type = 2 cv2.putText( img_plane, '({0:.2f}, {1:.2f})'.format(global_position[0], global_position[1]), pt2, font, font_scale, font_color, line_type) filename = os.path.join(path_actions, '{0}_g.jpg'.format(ticks)) cv2.imwrite(filename, img_plane)
def process_list_partial(filename, zone_data, idx_cls): # Loading json data from filename with open(filename, 'r') as f: person_txt = f.read() person = json.loads(person_txt) list_poses = person['listPoses'] # Create partial list of ele moving = True index = 0 num_poses_future = 5 min_distance_x = 100 min_distance_y = 100 list_poses_action = list() list_poses_partial = list() while index < len(list_poses): pose = list_poses[index] remaining = len(list_poses) - index - 1 pos = pose['globalPosition'] if moving: valid = True if remaining >= num_poses_future: count = 0 count_min = 0 for i in range(index + 1, index + num_poses_future): count += 1 pos_future = list_poses[index + num_poses_future]['globalPosition'] distance_x = math.fabs(pos[0] - pos_future[0]) distance_y = math.fabs(pos[1] - pos_future[1]) if distance_x < min_distance_x and distance_y < min_distance_y: count_min += 1 # All points must be in a range if count == count_min: valid = False if not valid: if len(list_poses_partial) != 0: # Check total moving changes # Using movement variations _, list_action_mov = ClassDescriptors.get_moving_action_poses( list_poses_partial) print('Total action moving: {0}'.format( len(list_action_mov))) for idx_act, action_mov in enumerate(list_action_mov): if idx_act == len(action_mov) - 1: # Take last action as num_pose_future position final_pos = list_poses[ index + num_poses_future]['globalPosition'] else: # Take final pos as last item final_pos = action_mov[-1]['globalPosition'] initial_pos = action_mov[0]['globalPosition'] # Get if action ends in zone # Except for walk, loitering actions if idx_cls == 2 or idx_cls == 6: in_zone = False in_zone_before = False else: in_zone = is_point_in_zone(final_pos, zone_data) # Get if action init in zone in_zone_before = is_point_in_zone( initial_pos, zone_data) # Saving list_poses_action.append({ 'moving': moving, 'listPoses': copy.deepcopy(action_mov), 'finalPos': final_pos, 'inZone': in_zone, 'inZoneBefore': in_zone_before }) list_poses_partial.clear() moving = False list_poses_partial.append(pose) else: # Generating elements into list valid = True if remaining >= num_poses_future: for i in range(index + 1, index + num_poses_future): pos_future = list_poses[index + num_poses_future]['globalPosition'] distance_x = math.fabs(pos[0] - pos_future[0]) distance_y = math.fabs(pos[1] - pos_future[1]) # Inverse # If there is some point with greater distance! if distance_x >= min_distance_x or distance_y >= min_distance_y: valid = False break list_poses_partial.append(pose) if not valid: final_pos = list_poses[index]['globalPosition'] in_zone = is_point_in_zone(final_pos, zone_data) # New pose list for i in range(num_poses_future): list_poses_partial.append(list_poses[index + i]) list_poses_action.append({ 'moving': moving, 'listPoses': copy.deepcopy(list_poses_partial), 'finalPos': final_pos, 'inZone': in_zone, 'inZoneBefore': False # In zone before does not apply }) list_poses_partial.clear() index += num_poses_future moving = True # Iterator! index += 1 if len(list_poses_partial) != 0: final_pos = list_poses_partial[-1]['globalPosition'] initial_pos = list_poses_partial[0]['globalPosition'] if moving: if idx_cls == 2 or idx_cls == 6: in_zone = False in_zone_before = False else: in_zone = is_point_in_zone(final_pos, zone_data) in_zone_before = is_point_in_zone(initial_pos, zone_data) _, list_action_mov = ClassDescriptors.get_moving_action_poses( list_poses_partial) for action_mov in list_action_mov: list_poses_action.append({ 'moving': moving, 'listPoses': copy.deepcopy(action_mov), 'finalPos': final_pos, 'inZone': in_zone, 'inZoneBefore': in_zone_before }) else: in_zone = is_point_in_zone(final_pos, zone_data) # Saving elements in partial format list_poses_action.append({ 'moving': moving, 'listPoses': copy.deepcopy(list_poses_partial), 'finalPos': final_pos, 'inZone': in_zone, 'inZoneBefore': False # In zone before does not apply }) list_poses_partial.clear() # Generating elements into list person_action = { 'personGuid': person['personGuid'], 'listPosesAction': list_poses_action } new_filename = ClassUtils.change_ext_training(filename, 'partialdata') action_txt = json.dumps(person_action, indent=4) with open(new_filename, 'w') as f: f.write(action_txt) # Saving list poses action for debugging save_list_poses_action(filename, list_poses_action, zone_data) print('Done!')
def get_sets_folder(base_folder, label, instance_pose: ClassOpenPose): list_files = os.listdir(base_folder) # Work 70, 30 total_files = len(list_files) training = int(total_files * 70 / 100) tr_features = list() tr_labels = list() eval_features = list() eval_labels = list() for index, file in enumerate(list_files): full_name = os.path.join(base_folder, file) image = cv2.imread(full_name) if image is None: raise Exception('Error reading image: {0}'.format(full_name)) print('Processing image: {0}'.format(full_name)) arr = instance_pose.recognize_image(image) # Selecting array for list of vectors person_array = [] if len(arr) == 0: raise Exception('Invalid len for image {0}: {1}'.format( full_name, len(arr))) elif len(arr) == 1: person_array = arr[0] integrity = ClassUtils.check_vector_integrity_part( person_array, min_score) if not integrity: raise Exception( 'Invalid integrity for points in image: {0}'.format( full_name)) else: found = False for arr_index in arr: integrity = ClassUtils.check_vector_integrity_part( arr_index, min_score) if integrity: found = True person_array = arr_index break if not found: raise Exception( 'Cant find a valid person array for image {0}'.format( full_name)) results = ClassDescriptors.get_person_descriptors( person_array, min_score) if index < training: tr_features.append(results['angles']) tr_labels.append(label) else: eval_features.append(results['angles']) eval_labels.append(label) return tr_features, tr_labels, eval_features, eval_labels
def main(): print('Initializing main function') # Initializing instances instance_pose = ClassOpenPose() instance_net = ClassNN.load_from_params(model_dir) # Withdrawing list Tk().withdraw() # Select directory to process init_dir = '/home/mauricio/CNN/Images' options = {'initialdir': init_dir} dir_name = filedialog.askdirectory(**options) if not dir_name: print('Directory not selected') else: # Loading images list_files = os.listdir(dir_name) list_files.sort() desc_list = list() for file in list_files: full_path = os.path.join(dir_name, file) print('Processing image {0}'.format(full_path)) image = cv2.imread(full_path) arr = instance_pose.recognize_image(image) arr_pass = list() for person_arr in arr: if ClassUtils.check_vector_integrity_part( person_arr, min_pose_score): arr_pass.append(person_arr) if len(arr_pass) != 1: print('Invalid len {0} for image {1}'.format( len(arr_pass), full_path)) continue else: result_des = ClassDescriptors.get_person_descriptors( arr_pass[0], min_pose_score) descriptor_arr = result_des['fullDesc'] # Add descriptors to list desc_list.append(descriptor_arr) # Convert to numpy array print('Total poses: {0}'.format(len(desc_list))) # Transform list and predict desc_list_np = np.asarray(desc_list, dtype=np.float) print('ndim pose list: {0}'.format(desc_list_np.ndim)) list_classes = list() predict_results = instance_net.predict_model_array(desc_list_np) for result in predict_results: list_classes.append(result['classes']) print('Predict results: {0}'.format(list_classes)) print('Classes label: {0}'.format(instance_net.label_names)) print('Done!')
def main(): print('Initializing main function') Tk().withdraw() # filename1 = '/home/mauricio/Pictures/Poses/temp/636550787632290048_419.jpg' # filename2 = '/home/mauricio/Pictures/Poses/walk_front/636550801813440000_424.jpg' filename1 = '/home/mauricio/Pictures/Poses/bend_left/636453039344460032_1.jpg' filename2 = '/home/mauricio/Pictures/Poses/left_walk/636550795366450048_420.jpg' print('Select first file') init_dir = '/home/mauricio/Pictures/Poses' options = {'initialdir': init_dir} filename1_tmp = askopenfilename(**options) if filename1_tmp: filename1 = filename1_tmp print('File selected: {0}'.format(filename1)) print('Select second file') filename2_tmp = askopenfilename(**options) if filename2_tmp: filename2 = filename2_tmp print('File selected: {0}'.format(filename2)) img1 = cv2.imread(filename1) img2 = cv2.imread(filename2) instance_pose = ClassOpenPose() vectors1, img_draw1 = instance_pose.recognize_image_tuple(img1) vectors2, img_draw2 = instance_pose.recognize_image_tuple(img2) if len(vectors1) != 1: raise Exception('Invalid len for vector 1') if len(vectors2) != 1: raise Exception('Invalid len for vector 2') person_vector1 = vectors1[0] person_vector2 = vectors2[0] min_percent = 0.05 if not ClassUtils.check_vector_integrity_pos(person_vector1, min_percent): raise Exception('Invalid integrity for vector 1') if not ClassUtils.check_vector_integrity_pos(person_vector2, min_percent): raise Exception('Invalid integrity for vector 2') colors1 = ClassDescriptors.process_colors(vectors1, min_percent, img1, decode_img=False) colors2 = ClassDescriptors.process_colors(vectors2, min_percent, img2, decode_img=False) upper1 = colors1[0][0] lower1 = colors1[1][0] color_diff1 = ClassUtils.get_color_diff_rgb(upper1, lower1) upper2 = colors2[0][0] lower2 = colors2[1][0] color_diff2 = ClassUtils.get_color_diff_rgb(upper2, lower2) diff_upper = ClassUtils.get_color_diff_rgb(upper1, upper2) diff_lower = ClassUtils.get_color_diff_rgb(lower1, lower2) diff_diff = math.fabs(color_diff1 - color_diff2) print('Diff upper: {0}'.format(diff_upper)) print('Diff lower: {0}'.format(diff_lower)) print('Diff diffs: {0}'.format(diff_diff)) cv2.namedWindow('main_window', cv2.WND_PROP_AUTOSIZE) cv2.imshow('main_window', np.hstack((img_draw1, img_draw2))) cv2.waitKey(0) cv2.destroyAllWindows() print('Done!')
def main(): print('Initializing main function') # Prompt for user input cam_number_str = input('Insert camera number to process: ') cam_number = int(cam_number_str) # Open video from opencv cap = cv2.VideoCapture(cam_number) # Initializing open pose distance instance_pose = ClassOpenPose() # Initializing variables model_dir = '/home/mauricio/models/nn_classifier' instance_nn = ClassNN.load_from_params(model_dir) while True: # Capture frame-by-frame ret, frame = cap.read() # Processing frame with openpose arr, frame = instance_pose.recognize_image_tuple(frame) # Check if there is one frame with vector integrity arr_pass = list() min_score = 0.05 # Checking vector integrity for all elements # Verify there is at least one arm and one leg for elem in arr: if ClassUtils.check_vector_integrity_pos(elem, min_score): arr_pass.append(elem) if len(arr_pass) != 1: print('Invalid len for arr_pass: {0}'.format(arr_pass)) else: person_array = arr_pass[0] # Getting person descriptors results = ClassDescriptors.get_person_descriptors( person_array, min_score) # Descriptors data_to_add = results['angles'] data_to_add += ClassUtils.get_flat_list( results['transformedPoints']) data_np = np.asanyarray(data_to_add, dtype=np.float) # Getting result predict result_predict = instance_nn.predict_model(data_np) detected_class = result_predict['classes'] label_class = get_label_name(instance_nn.label_names, detected_class) print('Detected: {0} - Label: {1}'.format(detected_class, label_class)) # Draw text class into image - Evaluation purposes font = cv2.FONT_HERSHEY_SIMPLEX font_scale = 0.6 font_color = (255, 255, 255) line_type = 2 cv2.putText(frame, '{0}'.format(label_class), (0, 0), font, font_scale, font_color, line_type) # Display the resulting frame cv2.imshow('frame', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break # When everything done, release the capture cap.release() cv2.destroyAllWindows() print('Done!')