Test suite for OpenOCR functionality
| 20 | |
| 21 | |
| 22 | class OpenOCRTester: |
| 23 | """Test suite for OpenOCR functionality""" |
| 24 | |
| 25 | def __init__(self): |
| 26 | """Initialize tester and download test images""" |
| 27 | logger.info('=' * 80) |
| 28 | logger.info('OpenOCR Test Suite') |
| 29 | logger.info('=' * 80) |
| 30 | |
| 31 | # Download test images |
| 32 | logger.info('\n📥 Downloading test images...') |
| 33 | self.image_paths = download_example_images() |
| 34 | |
| 35 | # Verify image paths |
| 36 | self.ocr_images = Path(self.image_paths.get('ocr', '')) |
| 37 | self.rec_images = Path(self.image_paths.get('unirec', '')) / '..' / 'rec' # Use rec folder |
| 38 | self.doc_images = Path(self.image_paths.get('doc', '')) |
| 39 | self.unirec_images = Path(self.image_paths.get('unirec', '')) |
| 40 | |
| 41 | # Create output directory |
| 42 | self.output_dir = Path('test_output') |
| 43 | self.output_dir.mkdir(exist_ok=True) |
| 44 | |
| 45 | logger.info('\n📁 Test image directories:') |
| 46 | logger.info(f' OCR/Det: {self.ocr_images}') |
| 47 | logger.info(f' Rec: {self.rec_images}') |
| 48 | logger.info(f' Doc: {self.doc_images}') |
| 49 | logger.info(f' UniRec: {self.unirec_images}') |
| 50 | logger.info(f' Output: {self.output_dir}') |
| 51 | |
| 52 | def get_test_image(self, image_dir): |
| 53 | """Get first valid image from directory""" |
| 54 | if not image_dir.exists(): |
| 55 | logger.warning(f'Directory not found: {image_dir}') |
| 56 | return None |
| 57 | |
| 58 | for ext in ['.jpg', '.jpeg', '.png', '.bmp']: |
| 59 | images = list(image_dir.glob(f'*{ext}')) |
| 60 | if images: |
| 61 | return str(images[0]) |
| 62 | |
| 63 | logger.warning(f'No images found in: {image_dir}') |
| 64 | return None |
| 65 | |
| 66 | def test_python_api(self): |
| 67 | """Test OpenOCR using Python API""" |
| 68 | logger.info('\n' + '=' * 80) |
| 69 | logger.info('🐍 Testing Python API') |
| 70 | logger.info('=' * 80) |
| 71 | |
| 72 | from openocr import OpenOCR |
| 73 | |
| 74 | # Test 1: Detection task |
| 75 | logger.info('\n[Test 1/4] Testing Detection Task...') |
| 76 | try: |
| 77 | test_img = self.get_test_image(self.ocr_images) |
| 78 | if test_img: |
| 79 | openocr_det = OpenOCR(task='det', use_gpu='auto') |