img_data_insert.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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(send_url):
  38. front_face_img_path = r"C:\Code\ML\Project\CheckCardBoxAndDefectServer\_temp_work\test\正面面\rectify_center_img.jpg"
  39. front_face_json_path = r"C:\Code\ML\Project\CheckCardBoxAndDefectServer\_temp_work\test\正面面\front_face_score.json"
  40. front_edge_img_path = r"C:\Code\ML\Project\CheckCardBoxAndDefectServer\_temp_work\test\正面边角\rectify_center_img.jpg"
  41. front_edge_json_path = r"C:\Code\ML\Project\CheckCardBoxAndDefectServer\_temp_work\test\正面边角\front_corner_edge_score.json"
  42. back_face_img_path = r"C:\Code\ML\Project\CheckCardBoxAndDefectServer\_temp_work\test\背面面\rectify_center_img.jpg"
  43. back_face_json_path = r"C:\Code\ML\Project\CheckCardBoxAndDefectServer\_temp_work\test\背面面\back_face_score.json"
  44. back_edge_img_path = r"C:\Code\ML\Project\CheckCardBoxAndDefectServer\_temp_work\test\背面边角\rectify_center_img.jpg"
  45. back_edge_json_path = r"C:\Code\ML\Project\CheckCardBoxAndDefectServer\_temp_work\test\背面边角\back_corner_edge_score.json"
  46. data = {"front_face": {"image": front_face_img_path, "json_data": front_face_json_path},
  47. "front_edge": {"image": front_edge_img_path, "json_data": front_edge_json_path},
  48. "back_face": {"image": back_face_img_path, "json_data": back_face_json_path},
  49. "back_edge": {"image": back_edge_img_path, "json_data": back_edge_json_path}}
  50. for k, v in data.items():
  51. resp = upload_image_with_json(
  52. url=send_url,
  53. image_path=v['image'],
  54. image_type=k,
  55. image_name="",
  56. json_path=v['json_data']
  57. )
  58. print(f"{k} Status:", resp.status_code)
  59. print("Response:", resp.text)
  60. if __name__ == "__main__":
  61. # send_url = "http://127.0.0.1:7745/api/images/insert/9"
  62. send_url = "http://192.168.31.243:7745/api/images/insert/3"
  63. send4img(send_url)
  64. # resp = upload_image_with_json(
  65. # url="http://127.0.0.1:7745/api/images/insert/6",
  66. # image_path="v2-28c2dced87172b004d40eedc38e7889a_b.gif",
  67. # image_type="front_face",
  68. # image_name="星海",
  69. # json_data={"2343": "阿萨"}
  70. # )
  71. #
  72. # print("Status:", resp.status_code)
  73. # print("Response:", resp.text)