API测试.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import requests
  2. import os
  3. import io
  4. import zipfile
  5. from PIL import Image, ImageDraw
  6. # --- 配置 ---
  7. # 请确保你的 FastAPI 服务器正在运行,并修改此处的地址和端口
  8. BASE_URL = "http://127.0.0.1:7745/api"
  9. SINGLE_STITCH_URL = f"{BASE_URL}/stitch/"
  10. BATCH_STITCH_URL = f"{BASE_URL}/stitch/batch/"
  11. # --- 测试函数 1:单个拼图接口 ---
  12. def single_puzzle_api(zipfile_path):
  13. """
  14. 测试 /stitch/ 接口 (单个拼图)
  15. 使用 模板匹配法 (template_match)
  16. """
  17. print("--- 1. 开始测试: 单个拼图接口 (/stitch) ---")
  18. # 1. 准备请求数据
  19. form_data = {
  20. 'method': 'template_match',
  21. 'num_cols': 4,
  22. 'num_rows': 6,
  23. 'overlap_h': 405,
  24. 'overlap_v': 440,
  25. 'tm_blend_type': 'half_importance_add_weight',
  26. 'tm_light_compensation': True,
  27. }
  28. try:
  29. # 打开文件
  30. with open(zipfile_path, "rb") as f:
  31. files = {
  32. 'zip_file': (os.path.basename(zipfile_path), f, 'application/zip')
  33. }
  34. # 4. 发送 POST 请求
  35. print("向服务器发送请求...")
  36. response = requests.post(SINGLE_STITCH_URL, data=form_data, files=files) # 设置较长超时
  37. # 5. 处理响应
  38. print(f"服务器响应状态码: {response.status_code}")
  39. if response.status_code == 200:
  40. # 检查响应头,确认是图片
  41. content_type = response.headers.get('content-type')
  42. print(f"响应内容类型: {content_type}")
  43. if 'image/jpeg' in content_type:
  44. # 将返回的图片内容保存到本地文件
  45. output_filename = "stitched_single_result.jpg"
  46. with open(output_filename, "wb") as f:
  47. f.write(response.content)
  48. print(f"✅ 成功! 拼接后的大图已保存为: {output_filename}")
  49. else:
  50. print(f"❌ 失败! 期望得到 'image/jpeg',但收到了 '{content_type}'")
  51. else:
  52. # 如果接口返回错误,打印错误详情
  53. print(f"❌ 请求失败! 错误信息: {response.text}")
  54. except requests.exceptions.RequestException as e:
  55. print(f"❌ 请求异常! 无法连接到服务器: {e}")
  56. print("-" * 40 + "\n")
  57. # --- 测试函数 2:批量拼图接口 ---
  58. def batch_puzzle_api(zipfile_path):
  59. """
  60. 测试 /stitch/batch 接口 (批量拼图)
  61. 使用 点匹配法 (key_point)
  62. """
  63. print("--- 2. 开始测试: 批量拼图接口 (/stitch/batch) ---")
  64. # 1. 准备请求数据 (这次我们测试 key_point 方法)
  65. form_data = {
  66. 'method': 'template_match',
  67. 'num_cols': 4,
  68. 'num_rows': 6,
  69. 'overlap_h': 405,
  70. 'overlap_v': 440,
  71. 'tm_blend_type': 'half_importance_add_weight',
  72. 'tm_light_compensation': True,
  73. }
  74. try:
  75. # 打开文件
  76. with open(zipfile_path, "rb") as f:
  77. files = {
  78. 'zip_file': (os.path.basename(zipfile_path), f, 'application/zip')
  79. }
  80. # 4. 发送 POST 请求
  81. print("向服务器发送请求...")
  82. response = requests.post(BATCH_STITCH_URL, data=form_data, files=files) # 批量处理可能更耗时
  83. # 5. 处理响应
  84. print(f"服务器响应状态码: {response.status_code}")
  85. if response.status_code == 200:
  86. content_type = response.headers.get('content-type')
  87. print(f"响应内容类型: {content_type}")
  88. if 'application/zip' in content_type:
  89. # 将返回的ZIP文件保存到本地
  90. output_filename = "stitched_batch_result.zip"
  91. with open(output_filename, "wb") as f:
  92. f.write(response.content)
  93. print(f"✅ 成功! 包含拼接结果的ZIP包已保存为: {output_filename}")
  94. # (可选) 解压并检查结果
  95. try:
  96. extract_dir = "batch_results_unzipped"
  97. os.makedirs(extract_dir, exist_ok=True)
  98. with zipfile.ZipFile(output_filename, 'r') as zf:
  99. zf.extractall(extract_dir)
  100. print(f" - 结果已自动解压到 '{extract_dir}' 文件夹,包含文件: {os.listdir(extract_dir)}")
  101. except Exception as e:
  102. print(f" - 解压返回的ZIP文件时出错: {e}")
  103. else:
  104. print(f"❌ 失败! 期望得到 'application/zip',但收到了 '{content_type}'")
  105. else:
  106. print(f"❌ 请求失败! 错误信息: {response.text}")
  107. except requests.exceptions.RequestException as e:
  108. print(f"❌ 请求异常! 无法连接到服务器: {e}")
  109. print("-" * 40 + "\n")
  110. if __name__ == "__main__":
  111. # 依次运行两个测试函数
  112. single_puzzle_api(r"C:\Code\ML\Project\StitchImageServer\temp\_250801_1043_0001.zip")
  113. batch_puzzle_api(r"C:\Code\ML\Project\StitchImageServer\temp\Input.zip")