Upload 5 files
Browse filesFiles changed (5)
- detection.pyAdded+128
- input.mp4Added+3
- mouse.pngAdded
- output.mp4Added+3
- visualize.pyAdded+198
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128import osimport jsonimport cv2import numpy as npimport torchfrom tqdm import tqdmimport shutilimport 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) # Remove file or symbolic link elif os.path.isdir(file_path): shutil.rmtree(file_path) # Remove directory 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 output directories at the start 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
# Initialize default fields for all actions for frame_idx in range(len(actions)): actions[str(frame_idx)]['collision'] = 0 # Initialize single collision flag actions[str(frame_idx)]['jump_invalid'] = 0 actions[str(frame_idx)]['delta_pos'] = [0.0, 0.0, 0.0]
# First pass: mark jumps and collisions for frame_idx in range(1, len(actions)): # Exclude the first frame current_action = actions[str(frame_idx)] prev_action = actions.get(str(frame_idx - 1), None)
if prev_action: # Calculate delta pos delta_pos = np.array(current_action['pos']) - np.array(prev_action['pos']) current_action['delta_pos'] = delta_pos.tolist()
# Mark jump as invalid if height change is too small if current_action.get('scs') == 1 and delta_pos[1] <= height_threshold: current_action['jump_invalid'] = 1
# Mark collision if pos[0] and pos[2] changes are both below the threshold if abs(delta_pos[0]) <= threshold and abs(delta_pos[2]) <= threshold: current_action['collision'] = 1
# Second pass: mark subsequent jumps in a sequence as invalid jump_sequence_started = False for frame_idx in range(1, len(actions)): # Exclude the first frame current_action = actions[str(frame_idx)]
if current_action.get('scs') == 1 and current_action['jump_invalid'] == 0: if jump_sequence_started: # If already in a sequence, mark as invalid current_action['jump_invalid'] = 1 else: # First valid jump in a sequence jump_sequence_started = True else: jump_sequence_started = False # Reset sequence if no jump or invalid jump
metadata['actions'] = actions with open(output_json_file, 'w') as f: json.dump(metadata, f, indent=4) cap.release()
def main(): # Set up argparse to handle command-line arguments 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()
# Get root_name from command line argument 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()123version https://git-lfs.github.com/spec/v1oid sha256:3b15d0cf23ed73c8d51596de85e54db671741f32b06dfd65318ab24f93b99db7size 4022118123version https://git-lfs.github.com/spec/v1oid sha256:10ea25997402993c1ca54e62b5180aa6b2a0757e9c6fbefe74cd8c1f9afa0680size 2784327123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198from operator import indeximport cv2import numpy as npimport osimport subprocess
def parse_config(config): """ 根据配置生成按键数据和鼠标数据 - config: list_actions[i] 的配置 - 返回: key_data 和 mouse_data """ key_data = {} mouse_data = {}
# 解析 Space 按键的帧范围 space_frames = set() if config[-1]: space_frames = set(map(int, config[-1].split()))
# 遍历配置的每一段 for i in range(len(config) - 1): end_frame, action = config[i] w, s, a, d, shift, ctrl, _, mouse_y, mouse_x = map(float, action.split())
# 计算上一段的起始帧 start_frame = 0 if i == 0 else config[i - 1][0] + 1
# 填充帧范围的数据 for frame in range(start_frame, int(end_frame) + 1): # 按键状态 key_data[frame] = { "W": bool(w), "A": bool(a), "S": bool(s), "D": bool(d), "Space": frame in space_frames, "Shift": bool(shift), "Ctrl": bool(ctrl), } # 鼠标位置 if frame == 0: mouse_data[frame] = (320, 176) # 默认初始位置 else: global_scale_factor = 0.4 mouse_scale_x = 15 * global_scale_factor mouse_scale_y = 15 * 4 * global_scale_factor mouse_data[frame] = ( mouse_data[frame-1][0] + mouse_x * mouse_scale_x, # x 坐标累计 mouse_data[frame-1][1] + mouse_y * mouse_scale_y, # y 坐标累计 )
return key_data, mouse_data
# 绘制圆角矩形def draw_rounded_rectangle(image, top_left, bottom_right, color, radius=10, alpha=0.5): overlay = image.copy() x1, y1 = top_left x2, y2 = bottom_right
cv2.rectangle(overlay, (x1 + radius, y1), (x2 - radius, y2), color, -1) cv2.rectangle(overlay, (x1, y1 + radius), (x2, y2 - radius), color, -1)
cv2.ellipse(overlay, (x1 + radius, y1 + radius), (radius, radius), 180, 0, 90, color, -1) cv2.ellipse(overlay, (x2 - radius, y1 + radius), (radius, radius), 270, 0, 90, color, -1) cv2.ellipse(overlay, (x1 + radius, y2 - radius), (radius, radius), 90, 0, 90, color, -1) cv2.ellipse(overlay, (x2 - radius, y2 - radius), (radius, radius), 0, 0, 90, color, -1)
cv2.addWeighted(overlay, alpha, image, 1 - alpha, 0, image)
# 在帧上绘制按键def draw_keys_on_frame(frame, keys, key_size=(80, 50), spacing=20, bottom_margin=30): h, w, _ = frame.shape horison_shift = 90 vertical_shift = -20 horizon_shift_all = 50 key_positions = { "W": (w // 2 - key_size[0] // 2 - horison_shift - horizon_shift_all, h - bottom_margin - key_size[1] * 2 + vertical_shift - 20), "A": (w // 2 - key_size[0] * 2 + 5 - horison_shift - horizon_shift_all, h - bottom_margin - key_size[1] + vertical_shift), "S": (w // 2 - key_size[0] // 2 - horison_shift - horizon_shift_all, h - bottom_margin - key_size[1] + vertical_shift), "D": (w // 2 + key_size[0] - 5 - horison_shift - horizon_shift_all, h - bottom_margin - key_size[1] + vertical_shift), "Space": (w // 2 + key_size[0] * 2 + spacing * 2 - horison_shift - horizon_shift_all, h - bottom_margin - key_size[1] + vertical_shift), "Shift": (w // 2 + key_size[0] * 3 + spacing * 7 - horison_shift - horizon_shift_all, h - bottom_margin - key_size[1] + vertical_shift), "Ctrl": (w // 2 + key_size[0] * 4 + spacing * 12 - horison_shift - horizon_shift_all, h - bottom_margin - key_size[1] + vertical_shift), }
for key, (x, y) in key_positions.items(): is_pressed = keys.get(key, False) top_left = (x, y) if key in ["Space", "Shift", "Ctrl"]: bottom_right = (x + key_size[0]+40, y + key_size[1]) else: bottom_right = (x + key_size[0], y + key_size[1])
color = (0, 255, 0) if is_pressed else (200, 200, 200) alpha = 0.8 if is_pressed else 0.5
draw_rounded_rectangle(frame, top_left, bottom_right, color, radius=10, alpha=alpha)
text_size = cv2.getTextSize(key, cv2.FONT_HERSHEY_SIMPLEX, 0.8, 2)[0] if key in ["Space", "Shift", "Ctrl"]: text_x = x + (key_size[0]+40 - text_size[0]) // 2 else: text_x = x + (key_size[0] - text_size[0]) // 2 text_y = y + (key_size[1] + text_size[1]) // 2 cv2.putText(frame, key, (text_x, text_y), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 0), 2)
# 在帧上叠加鼠标图案def overlay_icon(frame, icon, position, scale=1.0, rotation=0): x, y = position h, w, _ = icon.shape
# 缩放图标 scaled_width = int(w * scale) scaled_height = int(h * scale) icon_resized = cv2.resize(icon, (scaled_width, scaled_height), interpolation=cv2.INTER_AREA)
# 旋转图标 center = (scaled_width // 2, scaled_height // 2) rotation_matrix = cv2.getRotationMatrix2D(center, rotation, 1.0) icon_rotated = cv2.warpAffine(icon_resized, rotation_matrix, (scaled_width, scaled_height), flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT, borderValue=(0, 0, 0, 0))
h, w, _ = icon_rotated.shape frame_h, frame_w, _ = frame.shape
# 计算绘制区域 top_left_x = max(0, int(x - w // 2)) top_left_y = max(0, int(y - h // 2)) bottom_right_x = min(frame_w, int(x + w // 2)) bottom_right_y = min(frame_h, int(y + h // 2))
icon_x_start = max(0, int(-x + w // 2)) icon_y_start = max(0, int(-y + h // 2)) icon_x_end = icon_x_start + (bottom_right_x - top_left_x) icon_y_end = icon_y_start + (bottom_right_y - top_left_y)
# 提取图标区域 icon_region = icon_rotated[icon_y_start:icon_y_end, icon_x_start:icon_x_end] alpha = icon_region[:, :, 3] / 255.0 icon_rgb = icon_region[:, :, :3]
# 提取帧对应区域 frame_region = frame[top_left_y:bottom_right_y, top_left_x:bottom_right_x]
# 叠加图标 # print(frame_region.shape, icon_rgb.shape) for c in range(3): frame_region[:, :, c] = (1 - alpha) * frame_region[:, :, c] + alpha * icon_rgb[:, :, c]
# 替换帧对应区域 frame[top_left_y:bottom_right_y, top_left_x:bottom_right_x] = frame_region
# 处理视频def process_video(input_video, output_video, config, mouse_icon_path, mouse_scale=1.0, mouse_rotation=0): key_data, mouse_data = parse_config(config)
cap = cv2.VideoCapture(input_video) fps = int(cap.get(cv2.CAP_PROP_FPS)) frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
mouse_icon = cv2.imread(mouse_icon_path, cv2.IMREAD_UNCHANGED)
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # fourcc = cv2.VideoWriter_fourcc(*'H264') out = cv2.VideoWriter(output_video, fourcc, fps, (frame_width, frame_height))
frame_idx = 0 while cap.isOpened(): ret, frame = cap.read() if not ret: break
keys = key_data.get(frame_idx, {"W": False, "A": False, "S": False, "D": False, "Sp": False, "Sh": False, "Ct": False}) mouse_position = mouse_data.get(frame_idx, (frame_width // 2, frame_height // 2))
draw_keys_on_frame(frame, keys, key_size=(50, 50), spacing=10, bottom_margin=20) overlay_icon(frame, mouse_icon, mouse_position, scale=mouse_scale, rotation=mouse_rotation)
out.write(frame) frame_idx += 1 print(f"Processing frame {frame_idx}/{frame_count}", end="\r")
cap.release() out.release() print("\nProcessing complete!")
# 使用示例mouse_icon_path = "./mouse.png"input_video = f"./input.mp4"output_video = f"./output.mp4"selected_config = [[25, "0 0 0 0 0 0 0 0 0.5"], [77, "1 0 0 0 0 0 0 0 0"], ""]process_video(input_video, output_video, selected_config, mouse_icon_path, mouse_scale=0.2, mouse_rotation=-20)
detection.pyAdded+128
input.mp4Added+3
mouse.pngAdded

Large File Pointer Details
- SHA256: 848775af1f622a2cd3776e9cf7d033d9656c6276c3541dc0f6999e77ee01a230
- Pointer size: 130 Bytes
- Size of remote file: 51.6 kB
output.mp4Added+3
visualize.pyAdded+198