388 lines
11 KiB
Markdown
388 lines
11 KiB
Markdown
# 现成的 Tesseract Docker 镜像及使用方案
|
||
|
||
确实有多个现成的 Tesseract Docker 镜像可以直接使用,无需自己构建。以下是基于现有镜像的简单 Docker 命令方案:
|
||
|
||
## 1. 可用的现成 Tesseract Docker 镜像
|
||
|
||
### 1.1 官方和社区镜像
|
||
```bash
|
||
# 1. 官方 Tesseract OCR 镜像
|
||
docker pull tesseractshadow/tesseract4re:latest
|
||
|
||
# 2. 另一个流行的社区镜像
|
||
docker pull jitesh/tesseract-ocr:latest
|
||
|
||
# 3. 轻量级 Alpine 版本
|
||
docker pull alexbrah/tesseract-ocr:alpine
|
||
|
||
# 4. 包含多种语言的镜像
|
||
docker pull napcs/tesseract-ocr:latest
|
||
```
|
||
|
||
## 2. 最简单的 Docker 使用方案
|
||
|
||
### 2.1 基础使用命令
|
||
```bash
|
||
# 查看镜像中的语言包
|
||
docker run --rm tesseractshadow/tesseract4re tesseract --list-langs
|
||
|
||
# 直接运行单次识别
|
||
docker run --rm -v "$(pwd):/data" tesseractshadow/tesseract4re \
|
||
tesseract /data/input.png /data/output
|
||
|
||
# 中文
|
||
docker run --rm -v "$(pwd):/data" tesseractshadow/tesseract4re \
|
||
tesseract /data/input.png /data/output -l chi_sim
|
||
|
||
# 中文简体 + 英文
|
||
docker run --rm -v "$(pwd):/data" tesseractshadow/tesseract4re \
|
||
tesseract /data/input.png /data/output -l chi_sim+eng
|
||
|
||
# Hex
|
||
docker run --rm \
|
||
-v "$(pwd):/data" \
|
||
tesseractshadow/tesseract4re \
|
||
tesseract /data/hex1.png /data/output \
|
||
-c tessedit_char_whitelist=0123456789ABCDEFabcdef \
|
||
--psm 6
|
||
|
||
|
||
# Base64
|
||
docker run --rm \
|
||
-v "$(pwd):/data" \
|
||
tesseractshadow/tesseract4re \
|
||
tesseract /data/hex1.png /data/output \
|
||
-c tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/= \
|
||
--psm 6
|
||
|
||
```
|
||
|
||
### 2.2 针对 HEX 和 Base64 的优化使用
|
||
```bash
|
||
# 创建专用配置目录
|
||
mkdir -p tesseract-config
|
||
mkdir -p test-images
|
||
mkdir -p output
|
||
|
||
# 创建 HEX 配置文件
|
||
cat > tesseract-config/hex.config << 'EOF'
|
||
tessedit_char_whitelist 0123456789ABCDEFabcdef
|
||
tessedit_pageseg_mode 6
|
||
tessedit_ocr_engine_mode 3
|
||
EOF
|
||
|
||
# 创建 Base64 配置文件
|
||
cat > tesseract-config/base64.config << 'EOF'
|
||
tessedit_char_whitelist ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=
|
||
tessedit_pageseg_mode 6
|
||
tessedit_ocr_engine_mode 3
|
||
EOF
|
||
```
|
||
|
||
|
||
|
||
## 3. 完整的验证脚本
|
||
|
||
### 3.1 创建测试图像
|
||
```bash
|
||
# create-test-images.py
|
||
#!/usr/bin/env python3
|
||
from PIL import Image, ImageDraw, ImageFont
|
||
import os
|
||
import random
|
||
import string
|
||
|
||
def create_test_images():
|
||
"""创建测试图像"""
|
||
os.makedirs('test-images', exist_ok=True)
|
||
|
||
# HEX 测试图像
|
||
hex_chars = "0123456789ABCDEF"
|
||
for i in range(5):
|
||
text = ''.join(random.choices(hex_chars, k=16))
|
||
create_image(f"test-images/hex_{i}.png", text)
|
||
|
||
# Base64 测试图像
|
||
base64_chars = string.ascii_letters + string.digits + "+/="
|
||
for i in range(5):
|
||
text = ''.join(random.choices(base64_chars, k=24))
|
||
create_image(f"test-images/base64_{i}.png", text)
|
||
|
||
def create_image(filename, text):
|
||
"""创建测试图像"""
|
||
img = Image.new('RGB', (400, 50), color='white')
|
||
draw = ImageDraw.Draw(img)
|
||
|
||
try:
|
||
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 20)
|
||
except:
|
||
font = ImageFont.load_default()
|
||
|
||
draw.text((10, 15), text, fill='black', font=font)
|
||
img.save(filename)
|
||
print(f"Created: {filename} - {text}")
|
||
|
||
if __name__ == "__main__":
|
||
create_test_images()
|
||
```
|
||
|
||
### 3.2 运行测试脚本创建图像
|
||
```bash
|
||
# 在主机上运行 Python 脚本创建测试图像
|
||
python3 create-test-images.py
|
||
|
||
# 或者使用 Docker 运行(如果主机没有 Python)
|
||
docker run --rm -v "$(pwd):/app" python:3.9-slim \
|
||
python3 /app/create-test-images.py
|
||
```
|
||
|
||
## 4. 批量识别脚本
|
||
|
||
### 4.1 创建批量处理脚本
|
||
```bash
|
||
# batch-recognize.sh
|
||
#!/bin/bash
|
||
|
||
echo "Starting batch recognition of HEX and Base64 images..."
|
||
|
||
# 创建输出目录
|
||
mkdir -p output
|
||
|
||
# 识别 HEX 图像
|
||
echo "Processing HEX images..."
|
||
for img in test-images/hex_*.png; do
|
||
if [ -f "$img" ]; then
|
||
filename=$(basename "$img" .png)
|
||
echo "Processing: $img"
|
||
|
||
docker run --rm \
|
||
-v "$(pwd):/data" \
|
||
tesseractshadow/tesseract4re \
|
||
tesseract "/data/$img" "/data/output/${filename}" \
|
||
-c tessedit_char_whitelist=0123456789ABCDEFabcdef \
|
||
--psm 6
|
||
fi
|
||
done
|
||
|
||
# 识别 Base64 图像
|
||
echo "Processing Base64 images..."
|
||
for img in test-images/base64_*.png; do
|
||
if [ -f "$img" ]; then
|
||
filename=$(basename "$img" .png)
|
||
echo "Processing: $img"
|
||
|
||
docker run --rm \
|
||
-v "$(pwd):/data" \
|
||
tesseractshadow/tesseract4re \
|
||
tesseract "/data/$img" "/data/output/${filename}" \
|
||
-c tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/= \
|
||
--psm 6
|
||
fi
|
||
done
|
||
|
||
echo "Recognition complete! Check output directory for results."
|
||
```
|
||
|
||
### 4.2 运行批量识别
|
||
```bash
|
||
# 给脚本执行权限
|
||
chmod +x batch-recognize.sh
|
||
|
||
# 运行批量识别
|
||
./batch-recognize.sh
|
||
```
|
||
|
||
## 5. 验证和结果分析
|
||
|
||
### 5.1 创建结果验证脚本
|
||
```bash
|
||
# validate-results.py
|
||
#!/usr/bin/env python3
|
||
import os
|
||
import re
|
||
import base64
|
||
|
||
def read_original_text():
|
||
"""从文件名读取原始文本(假设文件名包含原始文本)"""
|
||
originals = {}
|
||
for img_file in os.listdir('test-images'):
|
||
if img_file.startswith('hex_'):
|
||
name = img_file.replace('.png', '')
|
||
# 从文件名提取文本(实际使用时应该从其他地方获取)
|
||
originals[name] = "placeholder_hex"
|
||
elif img_file.startswith('base64_'):
|
||
name = img_file.replace('.png', '')
|
||
originals[name] = "placeholder_base64"
|
||
return originals
|
||
|
||
def validate_hex(text):
|
||
"""验证 HEX 文本"""
|
||
cleaned = re.sub(r'[^0-9A-Fa-f]', '', text)
|
||
is_valid = len(cleaned) > 0 and all(c in '0123456789ABCDEFabcdef' for c in cleaned)
|
||
return {
|
||
'cleaned': cleaned.upper(),
|
||
'is_valid': is_valid,
|
||
'length': len(cleaned)
|
||
}
|
||
|
||
def validate_base64(text):
|
||
"""验证 Base64 文本"""
|
||
cleaned = ''.join(c for c in text if c in 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=')
|
||
|
||
try:
|
||
# 尝试解码
|
||
if '=' in cleaned:
|
||
base64.b64decode(cleaned)
|
||
is_valid = True
|
||
else:
|
||
padding = 4 - (len(cleaned) % 4)
|
||
test_text = cleaned + '=' * padding
|
||
base64.b64decode(test_text)
|
||
is_valid = True
|
||
except:
|
||
is_valid = False
|
||
|
||
return {
|
||
'cleaned': cleaned,
|
||
'is_valid': is_valid,
|
||
'length': len(cleaned)
|
||
}
|
||
|
||
def main():
|
||
"""主验证函数"""
|
||
print("Validating OCR results...")
|
||
|
||
results = {}
|
||
|
||
# 读取输出文件
|
||
for txt_file in os.listdir('output'):
|
||
if txt_file.endswith('.txt'):
|
||
file_path = os.path.join('output', txt_file)
|
||
with open(file_path, 'r') as f:
|
||
content = f.read().strip()
|
||
|
||
filename = txt_file.replace('.txt', '')
|
||
|
||
if filename.startswith('hex_'):
|
||
validation = validate_hex(content)
|
||
results[filename] = {
|
||
'type': 'hex',
|
||
'original': content,
|
||
'validation': validation
|
||
}
|
||
elif filename.startswith('base64_'):
|
||
validation = validate_base64(content)
|
||
results[filename] = {
|
||
'type': 'base64',
|
||
'original': content,
|
||
'validation': validation
|
||
}
|
||
|
||
# 输出统计信息
|
||
hex_results = [r for r in results.values() if r['type'] == 'hex']
|
||
base64_results = [r for r in results.values() if r['type'] == 'base64']
|
||
|
||
print(f"\nHEX Results: {len(hex_results)} files")
|
||
hex_valid = sum(1 for r in hex_results if r['validation']['is_valid'])
|
||
print(f"Valid HEX: {hex_valid}/{len(hex_results)} ({hex_valid/len(hex_results)*100:.1f}%)")
|
||
|
||
print(f"\nBase64 Results: {len(base64_results)} files")
|
||
base64_valid = sum(1 for r in base64_results if r['validation']['is_valid'])
|
||
print(f"Valid Base64: {base64_valid}/{len(base64_results)} ({base64_valid/len(base64_results)*100:.1f}%)")
|
||
|
||
# 显示详细结果
|
||
print("\nDetailed Results:")
|
||
for filename, result in results.items():
|
||
status = "✓" if result['validation']['is_valid'] else "✗"
|
||
print(f"{status} {filename}: {result['original'][:50]}... -> {result['validation']['cleaned'][:30]}...")
|
||
|
||
if __name__ == "__main__":
|
||
main()
|
||
```
|
||
|
||
### 5.2 运行验证
|
||
```bash
|
||
# 运行验证脚本
|
||
python3 validate-results.py
|
||
```
|
||
|
||
## 6. 交互式测试容器
|
||
|
||
### 6.1 启动交互式容器进行测试
|
||
```bash
|
||
# 启动交互式容器
|
||
docker run -it --rm \
|
||
-v "$(pwd):/data" \
|
||
--name tesseract-test \
|
||
tesseractshadow/tesseract4re \
|
||
/bin/bash
|
||
|
||
# 在容器内手动测试
|
||
cd /data
|
||
tesseract test-images/hex_0.png stdout -c tessedit_char_whitelist=0123456789ABCDEFabcdef --psm 6
|
||
tesseract test-images/base64_0.png stdout -c tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/= --psm 6
|
||
```
|
||
|
||
## 7. 一键测试脚本
|
||
|
||
### 7.1 创建完整测试流程
|
||
```bash
|
||
# run-all-tests.sh
|
||
#!/bin/bash
|
||
|
||
echo "=== Tesseract HEX/Base64 OCR Test ==="
|
||
|
||
# 1. 创建测试图像
|
||
echo "Step 1: Creating test images..."
|
||
python3 create-test-images.py
|
||
|
||
# 2. 批量识别
|
||
echo "Step 2: Running batch OCR..."
|
||
./batch-recognize.sh
|
||
|
||
# 3. 验证结果
|
||
echo "Step 3: Validating results..."
|
||
python3 validate-results.py
|
||
|
||
echo "=== Test Complete ==="
|
||
```
|
||
|
||
## 8. 使用不同镜像的对比测试
|
||
|
||
### 8.1 多镜像测试脚本
|
||
```bash
|
||
# compare-images.sh
|
||
#!/bin/bash
|
||
|
||
IMAGES=("tesseractshadow/tesseract4re" "jitesh/tesseract-ocr")
|
||
|
||
for image in "${IMAGES[@]}"; do
|
||
echo "Testing with image: $image"
|
||
docker pull $image
|
||
|
||
mkdir -p "output-${image##*/}"
|
||
|
||
# 测试单张图像
|
||
docker run --rm \
|
||
-v "$(pwd):/data" \
|
||
$image \
|
||
tesseract "/data/test-images/hex_0.png" stdout \
|
||
-c tessedit_char_whitelist=0123456789ABCDEFabcdef \
|
||
--psm 6 > "output-${image##*/}/test_result.txt"
|
||
|
||
echo "Result for $image:"
|
||
cat "output-${image##*/}/test_result.txt"
|
||
echo "---"
|
||
done
|
||
```
|
||
|
||
## 总结
|
||
|
||
使用现成的 Tesseract Docker 镜像非常简单:
|
||
|
||
1. **拉取镜像**:`docker pull tesseractshadow/tesseract4re`
|
||
2. **运行识别**:使用 `docker run --rm -v` 挂载目录
|
||
3. **配置优化**:通过 `-c tessedit_char_whitelist` 设置字符白名单
|
||
4. **批量处理**:通过脚本批量处理多张图像
|
||
|
||
这种方法无需构建自定义镜像,直接使用现有镜像即可快速验证 Tesseract 对 HEX 和 Base64 字符的识别效果。通过字符白名单和适当的 PSM 参数,可以显著提高识别准确率。 |