face_reg_docker/quick_test_api.py

104 lines
3.8 KiB
Python

import requests
import json
import cv2
import numpy as np
import os
# 配置服务地址
JAVA_BACKEND_URL = "http://localhost:18080"
PYTHON_ALGO_URL = "http://localhost:18000"
def create_test_image(filename="test_face.jpg"):
"""创建一张简单的测试图片(如果不存在)"""
if not os.path.exists(filename):
print(f"Creating test image: {filename}...")
# 创建一个 640x480 的黑色图像
img = np.zeros((480, 640, 3), np.uint8)
# 画一个简单的圆代表"脸" (虽然检测不到,但可以测试文件上传流程)
cv2.circle(img, (320, 240), 100, (255, 255, 255), -1)
cv2.imwrite(filename, img)
return filename
def test_python_health():
"""测试 Python 算法服务健康检查"""
url = f"{PYTHON_ALGO_URL}/health"
print(f"\n[Testing] Python Algorithm Service Health ({url})...")
try:
response = requests.get(url, timeout=5)
if response.status_code == 200:
print(f"✅ Success: {response.json()}")
else:
print(f"❌ Failed: Status {response.status_code}, Response: {response.text}")
except requests.exceptions.ConnectionError:
print("❌ Failed: Connection refused. Is the service running on port 18000?")
except Exception as e:
print(f"❌ Error: {e}")
def test_extract_feature(image_path):
"""测试人脸特征提取接口"""
url = f"{PYTHON_ALGO_URL}/api/extract_feature"
print(f"\n[Testing] Face Feature Extraction ({url})...")
if not os.path.exists(image_path):
print("❌ Error: Test image not found.")
return
try:
with open(image_path, 'rb') as f:
files = {'image': f}
response = requests.post(url, files=files, timeout=10)
if response.status_code == 200:
result = response.json()
if result.get('success'):
dim = result.get('feature_dim')
print(f"✅ Success: Feature extracted. Dimension: {dim}")
else:
# 预期内失败,因为我们的假脸可能过不了检测,但这证明接口通了
print(f"⚠️ Service Reachable (Logic Result): {result.get('message')}")
else:
print(f"❌ Failed: Status {response.status_code}, Response: {response.text}")
except requests.exceptions.ConnectionError:
print("❌ Failed: Connection refused.")
except Exception as e:
print(f"❌ Error: {e}")
def test_java_group_list():
"""测试 Java 后端分组列表接口"""
url = f"{JAVA_BACKEND_URL}/api/groups/list"
print(f"\n[Testing] Java Backend Group List ({url})...")
try:
response = requests.get(url, timeout=5)
if response.status_code == 200:
data = response.json()
if data.get('code') == 200:
print(f"✅ Success: Retrieved {len(data.get('data', []))} groups.")
print(f" Response: {json.dumps(data, ensure_ascii=False)}")
else:
print(f"❌ Functional Error: {data}")
else:
print(f"❌ Failed: Status {response.status_code}, Response: {response.text}")
except requests.exceptions.ConnectionError:
print("❌ Failed: Connection refused. Is the service running on port 18080?")
except Exception as e:
print(f"❌ Error: {e}")
if __name__ == "__main__":
print("=== Face Recognition System Quick Test ===")
# 1. 准备测试图片
test_img = create_test_image()
# 2. 测试算法服务
test_python_health()
test_extract_feature(test_img)
# 3. 测试Java后端
test_java_group_list()
print("\n=== Test Finished ===")
# 清理生成的测试图 (可选,这里保留以便用户查看)
# if os.path.exists(test_img):
# os.remove(test_img)