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