#!/usr/bin/env python # -*- coding: utf-8 -*- """ 测试身份证照片(102x126)注册优化 验证智能预处理功能是否正常工作 """ import cv2 import numpy as np from face_feature_extractor import FaceFeatureExtractor def create_test_image(width=102, height=126): """创建模拟身份证照片大小的测试图像""" # 创建一个简单的测试图像(灰色背景) img = np.ones((height, width, 3), dtype=np.uint8) * 200 # 绘制一个简单的人脸轮廓(椭圆) center_x, center_y = width // 2, height // 2 cv2.ellipse(img, (center_x, center_y), (width//3, height//3), 0, 0, 360, (180, 150, 120), -1) # 绘制眼睛 cv2.circle(img, (center_x - 15, center_y - 15), 5, (50, 50, 50), -1) cv2.circle(img, (center_x + 15, center_y - 15), 5, (50, 50, 50), -1) # 绘制嘴巴 cv2.ellipse(img, (center_x, center_y + 20), (20, 10), 0, 0, 180, (100, 50, 50), 2) return img def test_preprocess_function(): """测试预处理函数""" print("=" * 60) print("测试1: 验证 _preprocess_small_face 函数") print("=" * 60) extractor = FaceFeatureExtractor() # 创建102x126的测试图像 test_img = create_test_image(102, 126) print(f"原始图像尺寸: {test_img.shape[1]}x{test_img.shape[0]}") # 测试预处理 processed = extractor._preprocess_small_face( test_img, min_width=112, min_height=112, apply_sharpening=True ) print(f"预处理后尺寸: {processed.shape[1]}x{processed.shape[0]}") # 验证尺寸 assert processed.shape[1] >= 112, "宽度应该 >= 112" assert processed.shape[0] >= 112, "高度应该 >= 112" print("✓ 预处理功能正常") print() return True def test_resolution_check(): """测试分辨率检查""" print("=" * 60) print("测试2: 验证分辨率检查逻辑") print("=" * 60) extractor = FaceFeatureExtractor() # 测试小于112的图像 small_img = np.ones((100, 100, 3), dtype=np.uint8) * 200 print(f"小图像尺寸: {small_img.shape[1]}x{small_img.shape[0]}") processed_small = extractor._preprocess_small_face(small_img) print(f"预处理后: {processed_small.shape[1]}x{processed_small.shape[0]}") # 检查是否通过分辨率检查 passed = extractor.brightness_checker.check_resolution(processed_small) print(f"分辨率检查: {'✓ 通过' if passed else '✗ 未通过'}") assert passed, "预处理后应该通过分辨率检查" # 测试已经足够大的图像 large_img = np.ones((150, 150, 3), dtype=np.uint8) * 200 print(f"\n大图像尺寸: {large_img.shape[1]}x{large_img.shape[0]}") processed_large = extractor._preprocess_small_face(large_img) print(f"预处理后: {processed_large.shape[1]}x{processed_large.shape[0]}") print("(大图像不需要放大,应该保持原样)") assert processed_large.shape == large_img.shape, "大图像不应该被修改" print("✓ 分辨率检查逻辑正常") print() return True def test_id_card_scenario(): """测试身份证照片场景""" print("=" * 60) print("测试3: 模拟身份证照片(102x126)场景") print("=" * 60) extractor = FaceFeatureExtractor() # 创建102x126的身份证照片 id_card_photo = create_test_image(102, 126) print(f"身份证照片尺寸: {id_card_photo.shape[1]}x{id_card_photo.shape[0]}") # 测试预处理 processed = extractor._preprocess_small_face(id_card_photo) print(f"预处理后尺寸: {processed.shape[1]}x{processed.shape[0]}") # 验证所有检查 brightness_ok = extractor.brightness_checker.check_bright(processed) resolution_ok = extractor.brightness_checker.check_resolution(processed) print(f"\n质量检查结果:") print(f" 亮度检查: {'✓ 通过' if brightness_ok else '✗ 未通过'}") print(f" 分辨率检查: {'✓ 通过' if resolution_ok else '✗ 未通过'}") assert resolution_ok, "身份证照片预处理后应该通过分辨率检查" print("\n✓ 身份证照片场景测试通过") print() return True def test_scale_calculation(): """测试缩放比例计算""" print("=" * 60) print("测试4: 验证缩放比例计算") print("=" * 60) test_cases = [ (102, 126, 112, 112), # 身份证场景 (90, 90, 112, 112), # 正方形小图 (200, 100, 112, 112), # 宽图 (100, 200, 112, 112), # 高图 ] for w, h, min_w, min_h in test_cases: scale_w = min_w / w if w < min_w else 1.0 scale_h = min_h / h if h < min_h else 1.0 scale = max(scale_w, scale_h) new_w = int(w * scale) new_h = int(h * scale) print(f"输入: {w}x{h} -> 输出: {new_w}x{new_h} (比例: {scale:.3f})") assert new_w >= min_w and new_h >= min_h, \ f"输出尺寸应该满足最小要求 ({min_w}x{min_h})" print("\n✓ 缩放比例计算正确") print() return True def main(): """运行所有测试""" print("\n[START] 开始测试身份证照片优化功能\n") try: test_preprocess_function() test_resolution_check() test_id_card_scenario() test_scale_calculation() print("=" * 60) print("[PASS] 所有测试通过!") print("=" * 60) print("\n[INFO] 优化说明:") print("1. 身份证照片(102x126)现在可以通过质量检查") print("2. 使用双三次插值保证放大质量") print("3. 应用USM锐化增强清晰度") print("4. 对正常尺寸照片无影响") print("5. 保持原有质量标准不降低") print("\n[OK] 可以安全部署到生产环境") return True except Exception as e: print(f"\n[ERROR] 测试失败: {e}") import traceback traceback.print_exc() return False if __name__ == "__main__": success = main() exit(0 if success else 1)