Test OpenOCR using Python API
(self)
| 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') |
| 80 | results = openocr_det(image_path=test_img) |
| 81 | boxes = results[0]['boxes'] |
| 82 | logger.info(f'✅ Detection: Found {len(boxes)} text regions') |
| 83 | else: |
| 84 | logger.warning('⚠️ Detection: No test image available') |
| 85 | except Exception as e: |
| 86 | logger.error(f'❌ Detection failed: {e}') |
| 87 | import traceback |
| 88 | traceback.print_exc() |
| 89 | |
| 90 | # Test 2: Recognition task |
| 91 | logger.info('\n[Test 2/4] Testing Recognition Task...') |
| 92 | try: |
| 93 | test_img = self.get_test_image(self.rec_images) |
| 94 | if not test_img: |
| 95 | # Fallback to ocr images |
| 96 | test_img = self.get_test_image(self.ocr_images) |
| 97 | |
| 98 | if test_img: |
| 99 | openocr_rec = OpenOCR(task='rec', mode='mobile', use_gpu='auto') |
| 100 | results = openocr_rec(image_path=test_img, batch_num=1) |
| 101 | text = results[0]['text'] |
| 102 | score = results[0]['score'] |
| 103 | logger.info(f"✅ Recognition: Text='{text}', Score={score:.3f}") |
| 104 | else: |
| 105 | logger.warning('⚠️ Recognition: No test image available') |
| 106 | except Exception as e: |
| 107 | logger.error(f'❌ Recognition failed: {e}') |
| 108 | import traceback |
| 109 | traceback.print_exc() |
| 110 | |
| 111 | # Test 3: OCR task (detection + recognition) |
| 112 | logger.info('\n[Test 3/4] Testing OCR Task (Detection + Recognition)...') |
| 113 | try: |
| 114 | test_img = self.get_test_image(self.ocr_images) |
| 115 | if test_img: |
| 116 | openocr_e2e = OpenOCR(task='ocr', mode='mobile', use_gpu='auto') |
| 117 | output_path = self.output_dir / 'ocr_test' |
| 118 | results, time_dicts = openocr_e2e( |
| 119 | image_path=test_img, |
| 120 | save_dir=str(output_path), |
| 121 | is_visualize=True, |
| 122 | rec_batch_num=6 |
| 123 | ) |
no test coverage detected