From 8916e4c7ee8b37479c6c5ad74e81697cf300e234 Mon Sep 17 00:00:00 2001
From: Bin <bin.zheng@slooong.com>
Date: 星期三, 17 十二月 2025 14:43:50 +0800
Subject: [PATCH] 测试脚本_5
---
test_converter_direct.py | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 114 insertions(+), 0 deletions(-)
diff --git a/test_converter_direct.py b/test_converter_direct.py
new file mode 100644
index 0000000..d07bc0f
--- /dev/null
+++ b/test_converter_direct.py
@@ -0,0 +1,114 @@
+#!/usr/bin/env python
+"""
+直接测试 Converter 的行为
+"""
+import os
+import sys
+import json
+
+# 设置环境变量
+os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'label_studio.core.settings.label_studio')
+os.environ['LABEL_STUDIO_BASE_DATA_DIR'] = '/data/label-studio-data'
+
+# 添加项目路径
+sys.path.insert(0, '/data/label-studio')
+
+import django
+django.setup()
+
+from django.conf import settings
+from label_studio_sdk.converter import Converter
+
+print("=" * 80)
+print("测试 Converter 行为")
+print("=" * 80)
+
+# 配置
+upload_dir = os.path.join(settings.MEDIA_ROOT, settings.UPLOAD_DIR)
+print(f"upload_dir: {upload_dir}")
+print(f"upload_dir exists: {os.path.exists(upload_dir)}")
+
+if os.path.exists(upload_dir):
+ print(f"upload_dir contents: {os.listdir(upload_dir)}")
+
+# 创建测试数据
+test_data = [
+ {
+ "id": 1,
+ "data": {
+ "$undefined$": "/data/upload/1/8812d845-屏幕截图_2025-03-20_190533.png"
+ },
+ "annotations": []
+ }
+]
+
+# 保存为临时JSON
+test_json = "/tmp/test_coco_input.json"
+with open(test_json, 'w', encoding='utf-8') as f:
+ json.dump(test_data, f, ensure_ascii=False, indent=2)
+
+print(f"\nTest JSON created: {test_json}")
+print(f"Test data: {test_data[0]['data']}")
+
+# 测试文件是否存在
+test_file_path = "/data/upload/1/8812d845-屏幕截图_2025-03-20_190533.png"
+print(f"\nTest file path: {test_file_path}")
+print(f"Test file exists: {os.path.exists(test_file_path)}")
+
+# 尝试不同的 upload_dir 配置
+test_configs = [
+ ("MEDIA_ROOT/UPLOAD_DIR", os.path.join(settings.MEDIA_ROOT, settings.UPLOAD_DIR)),
+ ("/data", "/data"),
+ ("/data/label-studio-data/media", "/data/label-studio-data/media"),
+]
+
+for config_name, test_upload_dir in test_configs:
+ print(f"\n{'=' * 80}")
+ print(f"Testing with upload_dir = {config_name} ({test_upload_dir})")
+ print(f"{'=' * 80}")
+
+ try:
+ # 创建输出目录
+ output_dir = f"/tmp/test_coco_output_{config_name.replace('/', '_')}"
+ os.makedirs(output_dir, exist_ok=True)
+
+ # 初始化 Converter
+ converter = Converter(
+ config="<View><Image name='image' value='$undefined$'/></View>",
+ project_dir=None,
+ upload_dir=test_upload_dir,
+ download_resources=True,
+ )
+
+ print(f"Converter initialized")
+ print(f"Converter.upload_dir: {converter.upload_dir if hasattr(converter, 'upload_dir') else 'N/A'}")
+
+ # 执行转换
+ converter.convert(test_json, output_dir, "COCO", is_dir=False)
+
+ print(f"Conversion completed")
+
+ # 检查输出
+ output_contents = os.listdir(output_dir)
+ print(f"Output directory contents: {output_contents}")
+
+ # 检查 images 目录
+ images_dir = os.path.join(output_dir, "images")
+ if os.path.exists(images_dir):
+ image_files = os.listdir(images_dir)
+ print(f"✓ images/ directory exists with {len(image_files)} files")
+ if image_files:
+ print(f" Files: {image_files}")
+ else:
+ print(f" ✗ ERROR: images/ directory is EMPTY!")
+ else:
+ print(f"✗ ERROR: images/ directory does NOT exist")
+
+ except Exception as e:
+ print(f"✗ ERROR: {e}")
+ import traceback
+ traceback.print_exc()
+
+print(f"\n{'=' * 80}")
+print("测试完成")
+print(f"{'=' * 80}")
--
Gitblit v1.9.3