Files
rocr/rusto-rs/test_preprocess.py
ssw c303c315e5 rocr: rusto-rs 源码入库(上游 v0.2.5 基线 57609fc + 本地补丁)
- 源码 316 文件收编为 rusto-rs/ 文件夹,随本仓库管理
- 移除内部 .git(补丁已存档 docs/rusto-rs-metal-patch-20260912.patch,上游可随时 re-clone)
- target/ 与 docs/.yarn 缓存不入库;本地改动:ROCR_* 环境变量调优 + Metal 链接修复 + 预处理测试脚本
2026-09-12 17:30:04 +08:00

57 lines
2.0 KiB
Python

#!/usr/bin/env python3
"""Test preprocessing to compare with Rust"""
import cv2
import numpy as np
import sys
def preprocess_recognition(img, img_h=48, img_w=320):
"""Preprocess image for recognition model (PaddleOCR style)"""
h, w = img.shape[:2]
# Calculate aspect ratio
import math
ratio = w / float(h)
if math.ceil(img_h * ratio) > img_w:
resized_w = img_w
else:
resized_w = int(math.ceil(img_h * ratio))
print(f"Original size: {w}x{h}")
print(f"Resized to: {resized_w}x{img_h}")
# Resize image
resized_image = cv2.resize(img, (resized_w, img_h))
# Check pixel values before normalization
print(f"Image shape after resize: {resized_image.shape}")
print(f"Image dtype: {resized_image.dtype}")
print(f"First pixel (BGR): {resized_image[0, 0]}")
# Normalize: (pixel/255 - 0.5) / 0.5
# Model expects BGR format (cv2.imread already gives BGR)
resized_image = resized_image.astype(np.float32)
resized_image = resized_image.transpose(2, 0, 1) / 255.0
print(f"After transpose and /255: shape={resized_image.shape}")
print(f"First pixel values (CHW): B={resized_image[0, 0, 0]:.6f}, G={resized_image[1, 0, 0]:.6f}, R={resized_image[2, 0, 0]:.6f}")
resized_image = (resized_image - 0.5) / 0.5
print(f"After normalization: B={resized_image[0, 0, 0]:.6f}, G={resized_image[1, 0, 0]:.6f}, R={resized_image[2, 0, 0]:.6f}")
# Pad to img_w
padding_im = np.zeros((3, img_h, img_w), dtype=np.float32)
padding_im[:, :, 0:resized_w] = resized_image
padding_im = np.expand_dims(padding_im, axis=0)
print(f"Final shape: {padding_im.shape}")
return padding_im
if __name__ == "__main__":
img_path = sys.argv[1] if len(sys.argv) > 1 else "debug_output/example1_crops/crop_00.png"
img = cv2.imread(img_path)
print(f"Testing: {img_path}\n")
result = preprocess_recognition(img)
print(f"\nMin value: {result.min():.6f}")
print(f"Max value: {result.max():.6f}")