| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- """
- Mock external projects script.
- Creates 4 external source projects with cat images for testing.
- """
- import sys
- import os
- import uuid
- import json
- # Add parent directory to path
- sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
- from database import get_db_connection
- # 猫猫图片 URL (使用支持跨域的图片)
- # 使用 picsum.photos 提供的随机图片服务,支持 CORS
- CAT_IMAGE_URL = "https://picsum.photos/id/40/800/600" # 一张猫的图片
- # 4 个外部项目的配置
- MOCK_PROJECTS = [
- {
- "name": "猫咪品种分类项目",
- "description": "对猫咪图片进行品种分类标注,包括英短、美短、布偶、橘猫等常见品种",
- "external_id": "ext_cat_breed_001",
- "task_type": "image_classification",
- "task_count": 5,
- },
- {
- "name": "猫咪目标检测项目",
- "description": "检测图片中猫咪的位置,使用矩形框标注猫咪的头部、身体等部位",
- "external_id": "ext_cat_detection_002",
- "task_type": "object_detection",
- "task_count": 8,
- },
- {
- "name": "猫咪情绪识别项目",
- "description": "识别猫咪的情绪状态,如开心、生气、困倦、好奇等",
- "external_id": "ext_cat_emotion_003",
- "task_type": "image_classification",
- "task_count": 6,
- },
- {
- "name": "猫咪姿态标注项目",
- "description": "标注猫咪的姿态,包括站立、坐着、躺着、跳跃等动作",
- "external_id": "ext_cat_pose_004",
- "task_type": "image_classification",
- "task_count": 4,
- },
- ]
- def create_mock_projects():
- """Create mock external projects with tasks."""
-
- with get_db_connection() as conn:
- cursor = conn.cursor()
-
- created_projects = []
-
- for project_info in MOCK_PROJECTS:
- # Generate project ID
- project_id = f"proj_{uuid.uuid4().hex[:12]}"
-
- # Insert project (draft status, external source, no config)
- cursor.execute("""
- INSERT INTO projects (id, name, description, config, status, source, task_type, external_id)
- VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
- """, (
- project_id,
- project_info["name"],
- project_info["description"],
- "", # 空配置,未配置状态
- "draft", # 草稿状态
- "external", # 外部来源
- project_info["task_type"],
- project_info["external_id"],
- ))
-
- # Create tasks for this project
- for i in range(project_info["task_count"]):
- task_id = f"task_{uuid.uuid4().hex[:12]}"
- task_name = f"{project_info['name']} - 任务 {i + 1}"
-
- # Task data with cat image
- task_data = {
- "image": CAT_IMAGE_URL,
- "meta": {
- "source": "external_api",
- "index": i + 1,
- "total": project_info["task_count"],
- }
- }
-
- cursor.execute("""
- INSERT INTO tasks (id, project_id, name, data, status, assigned_to)
- VALUES (%s, %s, %s, %s, %s, %s)
- """, (
- task_id,
- project_id,
- task_name,
- json.dumps(task_data, ensure_ascii=False),
- "pending", # 待处理状态
- None, # 未分配
- ))
-
- created_projects.append({
- "id": project_id,
- "name": project_info["name"],
- "task_count": project_info["task_count"],
- })
-
- print(f"✓ 创建项目: {project_info['name']} (ID: {project_id}, 任务数: {project_info['task_count']})")
-
- print(f"\n共创建 {len(created_projects)} 个外部项目")
- return created_projects
- if __name__ == "__main__":
- print("=" * 60)
- print("Mock 外部项目创建脚本")
- print("=" * 60)
- print(f"图片 URL: {CAT_IMAGE_URL}")
- print("-" * 60)
-
- try:
- projects = create_mock_projects()
- print("\n" + "=" * 60)
- print("创建完成!")
- print("=" * 60)
- except Exception as e:
- print(f"\n错误: {e}")
- import traceback
- traceback.print_exc()
- sys.exit(1)
|