Download task_032/tests/validate_outputs.py from Lego-X/UXInteract: direct link, hf CLI and curl.
- Browser
- Download file 2.64 kB
-
https://huggingface.co/datasets/Lego-X/UXInteract/resolve/main/task_032/tests/validate_outputs.py
- Command line
-
hf download hf://datasets/Lego-X/UXInteract/task_032/tests/validate_outputs.py
-
curl -L -o validate_outputs.py https://huggingface.co/datasets/Lego-X/UXInteract/resolve/main/task_032/tests/validate_outputs.py
2.64 kB
| 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. | |