Spaces:
Sleeping
Sleeping
File size: 3,500 Bytes
43267ee 2b5a39a 43267ee | 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 | #!/usr/bin/env python3
# minimal_test_paddle.py - Minimal test to isolate the OCR issue
import sys
import os
import json
import fitz
from paddleocr import PaddleOCR
def test_ocr():
if len(sys.argv) < 2:
print(json.dumps({"error": "No file path provided"}))
return
file_path = sys.argv[1]
try:
print(f"Testing OCR on: {file_path}", file=sys.stderr)
# Test 1: Can we open the PDF?
print("Opening PDF...", file=sys.stderr)
doc = fitz.open(file_path)
print(f"PDF has {len(doc)} pages", file=sys.stderr)
# Test 2: Convert first page to image
print("Converting first page to image...", file=sys.stderr)
page = doc[0]
mat = fitz.Matrix(150/72, 150/72)
pix = page.get_pixmap(matrix=mat)
temp_img = "/tmp/test_page.png"
pix.save(temp_img)
if os.path.exists(temp_img):
img_size = os.path.getsize(temp_img)
print(f"Image created: {temp_img} (size: {img_size} bytes, {pix.width}x{pix.height})", file=sys.stderr)
else:
print("Failed to create image", file=sys.stderr)
doc.close()
return
doc.close()
# Test 3: Initialize OCR
print("Initializing OCR...", file=sys.stderr)
ocr = PaddleOCR(use_angle_cls=True, lang='en', show_log=False)
print("OCR initialized", file=sys.stderr)
# Test 4: Run OCR on the image
print("Running OCR...", file=sys.stderr)
result = ocr.ocr(temp_img, cls=True)
print(f"OCR result type: {type(result)}", file=sys.stderr)
if result:
print(f"Result length: {len(result)}", file=sys.stderr)
if result[0]:
print(f"First page has {len(result[0])} detections", file=sys.stderr)
# Print all detected text
for i, detection in enumerate(result[0]):
if len(detection) >= 2:
text = detection[1][0] if isinstance(detection[1], (list, tuple)) else str(detection[1])
conf = detection[1][1] if isinstance(detection[1], (list, tuple)) and len(detection[1]) > 1 else 1.0
print(f"Detection {i}: '{text}' (confidence: {conf})", file=sys.stderr)
else:
print("First page result is empty", file=sys.stderr)
else:
print("OCR returned None", file=sys.stderr)
# Clean up
if os.path.exists(temp_img):
os.unlink(temp_img)
# Return simple result
text_found = ""
if result and result[0]:
for detection in result[0]:
if len(detection) >= 2:
# Convert to string to handle both string and float values
text_value = str(detection[1][0]) if isinstance(detection[1], (list, tuple)) else str(detection[1])
text_found += text_value + "\n"
print(json.dumps({
"success": True,
"text": text_found,
"detections": len(result[0]) if result and result[0] else 0
}))
except Exception as e:
print(f"Error: {e}", file=sys.stderr)
import traceback
traceback.print_exc(file=sys.stderr)
print(json.dumps({"success": False, "error": str(e)}))
if __name__ == "__main__":
test_ocr() |