Files
change_hair/test_swaphair.py
2026-07-17 22:13:56 +08:00

35 lines
1.3 KiB
Python

#!/usr/bin/env python3
"""换发型接口测试"""
import base64, json, requests, sys, time
img_path = "/home/ubuntu/change_hair/project/data/userImage/8488902485_20250630055547.jpg"
with open(img_path, "rb") as f:
img_b64 = base64.b64encode(f.read()).decode()
payload = {
"hair_id": "1905785164224868354",
"task_id": "test_swaphair_newtrain",
"is_hr": "true",
"user_img_path": "data:image/jpeg;base64," + img_b64,
"output_format": "base64"
}
print("=== 调用换发型 /api/swapHair/v1 (hair_id=1907641420518580226) ===")
t0 = time.time()
try:
r = requests.post("http://127.0.0.1:8801/api/swapHair/v1", json=payload, timeout=600)
d = r.json()
print(f"HTTP {r.status_code} | msg={d.get('msg')} | state={d.get('state')} | 耗时={time.time()-t0:.1f}s")
data = d.get("data", "")
if d.get("state") == 0 and data:
out = "/home/ubuntu/change_hair/project/logs/test_swaphair_result.jpg"
with open(out, "wb") as f:
f.write(base64.b64decode(data))
print(f"✅ 换发型成功! 结果图已保存: {out} ({len(data)} bytes base64)")
else:
print(f"❌ 换发型失败: {d}")
sys.exit(1)
except Exception as e:
print(f"❌ 请求异常(耗时{time.time()-t0:.1f}s): {e}")
sys.exit(1)