img_data_insert.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import requests
  2. import json
  3. def upload_image_with_json(
  4. url: str,
  5. image_path: str,
  6. image_type: str = "front_face",
  7. image_name: str = "未命名",
  8. json_path: str = ''
  9. ):
  10. """
  11. 向指定接口上传图片及附带的 JSON 数据。
  12. 参数:
  13. url (str): 接口 URL,例如 http://127.0.0.1:7745/api/images/insert/6
  14. image_path (str): 图片文件路径
  15. image_type (str): 图片类型,例如 'front_face'
  16. image_name (str): 图片名称
  17. json_data (str | dict | None): JSON 内容,可以是字符串、字典或 None
  18. 返回:
  19. requests.Response: 服务器响应对象
  20. """
  21. with open(json_path, "r", encoding="utf-8") as f:
  22. json_data_str = f.read()
  23. files = {
  24. "image": (image_path, open(image_path, "rb"), "image/gif")
  25. }
  26. data = {
  27. "image_type": image_type,
  28. "image_name": image_name,
  29. "json_data_str": json_data_str
  30. }
  31. headers = {
  32. "accept": "application/json"
  33. }
  34. # 发送 POST 请求
  35. response = requests.post(url, headers=headers, files=files, data=data)
  36. return response
  37. def send4img():
  38. send_url = "http://127.0.0.1:7745/api/images/insert/7"
  39. front_face_img_path = r"C:\Code\ML\Project\CheckCardBoxAndDefectServer\_temp_work\test\正面面\rectify_center_img.jpg"
  40. front_face_json_path = r"C:\Code\ML\Project\CheckCardBoxAndDefectServer\_temp_work\test\正面面\front_face_score.json"
  41. front_edge_img_path = r"C:\Code\ML\Project\CheckCardBoxAndDefectServer\_temp_work\test\正面边角\rectify_center_img.jpg"
  42. front_edge_json_path = r"C:\Code\ML\Project\CheckCardBoxAndDefectServer\_temp_work\test\正面边角\front_corner_edge_score.json"
  43. back_face_img_path = r"C:\Code\ML\Project\CheckCardBoxAndDefectServer\_temp_work\test\背面面\rectify_center_img.jpg"
  44. back_face_json_path = r"C:\Code\ML\Project\CheckCardBoxAndDefectServer\_temp_work\test\背面面\back_face_score.json"
  45. back_edge_img_path = r"C:\Code\ML\Project\CheckCardBoxAndDefectServer\_temp_work\test\背面边角\rectify_center_img.jpg"
  46. back_edge_json_path = r"C:\Code\ML\Project\CheckCardBoxAndDefectServer\_temp_work\test\背面边角\back_corner_edge_score.json"
  47. data = {"front_face": {"image": front_face_img_path, "json_data": front_face_json_path},
  48. "front_edge": {"image": front_edge_img_path, "json_data": front_edge_json_path},
  49. "back_face": {"image": back_face_img_path, "json_data": back_face_json_path},
  50. "back_edge": {"image": back_edge_img_path, "json_data": back_edge_json_path}}
  51. for k, v in data.items():
  52. resp = upload_image_with_json(
  53. url=send_url,
  54. image_path=v['image'],
  55. image_type=k,
  56. image_name="",
  57. json_path=v['json_data']
  58. )
  59. print(f"{k} Status:", resp.status_code)
  60. print("Response:", resp.text)
  61. if __name__ == "__main__":
  62. send4img()
  63. # resp = upload_image_with_json(
  64. # url="http://127.0.0.1:7745/api/images/insert/6",
  65. # image_path="v2-28c2dced87172b004d40eedc38e7889a_b.gif",
  66. # image_type="front_face",
  67. # image_name="星海",
  68. # json_data={"2343": "阿萨"}
  69. # )
  70. #
  71. # print("Status:", resp.status_code)
  72. # print("Response:", resp.text)