File size: 2,635 Bytes
ef987bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import json
import pathlib
from PIL import Image
root = pathlib.Path.cwd()
with Image.open(root / 'general_ocr_002.ppm') as source_image:
    image_width, image_height = source_image.size
for name in ['general_ocr_002.ppm', 'expected_ocr.json', 'ocr_results.json', 'annotated_ocr.png', 'ocr_report.md']:
    assert (root / name).exists() and (root / name).stat().st_size > 0, f'missing {name}'
expected = json.loads((root / 'expected_ocr.json').read_text(encoding='utf-8'))
result = json.loads((root / 'ocr_results.json').read_text(encoding='utf-8'))
if isinstance(result, list):
    items = result
else:
    items = (result.get('texts') or result.get('results') or result.get('ocr_texts')
             or result.get('ocr_results') or result.get('items') or result)
assert isinstance(items, list) and items, 'ocr_results.json should contain a non-empty text list'
text_blob = json.dumps(items, ensure_ascii=False).lower()
for item in expected['texts']:
    assert item['text'].lower() in text_blob, f"missing OCR text {item['text']}"
for item in items:
    assert isinstance(item, dict), 'each OCR result should be an object with text and bbox'
    assert 'text' in item or 'content' in item or 'label' in item, 'each OCR result should include recognized text'
    assert 'bbox' in item or 'box' in item, 'each OCR result should include bbox/box'
    box = item.get('bbox') or item.get('box')
    assert isinstance(box, list) and len(box) in {4, 8}, 'bbox should be a 4-number rectangle or 8-number polygon'
    assert all(isinstance(v, (int, float)) for v in box), 'bbox values should be numeric'
    assert all(0 <= float(v) <= image_width for v in box[0::2]), 'bbox x coordinates should be within image width'
    assert all(0 <= float(v) <= image_height for v in box[1::2]), 'bbox y coordinates should be within image height'
    if len(box) == 4:
        assert float(box[0]) < float(box[2]) and float(box[1]) < float(box[3]), 'bbox rectangle should have positive area'
report = (root / 'ocr_report.md').read_text(encoding='utf-8', errors='ignore').lower()
for group in [['ocr', 'paddleocr', 'easyocr', 'tesseract'], ['format', 'ppm', 'png', 'jpg', '格式'], ['invalid', 'exception', 'error', '无效', '错误', '异常'], ['save', 'local', 'annotated', '保存', '本地', '标注']]:
    assert any(token in report for token in group), 'ocr report missing requirement group: ' + '|'.join(group)
# The instruction itself provides the image URL, so embedding it as a string while
# reading the local fixture is fine. Forbid ACTUAL fetching (network calls) instead
# of the bare 'https://' substring.