"""
初始化图片标注示例数据脚本
创建多种图片标注项目和任务,用于测试图片标注功能。
包括:目标检测、图像分类、图像分割、关键点标注等。
"""
import requests
import json
# API 基础 URL
BASE_URL = "http://localhost:8000"
# 1. 目标检测标注配置(矩形框标注)
OBJECT_DETECTION_CONFIG = """
"""
# 2. 图像分类标注配置
IMAGE_CLASSIFICATION_CONFIG = """
"""
# 3. 图像分割标注配置(多边形标注)
IMAGE_SEGMENTATION_CONFIG = """
"""
# 4. 关键点标注配置(人体姿态估计)
KEYPOINT_DETECTION_CONFIG = """
"""
# 5. 多标签图像分类配置
MULTI_LABEL_CLASSIFICATION_CONFIG = """
"""
# 6. 图像质量评估配置
IMAGE_QUALITY_CONFIG = """
"""
# 示例图片任务数据
# 使用公开的测试图片 URL(来自 Unsplash 等免费图片网站)
SAMPLE_IMAGE_TASKS = [
# 目标检测任务
{
"name": "街道场景-1",
"data": {
"image": "https://images.unsplash.com/photo-1449824913935-59a10b8d2000?w=800"
}
},
{
"name": "街道场景-2",
"data": {
"image": "https://images.unsplash.com/photo-1477959858617-67f85cf4f1df?w=800"
}
},
{
"name": "公园场景",
"data": {
"image": "https://images.unsplash.com/photo-1441974231531-c6227db76b6e?w=800"
}
},
# 图像分类任务
{
"name": "风景图片-1",
"data": {
"image": "https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=800"
}
},
{
"name": "建筑图片-1",
"data": {
"image": "https://images.unsplash.com/photo-1480714378408-67cf0d13bc1b?w=800"
}
},
{
"name": "动物图片-1",
"data": {
"image": "https://images.unsplash.com/photo-1425082661705-1834bfd09dca?w=800"
}
},
# 图像分割任务
{
"name": "物体分割-1",
"data": {
"image": "https://images.unsplash.com/photo-1518791841217-8f162f1e1131?w=800"
}
},
{
"name": "物体分割-2",
"data": {
"image": "https://images.unsplash.com/photo-1514888286974-6c03e2ca1dba?w=800"
}
},
# 关键点标注任务
{
"name": "人体姿态-1",
"data": {
"image": "https://images.unsplash.com/photo-1571019614242-c5c5dee9f50b?w=800"
}
},
{
"name": "人体姿态-2",
"data": {
"image": "https://images.unsplash.com/photo-1517836357463-d25dfeac3438?w=800"
}
},
# 多标签分类任务
{
"name": "场景分析-1",
"data": {
"image": "https://images.unsplash.com/photo-1464822759023-fed622ff2c3b?w=800"
}
},
{
"name": "场景分析-2",
"data": {
"image": "https://images.unsplash.com/photo-1469474968028-56623f02e42e?w=800"
}
},
# 图像质量评估任务
{
"name": "质量评估-1",
"data": {
"image": "https://images.unsplash.com/photo-1472214103451-9374bd1c798e?w=800"
}
},
{
"name": "质量评估-2",
"data": {
"image": "https://images.unsplash.com/photo-1501594907352-04cda38ebc29?w=800"
}
},
]
def create_project(name, description, config):
"""创建项目"""
url = f"{BASE_URL}/api/projects"
data = {
"name": name,
"description": description,
"config": config
}
response = requests.post(url, json=data)
if response.status_code == 201:
project = response.json()
print(f"✓ 创建项目成功: {project['name']} (ID: {project['id']})")
return project
else:
print(f"✗ 创建项目失败: {response.status_code} - {response.text}")
return None
def create_task(project_id, task_name, task_data):
"""创建任务"""
url = f"{BASE_URL}/api/tasks"
data = {
"project_id": project_id,
"name": task_name,
"data": task_data
}
response = requests.post(url, json=data)
if response.status_code == 201:
task = response.json()
print(f" ✓ 创建任务: {task['name']} (ID: {task['id']})")
return task
else:
print(f" ✗ 创建任务失败: {response.status_code} - {response.text}")
return None
def main():
"""主函数"""
print("=" * 60)
print("初始化图片标注平台示例数据")
print("=" * 60)
print()
# 1. 创建目标检测项目
print("1. 创建目标检测项目...")
detection_project = create_project(
name="目标检测 - 街道场景",
description="标注街道场景中的人、车、自行车等物体",
config=OBJECT_DETECTION_CONFIG
)
if detection_project:
print(" 创建目标检测任务...")
for i in range(3):
create_task(
detection_project['id'],
SAMPLE_IMAGE_TASKS[i]['name'],
SAMPLE_IMAGE_TASKS[i]['data']
)
print()
# 2. 创建图像分类项目
print("2. 创建图像分类项目...")
classification_project = create_project(
name="图像分类",
description="将图片分类为风景、人物、动物、建筑、食物等类别",
config=IMAGE_CLASSIFICATION_CONFIG
)
if classification_project:
print(" 创建图像分类任务...")
for i in range(3, 6):
create_task(
classification_project['id'],
SAMPLE_IMAGE_TASKS[i]['name'],
SAMPLE_IMAGE_TASKS[i]['data']
)
print()
# 3. 创建图像分割项目
print("3. 创建图像分割项目...")
segmentation_project = create_project(
name="图像分割 - 物体轮廓",
description="使用多边形工具精细标注物体轮廓",
config=IMAGE_SEGMENTATION_CONFIG
)
if segmentation_project:
print(" 创建图像分割任务...")
for i in range(6, 8):
create_task(
segmentation_project['id'],
SAMPLE_IMAGE_TASKS[i]['name'],
SAMPLE_IMAGE_TASKS[i]['data']
)
print()
# 4. 创建关键点标注项目
print("4. 创建关键点标注项目...")
keypoint_project = create_project(
name="关键点标注 - 人体姿态估计",
description="标注人体关键点(头部、肩膀、肘部、手腕、膝盖、脚踝)",
config=KEYPOINT_DETECTION_CONFIG
)
if keypoint_project:
print(" 创建关键点标注任务...")
for i in range(8, 10):
create_task(
keypoint_project['id'],
SAMPLE_IMAGE_TASKS[i]['name'],
SAMPLE_IMAGE_TASKS[i]['data']
)
print()
# 5. 创建多标签分类项目
print("5. 创建多标签分类项目...")
multi_label_project = create_project(
name="多标签图像分类",
description="为图片添加多个属性标签(室内/室外、白天/夜晚等)",
config=MULTI_LABEL_CLASSIFICATION_CONFIG
)
if multi_label_project:
print(" 创建多标签分类任务...")
for i in range(10, 12):
create_task(
multi_label_project['id'],
SAMPLE_IMAGE_TASKS[i]['name'],
SAMPLE_IMAGE_TASKS[i]['data']
)
print()
# 6. 创建图像质量评估项目
print("6. 创建图像质量评估项目...")
quality_project = create_project(
name="图像质量评估",
description="评估图片质量并标注存在的问题(模糊、曝光等)",
config=IMAGE_QUALITY_CONFIG
)
if quality_project:
print(" 创建图像质量评估任务...")
for i in range(12, 14):
create_task(
quality_project['id'],
SAMPLE_IMAGE_TASKS[i]['name'],
SAMPLE_IMAGE_TASKS[i]['data']
)
print()
print("=" * 60)
print("图片标注示例数据初始化完成!")
print("=" * 60)
print()
print("已创建的项目类型:")
print("1. 目标检测 - 使用矩形框标注物体")
print("2. 图像分类 - 单标签分类")
print("3. 图像分割 - 使用多边形精细标注")
print("4. 关键点标注 - 人体姿态估计")
print("5. 多标签分类 - 场景属性标注")
print("6. 图像质量评估 - 质量评分和问题标注")
print()
print("你现在可以:")
print("1. 访问 http://localhost:4200/projects 查看项目列表")
print("2. 点击项目查看详情和任务")
print("3. 点击'开始标注'按钮进行图片标注")
print()
print("注意:")
print("- 图片来自 Unsplash 免费图片库")
print("- 需要网络连接才能加载图片")
print("- 如果图片加载失败,请检查网络连接")
print()
if __name__ == "__main__":
try:
main()
except requests.exceptions.ConnectionError:
print("✗ 错误: 无法连接到后端服务器")
print(" 请确保后端服务器正在运行:")
print(" cd backend && python -m uvicorn main:app --reload --host 0.0.0.0 --port 8000")
except Exception as e:
print(f"✗ 发生错误: {e}")
import traceback
traceback.print_exc()