def calculate_poses(option: Option, nn_classifier: ClassNN, svm_classifier: ClassSVM): print('Calculating poses using nn') # Recalculate all poses and get confidence for classInfo in list_classes: folder = classInfo['folderPath'] for root, _, files in os.walk(folder): for file in files: full_path = os.path.join(root, file) ext = ClassUtils.get_filename_extension(full_path) if '_rawdata' in file and ext == '.json': print('Processing file: {0}'.format(full_path)) with open(full_path, 'r') as f: file_txt = f.read() file_json = json.loads(file_txt) list_poses = file_json['listPoses'] for pose in list_poses: angles = pose['angles'] transformed_points = pose['transformedPoints'] # Using transformed points and angles list_desc = list() list_desc += ClassUtils.get_flat_list(transformed_points) # Convert to numpy list_desc_np = np.asanyarray(list_desc, np.float) if option == Option.NN: result = nn_classifier.predict_model_fast(list_desc_np) else: result = svm_classifier.predict_model(list_desc_np) pose['class'] = int(result['classes']) pose['probabilities'] = result['probabilities'].tolist() # Writing again into file file_txt = json.dumps(file_json, indent=4) new_full_path = ClassUtils.change_ext_training(full_path, '{0}_posedata'.format(option.value)) with open(new_full_path, 'w') as f: f.write(file_txt) # Done print('Done processing elements')
def apply_classifier(option: Option, base_data_1, base_data_2, instance_nn=None, hmm_models=None, accuracy=0, real_accuracy=0): # Apply classification for each item! for item in list_classes: folder = item['folderPath'] for root, _, files in os.walk(folder): for file in files: full_path = os.path.join(root, file) if '{0}_{1}_actiondata'.format(base_data_1, base_data_2) in full_path: with open(full_path, 'r') as f: json_txt = f.read() json_data = json.loads(json_txt) list_actions = json_data['listActions'] if option == Option.HMM: res = predict_data_hmm(list_actions, hmm_models) else: words = get_bow_descriptors(list_actions) res = instance_nn.predict_model_fast(words) obj_to_write = { 'class': int(res['classes']), 'probabilities': res['probabilities'].tolist(), 'modelAccuracy': accuracy, 'realAccuracy': real_accuracy } obj_txt = json.dumps(obj_to_write, indent=4) new_filename = ClassUtils.change_ext_training( full_path, '{0}_ensembledata'.format(option.value)) with open(new_filename, 'w') as f: f.write(obj_txt) # Done Writing print('Done!')
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 process_file_action(full_path, option: Option, instance_model=None, hmm_models=None, accuracy=0): with open(full_path, 'r') as f: json_txt = f.read() json_data = json.loads(json_txt) list_actions = list() list_list_poses = json_data['listPosesAction'] for list_poses_data in list_list_poses: list_poses = list_poses_data['listPoses'] moving = list_poses_data['moving'] in_zone = list_poses_data['inZone'] in_zone_before = list_poses_data['inZoneBefore'] # Descriptor not moving if not moving: if option == Option.NN: descriptor = get_nn_descriptor(list_poses) descriptor_nn = np.asanyarray(descriptor, dtype=np.float) res = instance_model.predict_model_fast(descriptor_nn) elif option == Option.CNN: descriptor = get_cnn_descriptor_pos(list_poses) descriptor_nn = np.asanyarray(descriptor, dtype=np.float) res = instance_model.predict_model_fast(descriptor_nn) else: res = predict_data(hmm_models, list_poses) cls = int(res['classes']) if in_zone: cls += 4 probabilities = res['probabilities'].tolist() list_prob = list() if in_zone: for i in range(4): list_prob.append(0) list_prob += probabilities else: list_prob += probabilities for i in range(4): list_prob.append(0) for i in range(4): list_prob.append(0) data_action = { 'class': cls, 'probabilities': list_prob, 'count': len(list_poses) } else: if not in_zone and not in_zone_before: cls = 8 elif in_zone and not in_zone_before: cls = 9 elif not in_zone and in_zone_before: cls = 10 else: cls = 11 probabilities = list() for i in range(12): if i == cls: probabilities.append(1) else: probabilities.append(0) data_action = { 'class': cls, 'probabilities': probabilities, 'count': len(list_poses) } list_actions.append(data_action) json_data = {'listActions': list_actions, 'accuracy': accuracy} # Saving full action info into list json_txt = json.dumps(json_data, indent=4) new_file_path = ClassUtils.change_ext_training( full_path, '{0}_actiondata'.format(option.value)) with open(new_file_path, 'w') as f: f.write(json_txt)