-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathtest_openocr.py
More file actions
380 lines (333 loc) · 14.3 KB
/
test_openocr.py
File metadata and controls
380 lines (333 loc) · 14.3 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
"""
OpenOCR Test Script
Tests all OpenOCR tasks using both Python API and command-line interface.
"""
import os
import sys
import subprocess
from pathlib import Path
import json
# Add parent directory to path for imports
__dir__ = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, __dir__)
from tools.download_example_images import download_example_images
from tools.utils.logging import get_logger
logger = get_logger(name='test_openocr')
class OpenOCRTester:
"""Test suite for OpenOCR functionality"""
def __init__(self):
"""Initialize tester and download test images"""
logger.info('=' * 80)
logger.info('OpenOCR Test Suite')
logger.info('=' * 80)
# Download test images
logger.info('\n📥 Downloading test images...')
self.image_paths = download_example_images()
# Verify image paths
self.ocr_images = Path(self.image_paths.get('ocr', ''))
self.rec_images = Path(self.image_paths.get('unirec', '')) / '..' / 'rec' # Use rec folder
self.doc_images = Path(self.image_paths.get('doc', ''))
self.unirec_images = Path(self.image_paths.get('unirec', ''))
# Create output directory
self.output_dir = Path('test_output')
self.output_dir.mkdir(exist_ok=True)
logger.info('\n📁 Test image directories:')
logger.info(f' OCR/Det: {self.ocr_images}')
logger.info(f' Rec: {self.rec_images}')
logger.info(f' Doc: {self.doc_images}')
logger.info(f' UniRec: {self.unirec_images}')
logger.info(f' Output: {self.output_dir}')
def get_test_image(self, image_dir):
"""Get first valid image from directory"""
if not image_dir.exists():
logger.warning(f'Directory not found: {image_dir}')
return None
for ext in ['.jpg', '.jpeg', '.png', '.bmp']:
images = list(image_dir.glob(f'*{ext}'))
if images:
return str(images[0])
logger.warning(f'No images found in: {image_dir}')
return None
def test_python_api(self):
"""Test OpenOCR using Python API"""
logger.info('\n' + '=' * 80)
logger.info('🐍 Testing Python API')
logger.info('=' * 80)
from openocr import OpenOCR
# Test 1: Detection task
logger.info('\n[Test 1/4] Testing Detection Task...')
try:
test_img = self.get_test_image(self.ocr_images)
if test_img:
openocr_det = OpenOCR(task='det', use_gpu='auto')
results = openocr_det(image_path=test_img)
boxes = results[0]['boxes']
logger.info(f'✅ Detection: Found {len(boxes)} text regions')
else:
logger.warning('⚠️ Detection: No test image available')
except Exception as e:
logger.error(f'❌ Detection failed: {e}')
import traceback
traceback.print_exc()
# Test 2: Recognition task
logger.info('\n[Test 2/4] Testing Recognition Task...')
try:
test_img = self.get_test_image(self.rec_images)
if not test_img:
# Fallback to ocr images
test_img = self.get_test_image(self.ocr_images)
if test_img:
openocr_rec = OpenOCR(task='rec', mode='mobile', use_gpu='auto')
results = openocr_rec(image_path=test_img, batch_num=1)
text = results[0]['text']
score = results[0]['score']
logger.info(f"✅ Recognition: Text='{text}', Score={score:.3f}")
else:
logger.warning('⚠️ Recognition: No test image available')
except Exception as e:
logger.error(f'❌ Recognition failed: {e}')
import traceback
traceback.print_exc()
# Test 3: OCR task (detection + recognition)
logger.info('\n[Test 3/4] Testing OCR Task (Detection + Recognition)...')
try:
test_img = self.get_test_image(self.ocr_images)
if test_img:
openocr_e2e = OpenOCR(task='ocr', mode='mobile', use_gpu='auto')
output_path = self.output_dir / 'ocr_test'
results, time_dicts = openocr_e2e(
image_path=test_img,
save_dir=str(output_path),
is_visualize=True,
rec_batch_num=6
)
for result in results:
image_name, ocr_result = result.split('\t')
ocr_result = json.loads(ocr_result)
logger.info(f'✅ OCR: {image_name} results: {ocr_result}')
logger.info(f'✅ OCR: Processed successfully, results saved to {output_path}')
else:
logger.warning('⚠️ OCR: No test image available')
except Exception as e:
logger.error(f'❌ OCR failed: {e}')
import traceback
traceback.print_exc()
# Test 4: UniRec task
logger.info('\n[Test 4/4] Testing UniRec Task...')
try:
test_img = self.get_test_image(self.unirec_images)
if test_img:
openocr_unirec = OpenOCR(task='unirec', use_gpu='auto', auto_download=True)
result_text, generated_ids = openocr_unirec(
image_path=test_img,
max_length=2048
)
logger.info(f'✅ UniRec: Generated {len(generated_ids)} tokens')
logger.info(f' Text preview: {result_text[:100]}...' if len(result_text) > 100 else f' Text: {result_text}')
else:
logger.warning('⚠️ UniRec: No test image available')
except Exception as e:
logger.error(f'❌ UniRec failed: {e}')
import traceback
traceback.print_exc()
# Test 5: Doc task
logger.info('\n[Test 5/5] Testing Doc Task...')
try:
test_img = self.get_test_image(self.doc_images)
if test_img:
openocr_doc = OpenOCR(
task='doc',
use_gpu='auto',
auto_download=True,
use_layout_detection=True
)
result = openocr_doc(
image_path=test_img,
layout_threshold=0.4,
max_length=2048
)
# Save results
output_path = self.output_dir / 'doc_test'
output_path.mkdir(exist_ok=True)
openocr_doc.save_to_json(result, str(output_path))
openocr_doc.save_to_markdown(result, str(output_path))
# Only save visualization if layout_results exists
if 'layout_results' in result:
openocr_doc.save_visualization(result, str(output_path))
logger.info(f'✅ Doc: Processed successfully, results saved to {output_path}')
else:
logger.warning('⚠️ Doc: No test image available')
except Exception as e:
logger.error(f'❌ Doc failed: {e}')
import traceback
traceback.print_exc()
logger.info('\n' + '=' * 80)
logger.info('✅ Python API tests completed')
logger.info('=' * 80)
def test_command_line(self):
"""Test OpenOCR using command-line interface"""
logger.info('\n' + '=' * 80)
logger.info('💻 Testing Command-Line Interface')
logger.info('=' * 80)
# Test 1: Detection task
logger.info('\n[Test 1/4] Testing Detection Command...')
try:
test_img = self.get_test_image(self.ocr_images)
if test_img:
output_path = self.output_dir / 'cli_det'
cmd = [
sys.executable, 'openocr.py',
'--task', 'det',
'--input_path', test_img,
'--output_path', str(output_path),
'--is_vis'
]
logger.info(f"Running: {' '.join(cmd)}")
result = subprocess.run(cmd, cwd=__dir__, capture_output=True, text=True)
if result.returncode == 0:
logger.info('✅ Detection CLI: Success')
else:
logger.error(f'❌ Detection CLI failed: {result.stderr}')
else:
logger.warning('⚠️ Detection CLI: No test image available')
except Exception as e:
logger.error(f'❌ Detection CLI failed: {e}')
# Test 2: Recognition task
logger.info('\n[Test 2/4] Testing Recognition Command...')
try:
test_img = self.get_test_image(self.rec_images)
if not test_img:
test_img = self.get_test_image(self.ocr_images)
if test_img:
output_path = self.output_dir / 'cli_rec'
cmd = [
sys.executable, 'openocr.py',
'--task', 'rec',
'--input_path', test_img,
'--output_path', str(output_path),
'--mode', 'mobile'
]
logger.info(f"Running: {' '.join(cmd)}")
result = subprocess.run(cmd, cwd=__dir__, capture_output=True, text=True)
if result.returncode == 0:
logger.info('✅ Recognition CLI: Success')
else:
logger.error(f'❌ Recognition CLI failed: {result.stderr}')
else:
logger.warning('⚠️ Recognition CLI: No test image available')
except Exception as e:
logger.error(f'❌ Recognition CLI failed: {e}')
# Test 3: OCR task
logger.info('\n[Test 3/4] Testing OCR Command...')
try:
test_img = self.get_test_image(self.ocr_images)
if test_img:
output_path = self.output_dir / 'cli_ocr'
cmd = [
sys.executable, 'openocr.py',
'--task', 'ocr',
'--input_path', test_img,
'--output_path', str(output_path),
'--is_vis',
'--mode', 'mobile'
]
logger.info(f"Running: {' '.join(cmd)}")
result = subprocess.run(cmd, cwd=__dir__, capture_output=True, text=True)
if result.returncode == 0:
logger.info('✅ OCR CLI: Success')
else:
logger.error(f'❌ OCR CLI failed: {result.stderr}')
else:
logger.warning('⚠️ OCR CLI: No test image available')
except Exception as e:
logger.error(f'❌ OCR CLI failed: {e}')
# Test 4: UniRec task
logger.info('\n[Test 4/4] Testing UniRec Command...')
try:
test_img = self.get_test_image(self.unirec_images)
if test_img:
output_path = self.output_dir / 'cli_unirec'
cmd = [
sys.executable, 'openocr.py',
'--task', 'unirec',
'--input_path', test_img,
'--output_path', str(output_path),
'--max_length', '2048'
]
logger.info(f"Running: {' '.join(cmd)}")
result = subprocess.run(cmd, cwd=__dir__, capture_output=True, text=True)
if result.returncode == 0:
logger.info('✅ UniRec CLI: Success')
else:
logger.error(f'❌ UniRec CLI failed: {result.stderr}')
else:
logger.warning('⚠️ UniRec CLI: No test image available')
except Exception as e:
logger.error(f'❌ UniRec CLI failed: {e}')
# Test 5: Doc task
logger.info('\n[Test 5/5] Testing Doc Command...')
try:
test_img = self.get_test_image(self.doc_images)
if test_img:
output_path = self.output_dir / 'cli_doc'
cmd = [
sys.executable, 'openocr.py',
'--task', 'doc',
'--input_path', test_img,
'--output_path', str(output_path),
'--save_vis',
'--save_json',
'--save_markdown',
'--use_layout_detection'
]
logger.info(f"Running: {' '.join(cmd)}")
result = subprocess.run(cmd, cwd=__dir__, capture_output=True, text=True)
if result.returncode == 0:
logger.info('✅ Doc CLI: Success')
else:
logger.error(f'❌ Doc CLI failed: {result.stderr}')
else:
logger.warning('⚠️ Doc CLI: No test image available')
except Exception as e:
logger.error(f'❌ Doc CLI failed: {e}')
logger.info('\n' + '=' * 80)
logger.info('✅ Command-line tests completed')
logger.info('=' * 80)
def run_all_tests(self):
"""Run all tests"""
logger.info('\n🚀 Starting OpenOCR test suite...\n')
# Test Python API
self.test_python_api()
# Test Command-line
self.test_command_line()
logger.info('\n' + '=' * 80)
logger.info('🎉 All tests completed!')
logger.info(f'📁 Test outputs saved to: {self.output_dir.absolute()}')
logger.info('=' * 80)
def main():
"""Main test entry point"""
import argparse
parser = argparse.ArgumentParser(description='Test OpenOCR functionality')
parser.add_argument(
'--test-type',
type=str,
default='all',
choices=['all', 'python', 'cli'],
help='Type of tests to run: all (default), python (API only), cli (command-line only)'
)
args = parser.parse_args()
try:
tester = OpenOCRTester()
if args.test_type == 'all':
tester.run_all_tests()
elif args.test_type == 'python':
tester.test_python_api()
elif args.test_type == 'cli':
tester.test_command_line()
except Exception as e:
logger.error(f'Test suite failed: {e}')
import traceback
traceback.print_exc()
sys.exit(1)
if __name__ == '__main__':
main()