MCPcopy Create free account
hub / github.com/NanoNets/docstrange / test_ocr_debug

Function test_ocr_debug

tests/debug_ocr.py:11–81  ·  view source on GitHub ↗

Test OCR with detailed debugging.

()

Source from the content-addressed store, hash-verified

9logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
10
11def test_ocr_debug():
12 """Test OCR with detailed debugging."""
13 print("🔍 OCR Debug Test")
14 print("=" * 50)
15
16 # Create a simple test image with text
17 from PIL import Image, ImageDraw, ImageFont
18 import tempfile
19
20 # Create a test image with text
21 img = Image.new('RGB', (400, 200), color='white')
22 draw = ImageDraw.Draw(img)
23
24 # Try to use a font, fallback to default if not available
25 try:
26 font = ImageFont.truetype("/System/Library/Fonts/Arial.ttf", 20)
27 except:
28 try:
29 font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 20)
30 except:
31 font = ImageFont.load_default()
32
33 # Add some text to the image
34 draw.text((50, 50), "Hello World!", fill='black', font=font)
35 draw.text((50, 100), "This is a test image", fill='black', font=font)
36 draw.text((50, 150), "for OCR testing", fill='black', font=font)
37
38 # Save to temporary file
39 with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as tmp:
40 img.save(tmp.name)
41 test_image_path = tmp.name
42
43 print(f"📸 Created test image: {test_image_path}")
44 print(f"📏 Image size: {img.size}")
45
46 try:
47 # Test OCR directly
48 print("\n🤖 Testing OCR Service Directly")
49 print("-" * 30)
50
51 from docstrange.services.ocr_service import OCRServiceFactory
52
53 ocr_service = OCRServiceFactory.create_service()
54 print("✅ OCR service created")
55
56 # Test simple OCR
57 print("\n📝 Testing simple OCR...")
58 simple_result = ocr_service.extract_text(test_image_path)
59 print(f"Simple OCR result: '{simple_result}'")
60
61 # Test layout-aware OCR
62 print("\n📋 Testing layout-aware OCR...")
63 layout_result = ocr_service.extract_text_with_layout(test_image_path)
64 print(f"Layout OCR result: '{layout_result}'")
65
66 # Test with extractor
67 print("\n🔄 Testing with DocumentExtractor...")
68 extractor = DocumentExtractor(ocr_enabled=True)

Callers 1

debug_ocr.pyFile · 0.85

Calls 5

extractMethod · 0.95
DocumentExtractorClass · 0.90
create_serviceMethod · 0.80
extract_textMethod · 0.45

Tested by

no test coverage detected