99 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			99 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
#!/usr/bin/env python
 | 
						|
# -*- coding: utf-8 -*-
 | 
						|
"""
 | 
						|
LPRNet接口真实图片测试脚本
 | 
						|
测试LPRNET_part目录下的真实车牌图片
 | 
						|
"""
 | 
						|
 | 
						|
import cv2
 | 
						|
import numpy as np
 | 
						|
import os
 | 
						|
from LPRNET_part.lpr_interface import LPRNinitialize_model, LPRNmodel_predict
 | 
						|
 | 
						|
def test_real_images():
 | 
						|
    """
 | 
						|
    测试LPRNET_part目录下的真实车牌图片
 | 
						|
    """
 | 
						|
    print("=== LPRNet真实图片测试 ===")
 | 
						|
    
 | 
						|
    # 初始化模型
 | 
						|
    print("1. 初始化LPRNet模型...")
 | 
						|
    success = LPRNinitialize_model()
 | 
						|
    if not success:
 | 
						|
        print("模型初始化失败!")
 | 
						|
        return
 | 
						|
    
 | 
						|
    # 获取LPRNET_part目录下的图片文件
 | 
						|
    lprnet_dir = "LPRNET_part"
 | 
						|
    image_files = []
 | 
						|
    
 | 
						|
    if os.path.exists(lprnet_dir):
 | 
						|
        for file in os.listdir(lprnet_dir):
 | 
						|
            if file.lower().endswith(('.jpg', '.jpeg', '.png', '.bmp')):
 | 
						|
                image_files.append(os.path.join(lprnet_dir, file))
 | 
						|
    
 | 
						|
    if not image_files:
 | 
						|
        print("未找到图片文件!")
 | 
						|
        return
 | 
						|
    
 | 
						|
    print(f"2. 找到 {len(image_files)} 个图片文件")
 | 
						|
    
 | 
						|
    # 测试每个图片
 | 
						|
    for i, image_path in enumerate(image_files, 1):
 | 
						|
        print(f"\n--- 测试图片 {i}: {os.path.basename(image_path)} ---")
 | 
						|
        
 | 
						|
        try:
 | 
						|
            # 使用支持中文路径的方式读取图片
 | 
						|
            image = cv2.imdecode(np.fromfile(image_path, dtype=np.uint8), cv2.IMREAD_COLOR)
 | 
						|
            
 | 
						|
            if image is None:
 | 
						|
                print(f"无法读取图片: {image_path}")
 | 
						|
                continue
 | 
						|
            
 | 
						|
            print(f"图片尺寸: {image.shape}")
 | 
						|
            
 | 
						|
            # 进行预测
 | 
						|
            result = LPRNmodel_predict(image)
 | 
						|
            print(f"识别结果: {result}")
 | 
						|
            print(f"识别车牌号: {''.join(result)}")
 | 
						|
            
 | 
						|
        except Exception as e:
 | 
						|
            print(f"处理图片 {image_path} 时出错: {e}")
 | 
						|
            import traceback
 | 
						|
            traceback.print_exc()
 | 
						|
    
 | 
						|
    print("\n=== 测试完成 ===")
 | 
						|
 | 
						|
def test_image_loading():
 | 
						|
    """
 | 
						|
    测试图片加载方式
 | 
						|
    """
 | 
						|
    print("\n=== 图片加载测试 ===")
 | 
						|
    
 | 
						|
    lprnet_dir = "LPRNET_part"
 | 
						|
    
 | 
						|
    if os.path.exists(lprnet_dir):
 | 
						|
        for file in os.listdir(lprnet_dir):
 | 
						|
            if file.lower().endswith(('.jpg', '.jpeg', '.png', '.bmp')):
 | 
						|
                image_path = os.path.join(lprnet_dir, file)
 | 
						|
                print(f"\n测试文件: {file}")
 | 
						|
                
 | 
						|
                # 方法1: 普通cv2.imread
 | 
						|
                img1 = cv2.imread(image_path)
 | 
						|
                print(f"cv2.imread结果: {img1 is not None}")
 | 
						|
                
 | 
						|
                # 方法2: 支持中文路径的方式
 | 
						|
                try:
 | 
						|
                    img2 = cv2.imdecode(np.fromfile(image_path, dtype=np.uint8), cv2.IMREAD_COLOR)
 | 
						|
                    print(f"cv2.imdecode结果: {img2 is not None}")
 | 
						|
                    if img2 is not None:
 | 
						|
                        print(f"图片尺寸: {img2.shape}")
 | 
						|
                except Exception as e:
 | 
						|
                    print(f"cv2.imdecode失败: {e}")
 | 
						|
 | 
						|
if __name__ == "__main__":
 | 
						|
    # 首先测试图片加载
 | 
						|
    test_image_loading()
 | 
						|
    
 | 
						|
    # 然后测试完整的识别流程
 | 
						|
    test_real_images() |