def pre_process_images(list_folders_scores, recalculate): print('Start pre_processing images') # Loading instances instance_pose = ClassOpenPose() for folder, min_score in list_folders_scores: for file in os.listdir(folder): full_path = os.path.join(folder, file) extension = ClassUtils.get_filename_extension(full_path) if extension != '.jpg': print('Ignoring file {0}'.format(full_path)) continue file_no_ext = ClassUtils.get_filename_no_extension(full_path) arr_file_name = os.path.join(folder, '{0}.json'.format(file_no_ext)) # If image recalculation if not recalculate: if os.path.isfile(arr_file_name): print('File already processed {0}'.format(full_path)) continue # Processing file print('Processing file {0}'.format(full_path)) image = cv2.imread(full_path) arr, img_draw = instance_pose.recognize_image_tuple(image) arr_pass = list() # 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_part(elem, min_score): arr_pass.append(elem) # If there is more than one person with vector integrity if len(arr_pass) != 1: for elem in arr_pass: pt1, pt2 = ClassUtils.get_rectangle_bounds(elem, ClassUtils.MIN_POSE_SCORE) cv2.rectangle(img_draw, pt1, pt2, (0, 0, 255), 3) cv2.namedWindow('main_window') cv2.imshow('main_window', img_draw) print(arr) print(arr_pass) cv2.waitKey(0) cv2.destroyAllWindows() raise Exception('Invalid len: {0} file {1}'.format(len(arr_pass), full_path)) person_arr = arr_pass[0] arr_str = json.dumps(person_arr.tolist()) with open(arr_file_name, 'w') as text_file: text_file.write(arr_str) print('Done!')
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 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') # Initializing instances instance_pose = ClassOpenPose() # Withdrawing tkinter Tk().withdraw() # Loading folder images folder_images = '/home/mauricio/PosesProcessed/folder_images' folder_images_draw = '/home/mauricio/PosesProcessed/folder_images_draw' pose_base_folder = '/home/mauricio/Pictures/PosesNew' # Reading all elements in list list_files = sorted(os.listdir(folder_images_draw)) cv2.namedWindow('main_window', cv2.WND_PROP_AUTOSIZE) index = 0 while True: file = list_files[index] full_path_draw = os.path.join(folder_images_draw, file) image_draw = cv2.imread(full_path_draw) cv2.imshow('main_window', image_draw) key = cv2.waitKey(0) print('Key pressed: {0}'.format(key)) if key == 52: # Left arrow index -= 1 if index < 0: index = 0 elif key == 54: # Right arrow index += 1 if index == len(list_files): index = len(list_files) - 1 elif key == 27: # Esc # Ask are you sure question result = messagebox.askyesno("Exit", "Are you sure to exit?") print(result) if result: break elif key == 115: # Check JSON file image_full_path = os.path.join(folder_images, file) json_path = image_full_path.replace(".jpg", ".json") if not os.path.exists(json_path): # Generating JSON array image = cv2.imread(image_full_path) arr = instance_pose.recognize_image(image).tolist() else: with open(json_path, 'r') as file: arr_str = file.read() arr = json.load(arr_str) arr_pass = [] min_score = 0.05 # Drawing candidates for elem in arr: if ClassUtils.check_vector_integrity_part(elem, min_score): arr_pass.append(elem) if len(arr_pass) == 0: print('Arr pass size equals to zero') continue elif len(arr_pass) == 1: selection_int = 0 person_arr = arr_pass[0] arr_str = json.dumps(person_arr) else: # Draw rectangles for all candidates for index_elem, person_arr in enumerate(arr_pass): pt1, pt2 = ClassUtils.get_rectangle_bounds(person_arr, min_score) cv2.rectangle(image_draw, pt1, pt2, (0, 0, 255), 5) font = cv2.FONT_HERSHEY_SIMPLEX bottom_left_corner = pt2 font_scale = 0.6 font_color = (255, 255, 255) line_type = 2 cv2.putText(image_draw, '{0}'.format(index_elem), bottom_left_corner, font, font_scale, font_color, line_type) while True: print('Select image to put') cv2.imshow('main_window', image_draw) wait_key = cv2.waitKey(0) print('Wait Key: {0}'.format(wait_key)) selection_int = ClassUtils.key_to_number(wait_key) print('Selection: {0}'.format(selection_int)) if 0 <= selection_int < len(arr_pass): break person_arr = arr_pass[selection_int] arr_str = json.dumps(person_arr) # Getting new image using numpy slicing pt1, pt2 = ClassUtils.get_rectangle_bounds(person_arr, min_score) image_draw = image_draw[pt1[1]:pt2[1], pt1[0]:pt2[0]] # Selecting directory after processing image! init_dir = '/home/mauricio/Pictures/PosesNew' options = {'initialdir': init_dir} dir_name = filedialog.askdirectory(**options) if not dir_name: print('Directory not selected') else: new_name = ClassUtils.get_filename_no_extension(file) new_name = "{0}_{1}.{2}".format(new_name, selection_int, 'jpg') new_image_path = os.path.join(dir_name, new_name) new_json_path = os.path.join(dir_name, new_name.replace('.jpg', '.json')) cv2.imwrite(new_image_path, image_draw) with open(new_json_path, 'w') as file: file.write(arr_str) print('File copied from {0} to {1}'.format(full_path_draw, new_image_path)) index += 1 if index == len(list_files): index = len(list_files) - 1 cv2.destroyAllWindows() print('Done!')