init_image_annotation_data.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. """
  2. 初始化图片标注示例数据脚本
  3. 创建多种图片标注项目和任务,用于测试图片标注功能。
  4. 包括:目标检测、图像分类、图像分割、关键点标注等。
  5. """
  6. import requests
  7. import json
  8. # API 基础 URL
  9. BASE_URL = "http://localhost:8000"
  10. # 1. 目标检测标注配置(矩形框标注)
  11. OBJECT_DETECTION_CONFIG = """<View>
  12. <Header value="目标检测 - 物体识别"/>
  13. <Image name="image" value="$image" zoom="true" zoomControl="true" rotateControl="true"/>
  14. <RectangleLabels name="label" toName="image">
  15. <Label value="人" background="red"/>
  16. <Label value="车" background="blue"/>
  17. <Label value="自行车" background="green"/>
  18. <Label value="狗" background="orange"/>
  19. <Label value="猫" background="purple"/>
  20. </RectangleLabels>
  21. </View>"""
  22. # 2. 图像分类标注配置
  23. IMAGE_CLASSIFICATION_CONFIG = """<View>
  24. <Header value="图像分类"/>
  25. <Image name="image" value="$image" zoom="true" zoomControl="true" rotateControl="true"/>
  26. <Choices name="category" toName="image" choice="single" showInline="true">
  27. <Choice value="风景"/>
  28. <Choice value="人物"/>
  29. <Choice value="动物"/>
  30. <Choice value="建筑"/>
  31. <Choice value="食物"/>
  32. <Choice value="其他"/>
  33. </Choices>
  34. </View>"""
  35. # 3. 图像分割标注配置(多边形标注)
  36. IMAGE_SEGMENTATION_CONFIG = """<View>
  37. <Header value="图像分割 - 精细标注"/>
  38. <Image name="image" value="$image" zoom="true" zoomControl="true" rotateControl="true"/>
  39. <PolygonLabels name="label" toName="image">
  40. <Label value="前景" background="rgba(255, 0, 0, 0.5)"/>
  41. <Label value="背景" background="rgba(0, 0, 255, 0.5)"/>
  42. <Label value="物体" background="rgba(0, 255, 0, 0.5)"/>
  43. </PolygonLabels>
  44. </View>"""
  45. # 4. 关键点标注配置(人体姿态估计)
  46. KEYPOINT_DETECTION_CONFIG = """<View>
  47. <Header value="关键点标注 - 人体姿态"/>
  48. <Image name="image" value="$image" zoom="true" zoomControl="true" rotateControl="true"/>
  49. <KeyPointLabels name="keypoint" toName="image">
  50. <Label value="头部" background="red"/>
  51. <Label value="肩膀" background="blue"/>
  52. <Label value="肘部" background="green"/>
  53. <Label value="手腕" background="orange"/>
  54. <Label value="膝盖" background="purple"/>
  55. <Label value="脚踝" background="pink"/>
  56. </KeyPointLabels>
  57. </View>"""
  58. # 5. 多标签图像分类配置
  59. MULTI_LABEL_CLASSIFICATION_CONFIG = """<View>
  60. <Header value="多标签图像分类"/>
  61. <Image name="image" value="$image" zoom="true" zoomControl="true" rotateControl="true"/>
  62. <Choices name="attributes" toName="image" choice="multiple" showInline="false">
  63. <Choice value="室内"/>
  64. <Choice value="室外"/>
  65. <Choice value="白天"/>
  66. <Choice value="夜晚"/>
  67. <Choice value="晴天"/>
  68. <Choice value="雨天"/>
  69. <Choice value="有人"/>
  70. <Choice value="无人"/>
  71. </Choices>
  72. </View>"""
  73. # 6. 图像质量评估配置
  74. IMAGE_QUALITY_CONFIG = """<View>
  75. <Header value="图像质量评估"/>
  76. <Image name="image" value="$image" zoom="true" zoomControl="true" rotateControl="true"/>
  77. <Choices name="quality" toName="image" choice="single" showInline="true">
  78. <Choice value="优秀"/>
  79. <Choice value="良好"/>
  80. <Choice value="一般"/>
  81. <Choice value="较差"/>
  82. </Choices>
  83. <Choices name="issues" toName="image" choice="multiple" showInline="false">
  84. <Choice value="模糊"/>
  85. <Choice value="曝光过度"/>
  86. <Choice value="曝光不足"/>
  87. <Choice value="噪点多"/>
  88. <Choice value="色彩失真"/>
  89. </Choices>
  90. </View>"""
  91. # 示例图片任务数据
  92. # 使用公开的测试图片 URL(来自 Unsplash 等免费图片网站)
  93. SAMPLE_IMAGE_TASKS = [
  94. # 目标检测任务
  95. {
  96. "name": "街道场景-1",
  97. "data": {
  98. "image": "https://images.unsplash.com/photo-1449824913935-59a10b8d2000?w=800"
  99. }
  100. },
  101. {
  102. "name": "街道场景-2",
  103. "data": {
  104. "image": "https://images.unsplash.com/photo-1477959858617-67f85cf4f1df?w=800"
  105. }
  106. },
  107. {
  108. "name": "公园场景",
  109. "data": {
  110. "image": "https://images.unsplash.com/photo-1441974231531-c6227db76b6e?w=800"
  111. }
  112. },
  113. # 图像分类任务
  114. {
  115. "name": "风景图片-1",
  116. "data": {
  117. "image": "https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=800"
  118. }
  119. },
  120. {
  121. "name": "建筑图片-1",
  122. "data": {
  123. "image": "https://images.unsplash.com/photo-1480714378408-67cf0d13bc1b?w=800"
  124. }
  125. },
  126. {
  127. "name": "动物图片-1",
  128. "data": {
  129. "image": "https://images.unsplash.com/photo-1425082661705-1834bfd09dca?w=800"
  130. }
  131. },
  132. # 图像分割任务
  133. {
  134. "name": "物体分割-1",
  135. "data": {
  136. "image": "https://images.unsplash.com/photo-1518791841217-8f162f1e1131?w=800"
  137. }
  138. },
  139. {
  140. "name": "物体分割-2",
  141. "data": {
  142. "image": "https://images.unsplash.com/photo-1514888286974-6c03e2ca1dba?w=800"
  143. }
  144. },
  145. # 关键点标注任务
  146. {
  147. "name": "人体姿态-1",
  148. "data": {
  149. "image": "https://images.unsplash.com/photo-1571019614242-c5c5dee9f50b?w=800"
  150. }
  151. },
  152. {
  153. "name": "人体姿态-2",
  154. "data": {
  155. "image": "https://images.unsplash.com/photo-1517836357463-d25dfeac3438?w=800"
  156. }
  157. },
  158. # 多标签分类任务
  159. {
  160. "name": "场景分析-1",
  161. "data": {
  162. "image": "https://images.unsplash.com/photo-1464822759023-fed622ff2c3b?w=800"
  163. }
  164. },
  165. {
  166. "name": "场景分析-2",
  167. "data": {
  168. "image": "https://images.unsplash.com/photo-1469474968028-56623f02e42e?w=800"
  169. }
  170. },
  171. # 图像质量评估任务
  172. {
  173. "name": "质量评估-1",
  174. "data": {
  175. "image": "https://images.unsplash.com/photo-1472214103451-9374bd1c798e?w=800"
  176. }
  177. },
  178. {
  179. "name": "质量评估-2",
  180. "data": {
  181. "image": "https://images.unsplash.com/photo-1501594907352-04cda38ebc29?w=800"
  182. }
  183. },
  184. ]
  185. def create_project(name, description, config):
  186. """创建项目"""
  187. url = f"{BASE_URL}/api/projects"
  188. data = {
  189. "name": name,
  190. "description": description,
  191. "config": config
  192. }
  193. response = requests.post(url, json=data)
  194. if response.status_code == 201:
  195. project = response.json()
  196. print(f"✓ 创建项目成功: {project['name']} (ID: {project['id']})")
  197. return project
  198. else:
  199. print(f"✗ 创建项目失败: {response.status_code} - {response.text}")
  200. return None
  201. def create_task(project_id, task_name, task_data):
  202. """创建任务"""
  203. url = f"{BASE_URL}/api/tasks"
  204. data = {
  205. "project_id": project_id,
  206. "name": task_name,
  207. "data": task_data
  208. }
  209. response = requests.post(url, json=data)
  210. if response.status_code == 201:
  211. task = response.json()
  212. print(f" ✓ 创建任务: {task['name']} (ID: {task['id']})")
  213. return task
  214. else:
  215. print(f" ✗ 创建任务失败: {response.status_code} - {response.text}")
  216. return None
  217. def main():
  218. """主函数"""
  219. print("=" * 60)
  220. print("初始化图片标注平台示例数据")
  221. print("=" * 60)
  222. print()
  223. # 1. 创建目标检测项目
  224. print("1. 创建目标检测项目...")
  225. detection_project = create_project(
  226. name="目标检测 - 街道场景",
  227. description="标注街道场景中的人、车、自行车等物体",
  228. config=OBJECT_DETECTION_CONFIG
  229. )
  230. if detection_project:
  231. print(" 创建目标检测任务...")
  232. for i in range(3):
  233. create_task(
  234. detection_project['id'],
  235. SAMPLE_IMAGE_TASKS[i]['name'],
  236. SAMPLE_IMAGE_TASKS[i]['data']
  237. )
  238. print()
  239. # 2. 创建图像分类项目
  240. print("2. 创建图像分类项目...")
  241. classification_project = create_project(
  242. name="图像分类",
  243. description="将图片分类为风景、人物、动物、建筑、食物等类别",
  244. config=IMAGE_CLASSIFICATION_CONFIG
  245. )
  246. if classification_project:
  247. print(" 创建图像分类任务...")
  248. for i in range(3, 6):
  249. create_task(
  250. classification_project['id'],
  251. SAMPLE_IMAGE_TASKS[i]['name'],
  252. SAMPLE_IMAGE_TASKS[i]['data']
  253. )
  254. print()
  255. # 3. 创建图像分割项目
  256. print("3. 创建图像分割项目...")
  257. segmentation_project = create_project(
  258. name="图像分割 - 物体轮廓",
  259. description="使用多边形工具精细标注物体轮廓",
  260. config=IMAGE_SEGMENTATION_CONFIG
  261. )
  262. if segmentation_project:
  263. print(" 创建图像分割任务...")
  264. for i in range(6, 8):
  265. create_task(
  266. segmentation_project['id'],
  267. SAMPLE_IMAGE_TASKS[i]['name'],
  268. SAMPLE_IMAGE_TASKS[i]['data']
  269. )
  270. print()
  271. # 4. 创建关键点标注项目
  272. print("4. 创建关键点标注项目...")
  273. keypoint_project = create_project(
  274. name="关键点标注 - 人体姿态估计",
  275. description="标注人体关键点(头部、肩膀、肘部、手腕、膝盖、脚踝)",
  276. config=KEYPOINT_DETECTION_CONFIG
  277. )
  278. if keypoint_project:
  279. print(" 创建关键点标注任务...")
  280. for i in range(8, 10):
  281. create_task(
  282. keypoint_project['id'],
  283. SAMPLE_IMAGE_TASKS[i]['name'],
  284. SAMPLE_IMAGE_TASKS[i]['data']
  285. )
  286. print()
  287. # 5. 创建多标签分类项目
  288. print("5. 创建多标签分类项目...")
  289. multi_label_project = create_project(
  290. name="多标签图像分类",
  291. description="为图片添加多个属性标签(室内/室外、白天/夜晚等)",
  292. config=MULTI_LABEL_CLASSIFICATION_CONFIG
  293. )
  294. if multi_label_project:
  295. print(" 创建多标签分类任务...")
  296. for i in range(10, 12):
  297. create_task(
  298. multi_label_project['id'],
  299. SAMPLE_IMAGE_TASKS[i]['name'],
  300. SAMPLE_IMAGE_TASKS[i]['data']
  301. )
  302. print()
  303. # 6. 创建图像质量评估项目
  304. print("6. 创建图像质量评估项目...")
  305. quality_project = create_project(
  306. name="图像质量评估",
  307. description="评估图片质量并标注存在的问题(模糊、曝光等)",
  308. config=IMAGE_QUALITY_CONFIG
  309. )
  310. if quality_project:
  311. print(" 创建图像质量评估任务...")
  312. for i in range(12, 14):
  313. create_task(
  314. quality_project['id'],
  315. SAMPLE_IMAGE_TASKS[i]['name'],
  316. SAMPLE_IMAGE_TASKS[i]['data']
  317. )
  318. print()
  319. print("=" * 60)
  320. print("图片标注示例数据初始化完成!")
  321. print("=" * 60)
  322. print()
  323. print("已创建的项目类型:")
  324. print("1. 目标检测 - 使用矩形框标注物体")
  325. print("2. 图像分类 - 单标签分类")
  326. print("3. 图像分割 - 使用多边形精细标注")
  327. print("4. 关键点标注 - 人体姿态估计")
  328. print("5. 多标签分类 - 场景属性标注")
  329. print("6. 图像质量评估 - 质量评分和问题标注")
  330. print()
  331. print("你现在可以:")
  332. print("1. 访问 http://localhost:4200/projects 查看项目列表")
  333. print("2. 点击项目查看详情和任务")
  334. print("3. 点击'开始标注'按钮进行图片标注")
  335. print()
  336. print("注意:")
  337. print("- 图片来自 Unsplash 免费图片库")
  338. print("- 需要网络连接才能加载图片")
  339. print("- 如果图片加载失败,请检查网络连接")
  340. print()
  341. if __name__ == "__main__":
  342. try:
  343. main()
  344. except requests.exceptions.ConnectionError:
  345. print("✗ 错误: 无法连接到后端服务器")
  346. print(" 请确保后端服务器正在运行:")
  347. print(" cd backend && python -m uvicorn main:app --reload --host 0.0.0.0 --port 8000")
  348. except Exception as e:
  349. print(f"✗ 发生错误: {e}")
  350. import traceback
  351. traceback.print_exc()