cloud_api_stub.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import asyncio
  2. import aiohttp
  3. from minio_client import minio_client, get_full_url
  4. from services import MINIO_BUCKET, MINIO_BASE_PREFIX, RECOGNIZE_API_URL, DATA_PREFIX
  5. import time
  6. import datetime
  7. import os
  8. async def upload_image_to_cloud(local_file_path: str) -> str:
  9. """
  10. 实际调用图片服务器上传接口
  11. """
  12. print(f"正在上传: {local_file_path} 到 minio")
  13. if not os.path.exists(local_file_path):
  14. print("错误: 本地文件不存在")
  15. return None
  16. try:
  17. # 格式化输出:2023-10-27 10:00:00
  18. formatted_time = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
  19. object_name = f"raspi_{formatted_time}.jpg"
  20. cloud_path = os.path.join(MINIO_BASE_PREFIX, object_name).replace("\\", "/")
  21. minio_client.fput_object(MINIO_BUCKET, cloud_path, local_file_path, content_type="image/jpeg")
  22. print(f"成功上传 {object_name} 到 {MINIO_BUCKET}")
  23. img_url = get_full_url(object_name)
  24. return img_url
  25. except Exception as e:
  26. print(f"上传异常: {e}")
  27. return None
  28. async def recognize_card_info(local_file_path: str) -> dict:
  29. """
  30. 调用卡片识别接口
  31. """
  32. print(f"[Recognize] 正在识别卡片: {local_file_path} ...")
  33. EMPTY_CARD_INFO = {
  34. "year": "",
  35. "series": "",
  36. "cardSet": "",
  37. "player": "",
  38. "confidentialId": "",
  39. "limitId": ""
  40. }
  41. if not os.path.exists(local_file_path):
  42. print("[Recognize] 文件不存在,返回空信息")
  43. return EMPTY_CARD_INFO
  44. try:
  45. async with aiohttp.ClientSession() as session:
  46. # 构造 Multipart/form-data
  47. data = aiohttp.FormData()
  48. # 根据 curl -F 'imgFile=@...',字段名必须是 'imgFile'
  49. data.add_field('imgFile',
  50. open(local_file_path, 'rb'),
  51. filename=os.path.basename(local_file_path),
  52. content_type='image/jpeg')
  53. # 发送请求
  54. async with session.post(RECOGNIZE_API_URL, data=data, timeout=15) as response:
  55. if response.status != 200:
  56. print(f"[Recognize] 接口报错: Status {response.status}")
  57. text = await response.text()
  58. print(f"[Recognize] 错误内容: {text}")
  59. return EMPTY_CARD_INFO
  60. # 解析 JSON
  61. result_list = await response.json()
  62. # 校验返回数据是否有效
  63. if not result_list or not isinstance(result_list, list):
  64. print("[Recognize] 返回格式不是列表或为空")
  65. return EMPTY_CARD_INFO
  66. # 取第一个结果
  67. first_item = result_list[0]
  68. detail = first_item.get("detail", {})
  69. # 映射字段
  70. # mapping: no -> confidentialId
  71. path = detail.get("path", "")
  72. if path!="" or path is not None:
  73. path = f"{DATA_PREFIX}{path}"
  74. info = {
  75. "year": detail.get("year", ""),
  76. "series": detail.get("series", ""),
  77. "cardSet": detail.get("cardset", ""),
  78. "player": detail.get("player", ""),
  79. "confidentialId": detail.get("no", ""),
  80. "limitId": "", # 源数据没有,留空
  81. "path": path
  82. }
  83. print(f"[Recognize] 识别成功: {first_item.get('tag', 'No Tag')}")
  84. return info
  85. except Exception as e:
  86. print(f"[Recognize] 请求发生异常: {e}")
  87. import traceback
  88. traceback.print_exc()
  89. return EMPTY_CARD_INFO