File size: 9,499 Bytes
875e074 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | #!/usr/bin/env python3
"""
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获取gloss序列
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获取详细信息
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)}个):")
# 获取utterance的总帧范围
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目录
results_path = os.path.join(RESULTS_DIR, utterance_id)
if os.path.exists(results_path):
print(f"\n处理结果目录: {results_path}")
# 检查crop_frame
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
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]: # 只显示前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数量分布
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类型
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()
|