| | import os |
| | import json |
| | import cv2 |
| | import numpy as np |
| | import torch |
| | from tqdm import tqdm |
| | import shutil |
| | import argparse |
| |
|
| | def clear_directory(directory_path): |
| | """Clear all files and subdirectories in the specified directory. Create the directory if it does not exist.""" |
| | if not os.path.exists(directory_path): |
| | os.makedirs(directory_path) |
| | else: |
| | for filename in os.listdir(directory_path): |
| | file_path = os.path.join(directory_path, filename) |
| | try: |
| | if os.path.isfile(file_path) or os.path.islink(file_path): |
| | os.unlink(file_path) |
| | elif os.path.isdir(file_path): |
| | shutil.rmtree(file_path) |
| | except Exception as e: |
| | print(f"Failed to delete {file_path}. Reason: {e}") |
| |
|
| | def extract_data_from_json(json_path): |
| | """Extract action data from JSON for visualizing keys.""" |
| | with open(json_path, 'r') as f: |
| | data = json.load(f) |
| | return data["actions"] |
| |
|
| | def process_videos_and_metadata(video_dir, metadata_dir, output_metadata_dir, threshold=0.1, height_threshold=0.1): |
| | |
| | clear_directory(output_metadata_dir) |
| |
|
| | for video_file in tqdm(os.listdir(video_dir)): |
| | if not video_file.endswith('.mp4'): |
| | continue |
| | |
| | video_name = os.path.splitext(video_file)[0] |
| | json_file = os.path.join(metadata_dir, f"{video_name}.json") |
| | output_json_file = os.path.join(output_metadata_dir, f"{video_name}.json") |
| |
|
| | if not os.path.exists(json_file): |
| | print(f"Metadata file for {video_name} not found. Skipping.") |
| | continue |
| | |
| | with open(json_file, 'r') as f: |
| | metadata = json.load(f) |
| | |
| | video_path = os.path.join(video_dir, video_file) |
| | actions = metadata.get('actions', {}) |
| | cap = cv2.VideoCapture(video_path) |
| | if not cap.isOpened(): |
| | print(f"Failed to open video: {video_file}") |
| | continue |
| | |
| | ret, prev_frame = cap.read() |
| | if not ret: |
| | print(f"Failed to read frames from video: {video_file}") |
| | cap.release() |
| | continue |
| |
|
| | |
| | for frame_idx in range(len(actions)): |
| | actions[str(frame_idx)]['collision'] = 0 |
| | actions[str(frame_idx)]['jump_invalid'] = 0 |
| | actions[str(frame_idx)]['delta_pos'] = [0.0, 0.0, 0.0] |
| |
|
| | |
| | for frame_idx in range(1, len(actions)): |
| | current_action = actions[str(frame_idx)] |
| | prev_action = actions.get(str(frame_idx - 1), None) |
| |
|
| | if prev_action: |
| | |
| | delta_pos = np.array(current_action['pos']) - np.array(prev_action['pos']) |
| | current_action['delta_pos'] = delta_pos.tolist() |
| |
|
| | |
| | if current_action.get('scs') == 1 and delta_pos[1] <= height_threshold: |
| | current_action['jump_invalid'] = 1 |
| |
|
| | |
| | if abs(delta_pos[0]) <= threshold and abs(delta_pos[2]) <= threshold: |
| | current_action['collision'] = 1 |
| |
|
| | |
| | jump_sequence_started = False |
| | for frame_idx in range(1, len(actions)): |
| | current_action = actions[str(frame_idx)] |
| |
|
| | if current_action.get('scs') == 1 and current_action['jump_invalid'] == 0: |
| | if jump_sequence_started: |
| | current_action['jump_invalid'] = 1 |
| | else: |
| | jump_sequence_started = True |
| | else: |
| | jump_sequence_started = False |
| |
|
| | metadata['actions'] = actions |
| | with open(output_json_file, 'w') as f: |
| | json.dump(metadata, f, indent=4) |
| | |
| | cap.release() |
| |
|
| | def main(): |
| | |
| | parser = argparse.ArgumentParser(description="Process videos and metadata.") |
| | parser.add_argument('--dir_name', type=str, help="Root directory for the video and metadata files.") |
| | parser.add_argument('--threshold', type=float, default=0.01, help="Threshold for detecting collisions.") |
| | parser.add_argument('--height_threshold', type=float, default=0.01, help="Threshold for jump validity based on height change.") |
| |
|
| | args = parser.parse_args() |
| |
|
| | |
| | dir_name = args.dir_name |
| | threshold = args.threshold |
| | height_threshold = args.height_threshold |
| |
|
| | video_dir = os.path.join(dir_name, "video") |
| | metadata_dir = os.path.join(dir_name, "metadata") |
| | output_metadata_dir = os.path.join(dir_name, "metadata-detection") |
| |
|
| | process_videos_and_metadata(video_dir, metadata_dir, output_metadata_dir, threshold, height_threshold) |
| |
|
| |
|
| | if __name__ == "__main__": |
| | main() |