推理和存储.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import requests
  2. import json
  3. import os
  4. # --- 服务地址配置 ---
  5. # 第一个服务: 卡片评分/缺陷推理
  6. SCORE_API_URL = 'http://127.0.0.1:7744/api/card_score/score_inference'
  7. # 第二个服务: 结果存储
  8. STORAGE_API_URL = 'http://192.168.31.243:7745/api/image_data/insert'
  9. def process_and_store_card(image_path, score_type, is_reflect_card, img_name):
  10. """
  11. 一个完整的处理流程函数:
  12. 1. 请求评分服务获取缺陷JSON。
  13. 2. 将图片和缺陷JSON一起请求存储服务。
  14. :param image_path: 图片文件的本地路径。
  15. :param score_type: 评分类型 (例如: 'back_face')。
  16. :param is_reflect_card: 是否是反光卡 (布尔值: True 或 False)。
  17. :param img_name: 存储时使用的图片名称。
  18. :return: 成功时返回 True, 失败时返回 False。
  19. """
  20. # 检查图片文件是否存在
  21. if not os.path.exists(image_path):
  22. print(f"错误: 图片文件未找到 -> {image_path}")
  23. return False
  24. print(f"--- 开始处理图片: {image_path} ---")
  25. # --- 步骤 1: 请求评分服务 ---
  26. print("步骤 1: 请求评分服务...")
  27. # 准备评分服务的参数
  28. score_params = {
  29. 'score_type': score_type,
  30. 'is_reflect_card': str(is_reflect_card).lower() # API需要 'true' 或 'false' 字符串
  31. }
  32. try:
  33. # 打开图片文件, 'rb' 表示二进制读取
  34. with open(image_path, 'rb') as f:
  35. # 准备要上传的文件, 格式为: {'表单字段名': (文件名, 文件对象, MIME类型)}
  36. files_for_score = {'file': (os.path.basename(image_path), f, 'image/jpeg')}
  37. # 发送POST请求
  38. response_score = requests.post(
  39. SCORE_API_URL,
  40. params=score_params,
  41. files=files_for_score,
  42. timeout=90 # 设置超时时间, 例如30秒
  43. )
  44. # 检查响应状态码
  45. response_score.raise_for_status() # 如果状态码不是 2xx, 会抛出异常
  46. # 解析返回的JSON结果
  47. score_json_result = response_score.json()
  48. print("步骤 1: 成功获取评分结果。")
  49. # print("评分结果JSON:", json.dumps(score_json_result, indent=2, ensure_ascii=False))
  50. except requests.exceptions.RequestException as e:
  51. print(f"错误: 请求评分服务失败 -> {e}")
  52. return False
  53. except json.JSONDecodeError:
  54. print("错误: 无法解析评分服务的JSON响应。")
  55. print("服务器原始响应:", response_score.text)
  56. return False
  57. # --- 步骤 2: 请求存储服务 ---
  58. print("\n步骤 2: 请求存储服务...")
  59. try:
  60. # 准备存储服务的表单数据
  61. # 将上一步得到的JSON结果转换为字符串
  62. json_data_str = json.dumps(score_json_result, ensure_ascii=False)
  63. storage_data = {
  64. 'json_data_str': json_data_str,
  65. 'img_name': img_name
  66. }
  67. # 重新打开图片文件进行第二次上传
  68. # (因为上一个 `with` 语句块结束后文件已关闭)
  69. with open(image_path, 'rb') as f:
  70. # 准备要上传的文件, 注意这里的表单字段名是 'image'
  71. files_for_storage = {'image': (os.path.basename(image_path), f, 'image/jpeg')}
  72. # 发送POST请求
  73. response_storage = requests.post(
  74. STORAGE_API_URL,
  75. data=storage_data,
  76. files=files_for_storage,
  77. timeout=30
  78. )
  79. # 检查响应状态码
  80. response_storage.raise_for_status()
  81. print(f"步骤 2: 成功存储数据。服务器响应: {response_storage.json()}")
  82. except requests.exceptions.RequestException as e:
  83. print(f"错误: 请求存储服务失败 -> {e}")
  84. return False
  85. print(f"\n--- 图片 '{image_path}' 处理完成 ---")
  86. return True
  87. # --- 主程序入口 ---
  88. if __name__ == '__main__':
  89. # --- 在这里配置你要处理的图片信息 ---
  90. # 1. 图片的路径
  91. # 注意: 如果你的图片路径包含中文, 确保文件是以 UTF-8 编码保存的
  92. image_file_path = r"C:\Code\ML\Image\Card\_250917_1157_pokemon_no flecct01\37_front_0_1.jpg.jpg"
  93. # 2. 评分服务的参数
  94. # ["front_corner_edge", "front_face",
  95. # "back_corner_edge", "back_face"]
  96. card_score_type = 'front_face'
  97. # 是否是闪光卡
  98. card_is_reflect = False
  99. # 3. 存储服务的图片名称
  100. card_img_name = '铁甲蛹的边角'
  101. # 调用主函数执行流程
  102. process_and_store_card(
  103. image_path=image_file_path,
  104. score_type=card_score_type,
  105. is_reflect_card=card_is_reflect,
  106. img_name=card_img_name
  107. )