| import json |
| import os |
| from pathlib import Path |
| from tqdm import tqdm |
|
|
| def check_json_data(json_file_path, images_root_dir): |
| """检查JSON数据的完整性 - 只过滤有image字段但图像不存在的数据""" |
| |
| |
| try: |
| with open(json_file_path, 'r', encoding='utf-8') as f: |
| data = json.load(f) |
| except Exception as e: |
| print(f"加载JSON文件失败: {e}") |
| return [] |
| |
| anomalous_data = [] |
| total_entries = len(data) |
| |
| print(f"开始检查 {total_entries} 条数据...") |
| |
| for i, entry in enumerate(tqdm(data, desc="检查图像文件", unit="条")): |
| issues = [] |
| |
| |
| if 'image' in entry: |
| image_path = entry['image'] |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| if len(image_path) == 0: |
| issues.append("图像路径为空") |
| else: |
| for img in image_path: |
| if not Path(img).exists(): |
| issues.append(f"图像文件不存在: {img}") |
| |
| |
| if issues: |
| anomalous_entry = { |
| 'index': i, |
| 'id': entry.get('id', 'Unknown'), |
| 'image': entry.get('image', 'N/A'), |
| 'entry': entry, |
| 'issues': issues |
| } |
| anomalous_data.append(anomalous_entry) |
| |
| return anomalous_data |
|
|
| def save_valid_data(json_file_path, anomalous_data, output_file='filtered_data.json'): |
| """保存过滤后的有效数据""" |
| |
| with open(json_file_path, 'r', encoding='utf-8') as f: |
| original_data = json.load(f) |
| |
| |
| anomalous_indices = {anomaly['index'] for anomaly in anomalous_data} |
| |
| |
| valid_data = [entry for i, entry in enumerate(original_data) if i not in anomalous_indices] |
| |
| |
| with open(output_file, 'w', encoding='utf-8') as f: |
| json.dump(valid_data, f, indent=2, ensure_ascii=False) |
| |
| print(f"过滤后的有效数据已保存到: {output_file}") |
| print(f"原始数据: {len(original_data)} 条") |
| print(f"有效数据: {len(valid_data)} 条") |
| print(f"过滤掉: {len(anomalous_data)} 条") |
|
|
| def save_anomalous_data(anomalous_data, output_file='anomalous_data.json'): |
| """保存异常数据到文件""" |
| if anomalous_data: |
| with open(output_file, 'w', encoding='utf-8') as f: |
| json.dump(anomalous_data, f, indent=2, ensure_ascii=False) |
| print(f"异常数据已保存到: {output_file}") |
| else: |
| print("没有异常数据需要保存") |
|
|
| def print_statistics(json_file_path, anomalous_data): |
| """打印统计信息""" |
| with open(json_file_path, 'r', encoding='utf-8') as f: |
| data = json.load(f) |
| |
| total_entries = len(data) |
| anomalous_count = len(anomalous_data) |
| |
| print(f"\n=== 数据统计 ===") |
| print(f"总条目数: {total_entries}") |
| print(f"图像异常条目数: {anomalous_count}") |
| print(f"有效条目数: {total_entries - anomalous_count}") |
| print(f"数据有效率: {((total_entries - anomalous_count) / total_entries * 100):.2f}%") |
| |
| |
| fields_stats = { |
| 'id': sum(1 for entry in data if 'id' in entry), |
| 'image': sum(1 for entry in data if 'image' in entry), |
| 'conversations': sum(1 for entry in data if 'conversations' in entry) |
| } |
| |
| print(f"\n=== 字段存在统计 ===") |
| for field, count in fields_stats.items(): |
| print(f"包含'{field}'字段的条目: {count}/{total_entries} ({count/total_entries*100:.1f}%)") |
|
|
| |
| def main(): |
| json_file = 'train_data_processed.json' |
| images_root_dir = 'images' |
| |
| |
| if not os.path.exists(json_file): |
| print(f"文件不存在: {json_file}") |
| return |
| |
| |
| anomalous_data = check_json_data(json_file, images_root_dir) |
| |
| |
| if anomalous_data: |
| print(f"\n发现 {len(anomalous_data)} 条图像异常数据:") |
| print("=" * 50) |
| |
| |
| for anomaly in anomalous_data[:10]: |
| print(f"索引: {anomaly['index']}") |
| print(f"ID: {anomaly['id']}") |
| print(f"图像: {anomaly['image']}") |
| print(f"问题: {', '.join(anomaly['issues'])}") |
| print("-" * 30) |
| |
| if len(anomalous_data) > 10: |
| print(f"... 还有 {len(anomalous_data) - 10} 条异常数据") |
| |
| |
| |
| |
| |
| |
| |
| |
| issue_counts = {} |
| for anomaly in anomalous_data: |
| for issue in anomaly['issues']: |
| issue_type = issue.split(':')[0] if ':' in issue else issue |
| issue_counts[issue_type] = issue_counts.get(issue_type, 0) + 1 |
| |
| print(f"\n=== 问题类型统计 ===") |
| for issue_type, count in sorted(issue_counts.items()): |
| print(f"{issue_type}: {count}次") |
| |
| else: |
| print("✅ 所有数据的图像文件都正常!") |
| |
| |
| |
| |
| print_statistics(json_file, anomalous_data) |
|
|
| if __name__ == "__main__": |
| main() |
|
|