|
|
|
|
|
""" |
|
|
ASLLRP数据集查询工具 |
|
|
用于快速查询和提取特定utterance或sign的信息 |
|
|
""" |
|
|
import csv |
|
|
import os |
|
|
import argparse |
|
|
from collections import defaultdict |
|
|
|
|
|
|
|
|
BASE_PATH = "/research/cbim/vast/sf895/code/Sign-X/output/huggingface_asllrp_repo" |
|
|
MAPPING_FILE = os.path.join(BASE_PATH, "ASLLRP_utterances_mapping.txt") |
|
|
CSV_FILE = os.path.join(BASE_PATH, "asllrp_sentence_signs_2025_06_28.csv") |
|
|
RESULTS_DIR = os.path.join(BASE_PATH, "ASLLRP_utterances_results") |
|
|
|
|
|
def load_csv_data(): |
|
|
"""加载CSV数据""" |
|
|
data = defaultdict(list) |
|
|
with open(CSV_FILE, 'r') as f: |
|
|
reader = csv.DictReader(f) |
|
|
for row in reader: |
|
|
utterance_video = row['Utterance video filename'] |
|
|
data[utterance_video].append(row) |
|
|
return data |
|
|
|
|
|
def load_mapping_data(): |
|
|
"""加载mapping数据""" |
|
|
mapping = {} |
|
|
with open(MAPPING_FILE, 'r') as f: |
|
|
for line in f: |
|
|
video_id, glosses = line.strip().split(': ', 1) |
|
|
mapping[video_id] = glosses.split() |
|
|
return mapping |
|
|
|
|
|
def query_utterance(utterance_id): |
|
|
"""查询特定utterance的详细信息""" |
|
|
print("="*80) |
|
|
print(f"Utterance ID: {utterance_id}") |
|
|
print("="*80) |
|
|
|
|
|
|
|
|
mapping = load_mapping_data() |
|
|
if utterance_id in mapping: |
|
|
glosses = mapping[utterance_id] |
|
|
print(f"\nGloss序列 ({len(glosses)}个):") |
|
|
print(" " + " ".join(glosses)) |
|
|
else: |
|
|
print(f"\n警告: 在mapping.txt中找不到{utterance_id}") |
|
|
|
|
|
|
|
|
csv_data = load_csv_data() |
|
|
utterance_key = f"{utterance_id}.mp4" |
|
|
|
|
|
if utterance_key in csv_data: |
|
|
signs = csv_data[utterance_key] |
|
|
print(f"\nCSV中的Signs ({len(signs)}个):") |
|
|
|
|
|
|
|
|
if signs: |
|
|
utterance_start = int(signs[0]['Start frame of the containing utterance']) |
|
|
utterance_end = int(signs[0]['End frame of the containing utterance']) |
|
|
utterance_duration = utterance_end - utterance_start |
|
|
print(f"\nUtterance帧范围: {utterance_start} - {utterance_end} (总共{utterance_duration}帧)") |
|
|
|
|
|
print(f"\n详细Signs列表:") |
|
|
print(f"{'序号':<4} {'Gloss':<30} {'原始视频帧范围':<20} {'裁剪视频帧范围':<20} {'类型':<20}") |
|
|
print("-"*100) |
|
|
|
|
|
for i, sign in enumerate(signs, 1): |
|
|
gloss = sign['Main entry gloss label'] |
|
|
start = int(sign['Start frame of the sign video']) |
|
|
end = int(sign['End frame of the sign video']) |
|
|
sign_type = sign['Sign type'] |
|
|
|
|
|
|
|
|
cropped_start = start - utterance_start |
|
|
cropped_end = end - utterance_start |
|
|
|
|
|
print(f"{i:<4} {gloss:<30} {start}-{end} ({end-start}帧)".ljust(58) + |
|
|
f"{cropped_start}-{cropped_end}".ljust(24) + |
|
|
f"{sign_type}") |
|
|
else: |
|
|
print(f"\n警告: 在CSV文件中找不到{utterance_id}") |
|
|
|
|
|
|
|
|
results_path = os.path.join(RESULTS_DIR, utterance_id) |
|
|
if os.path.exists(results_path): |
|
|
print(f"\n处理结果目录: {results_path}") |
|
|
|
|
|
|
|
|
crop_frame_dir = os.path.join(results_path, "crop_frame") |
|
|
if os.path.exists(crop_frame_dir): |
|
|
frames = [f for f in os.listdir(crop_frame_dir) if f.endswith('.jpg')] |
|
|
print(f" - 裁剪帧数: {len(frames)}") |
|
|
|
|
|
|
|
|
video_path = os.path.join(results_path, "crop_original_video.mp4") |
|
|
if os.path.exists(video_path): |
|
|
size_mb = os.path.getsize(video_path) / (1024 * 1024) |
|
|
print(f" - 裁剪视频大小: {size_mb:.2f} MB") |
|
|
|
|
|
|
|
|
dwpose_dir = os.path.join(results_path, "results_dwpose/npz") |
|
|
if os.path.exists(dwpose_dir): |
|
|
npz_files = [f for f in os.listdir(dwpose_dir) if f.endswith('.npz')] |
|
|
print(f" - DWPose文件数: {len(npz_files)}") |
|
|
else: |
|
|
print(f"\n警告: 在results目录中找不到{utterance_id}") |
|
|
|
|
|
def search_gloss(gloss_query): |
|
|
"""搜索包含特定gloss的utterances""" |
|
|
print("="*80) |
|
|
print(f"搜索Gloss: {gloss_query}") |
|
|
print("="*80) |
|
|
|
|
|
csv_data = load_csv_data() |
|
|
matches = [] |
|
|
|
|
|
for utterance_video, signs in csv_data.items(): |
|
|
for sign in signs: |
|
|
if gloss_query.upper() in sign['Main entry gloss label'].upper(): |
|
|
utterance_id = utterance_video.replace('.mp4', '') |
|
|
matches.append({ |
|
|
'utterance_id': utterance_id, |
|
|
'gloss': sign['Main entry gloss label'], |
|
|
'start_frame': int(sign['Start frame of the sign video']), |
|
|
'end_frame': int(sign['End frame of the sign video']), |
|
|
'sign_type': sign['Sign type'] |
|
|
}) |
|
|
|
|
|
print(f"\n找到 {len(matches)} 个匹配的signs:") |
|
|
print(f"{'Utterance ID':<15} {'Gloss':<30} {'帧范围':<20} {'类型':<20}") |
|
|
print("-"*90) |
|
|
|
|
|
for match in matches[:20]: |
|
|
print(f"{match['utterance_id']:<15} {match['gloss']:<30} " |
|
|
f"{match['start_frame']}-{match['end_frame']}".ljust(24) + |
|
|
f"{match['sign_type']}") |
|
|
|
|
|
if len(matches) > 20: |
|
|
print(f"\n... 还有 {len(matches) - 20} 个结果未显示") |
|
|
|
|
|
def list_all_utterances(): |
|
|
"""列出所有utterances的统计信息""" |
|
|
print("="*80) |
|
|
print("所有Utterances统计") |
|
|
print("="*80) |
|
|
|
|
|
mapping = load_mapping_data() |
|
|
csv_data = load_csv_data() |
|
|
|
|
|
print(f"\nMapping.txt中的utterances: {len(mapping)}") |
|
|
print(f"CSV中的utterances: {len(csv_data)}") |
|
|
|
|
|
results_dirs = [d for d in os.listdir(RESULTS_DIR) |
|
|
if os.path.isdir(os.path.join(RESULTS_DIR, d))] |
|
|
print(f"Results目录中的文件夹: {len(results_dirs)}") |
|
|
|
|
|
|
|
|
gloss_counts = [len(glosses) for glosses in mapping.values()] |
|
|
avg_gloss = sum(gloss_counts) / len(gloss_counts) if gloss_counts else 0 |
|
|
min_gloss = min(gloss_counts) if gloss_counts else 0 |
|
|
max_gloss = max(gloss_counts) if gloss_counts else 0 |
|
|
|
|
|
print(f"\nGloss统计:") |
|
|
print(f" 平均每个utterance: {avg_gloss:.1f} 个glosses") |
|
|
print(f" 最少: {min_gloss} 个glosses") |
|
|
print(f" 最多: {max_gloss} 个glosses") |
|
|
|
|
|
|
|
|
sign_types = defaultdict(int) |
|
|
for signs in csv_data.values(): |
|
|
for sign in signs: |
|
|
sign_types[sign['Sign type']] += 1 |
|
|
|
|
|
print(f"\nSign类型分布:") |
|
|
for sign_type, count in sorted(sign_types.items(), key=lambda x: x[1], reverse=True): |
|
|
print(f" {sign_type}: {count}") |
|
|
|
|
|
def extract_sign_info(utterance_id, gloss): |
|
|
"""提取特定sign的信息,用于代码中使用""" |
|
|
csv_data = load_csv_data() |
|
|
utterance_key = f"{utterance_id}.mp4" |
|
|
|
|
|
if utterance_key not in csv_data: |
|
|
print(f"错误: 找不到utterance {utterance_id}") |
|
|
return None |
|
|
|
|
|
signs = csv_data[utterance_key] |
|
|
for sign in signs: |
|
|
if gloss.upper() == sign['Main entry gloss label'].upper(): |
|
|
utterance_start = int(sign['Start frame of the containing utterance']) |
|
|
start = int(sign['Start frame of the sign video']) |
|
|
end = int(sign['End frame of the sign video']) |
|
|
|
|
|
info = { |
|
|
'gloss': sign['Main entry gloss label'], |
|
|
'start_frame_original': start, |
|
|
'end_frame_original': end, |
|
|
'start_frame_cropped': start - utterance_start, |
|
|
'end_frame_cropped': end - utterance_start, |
|
|
'duration': end - start, |
|
|
'sign_type': sign['Sign type'] |
|
|
} |
|
|
|
|
|
print(f"Sign信息:") |
|
|
print(f" Gloss: {info['gloss']}") |
|
|
print(f" 原始视频帧: {info['start_frame_original']} - {info['end_frame_original']}") |
|
|
print(f" 裁剪视频帧: {info['start_frame_cropped']} - {info['end_frame_cropped']}") |
|
|
print(f" 持续时间: {info['duration']} 帧") |
|
|
print(f" 类型: {info['sign_type']}") |
|
|
|
|
|
return info |
|
|
|
|
|
print(f"错误: 在utterance {utterance_id} 中找不到gloss '{gloss}'") |
|
|
return None |
|
|
|
|
|
if __name__ == "__main__": |
|
|
parser = argparse.ArgumentParser(description='ASLLRP数据集查询工具') |
|
|
parser.add_argument('--utterance', '-u', help='查询特定utterance ID') |
|
|
parser.add_argument('--search', '-s', help='搜索包含特定gloss的utterances') |
|
|
parser.add_argument('--list', '-l', action='store_true', help='列出所有utterances的统计') |
|
|
parser.add_argument('--extract', '-e', nargs=2, metavar=('UTTERANCE_ID', 'GLOSS'), |
|
|
help='提取特定sign的信息') |
|
|
|
|
|
args = parser.parse_args() |
|
|
|
|
|
if args.utterance: |
|
|
query_utterance(args.utterance) |
|
|
elif args.search: |
|
|
search_gloss(args.search) |
|
|
elif args.list: |
|
|
list_all_utterances() |
|
|
elif args.extract: |
|
|
extract_sign_info(args.extract[0], args.extract[1]) |
|
|
else: |
|
|
print("使用示例:") |
|
|
print(" 查询utterance: python query_asllrp_data.py --utterance 10006709") |
|
|
print(" 搜索gloss: python query_asllrp_data.py --search THAT") |
|
|
print(" 列出统计: python query_asllrp_data.py --list") |
|
|
print(" 提取sign: python query_asllrp_data.py --extract 10006709 THAT") |
|
|
parser.print_help() |
|
|
|