full_client.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import requests
  2. import json
  3. import os
  4. import argparse
  5. def process_image(image_path, output_path, host, port):
  6. """上传文件到服务器并保存 JSON 结果"""
  7. url = f"http://{host}:{port}/process"
  8. print(f"[*] 正在处理: {image_path}")
  9. try:
  10. with open(image_path, "rb") as f:
  11. files = {"image": (os.path.basename(image_path), f)}
  12. response = requests.post(url, files=files, timeout=300)
  13. response.raise_for_status()
  14. result = response.json()
  15. if "error" in result:
  16. print(f"[!] 处理失败: {result['error']}")
  17. return
  18. print(f"[+] 处理成功")
  19. out_dir = os.path.dirname(os.path.abspath(output_path))
  20. if out_dir:
  21. os.makedirs(out_dir, exist_ok=True)
  22. with open(output_path, "w", encoding='utf-8') as f:
  23. json.dump(result, f, indent=2, ensure_ascii=False)
  24. print(f"[+] JSON 已保存: {output_path}")
  25. except Exception as e:
  26. print(f"[!] 发生错误: {e}")
  27. if __name__ == "__main__":
  28. parser = argparse.ArgumentParser(description="Floorplan 分割客户端")
  29. parser.add_argument("-i", "--image", type=str, required=True, help="需要处理的图片路径")
  30. parser.add_argument("-o", "--output", type=str, required=True, help="输出 JSON 路径")
  31. parser.add_argument("--host", type=str, default=os.getenv("API_HOST", "192.168.9.52"))
  32. parser.add_argument("--port", type=str, default=os.getenv("API_PORT", "8070"))
  33. args = parser.parse_args()
  34. process_image(args.image, args.output, args.host, args.port)