You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
2.7 KiB
83 lines
2.7 KiB
#!/usr/bin/env python3
|
|
import sys
|
|
import os
|
|
sys.path.insert(0, '/tmp/ocr_venv/lib/python3.12/site-packages')
|
|
import cv2
|
|
import numpy as np
|
|
import pytesseract
|
|
|
|
# 读取图片
|
|
image_path = "/root/.openclaw/media/inbound/5cbd7469-e34b-4ba8-9942-9f1a6ee002e4---d97c6b88-4b7d-4a8e-9829-629f4883fe67.jpg"
|
|
|
|
try:
|
|
# 读取图片
|
|
img = cv2.imread(image_path)
|
|
if img is None:
|
|
print("错误: 无法读取图片文件")
|
|
sys.exit(1)
|
|
|
|
print(f"原始图片尺寸: {img.shape}")
|
|
|
|
# 1. 调整大小(放大)
|
|
height, width = img.shape[:2]
|
|
new_width = width * 2
|
|
new_height = height * 2
|
|
resized = cv2.resize(img, (new_width, new_height), interpolation=cv2.INTER_CUBIC)
|
|
|
|
# 2. 转换为灰度图
|
|
gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)
|
|
|
|
# 3. 应用CLAHE(对比度受限自适应直方图均衡化)
|
|
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
|
|
enhanced = clahe.apply(gray)
|
|
|
|
# 4. 高斯模糊去噪
|
|
blurred = cv2.GaussianBlur(enhanced, (3, 3), 0)
|
|
|
|
# 5. 自适应阈值
|
|
thresh = cv2.adaptiveThreshold(blurred, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
|
|
cv2.THRESH_BINARY, 11, 2)
|
|
|
|
# 保存处理后的图片
|
|
temp_path = "/tmp/enhanced_ocr.jpg"
|
|
cv2.imwrite(temp_path, thresh)
|
|
print(f"已保存增强图片到: {temp_path}")
|
|
|
|
# 尝试多种OCR配置
|
|
configs = [
|
|
('--psm 6 -l eng', "PSM 6 (英文)"),
|
|
('--psm 6 -l chi_sim', "PSM 6 (简体中文)"),
|
|
('--psm 6 -l eng+chi_sim', "PSM 6 (英文+简体中文)"),
|
|
('--psm 3 -l eng+chi_sim', "PSM 3 (全自动)"),
|
|
('--psm 1 -l eng+chi_sim', "PSM 1 (自动页面分割)"),
|
|
('--psm 7 -l eng', "PSM 7 (单行英文)"),
|
|
('--psm 11 -l eng+chi_sim', "PSM 11 (稀疏文本)"),
|
|
('--psm 12 -l eng+chi_sim', "PSM 12 (稀疏文本+对齐)"),
|
|
]
|
|
|
|
best_result = ""
|
|
best_score = 0
|
|
|
|
for config, desc in configs:
|
|
print(f"\n=== 尝试配置: {desc} ===")
|
|
try:
|
|
text = pytesseract.image_to_string(thresh, config=config)
|
|
print(f"识别结果:\n{text}")
|
|
|
|
# 简单评分:计算非空格字符数
|
|
score = len([c for c in text if c.strip()])
|
|
if score > best_score:
|
|
best_score = score
|
|
best_result = text
|
|
except Exception as e:
|
|
print(f"配置 {config} 失败: {e}")
|
|
|
|
print(f"\n{'='*50}")
|
|
print(f"最佳识别结果 (字符数: {best_score}):")
|
|
print(f"{'='*50}")
|
|
print(best_result)
|
|
|
|
except Exception as e:
|
|
print(f"处理过程中出错: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|