-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_script.py
More file actions
75 lines (63 loc) · 2.08 KB
/
Copy pathdebug_script.py
File metadata and controls
75 lines (63 loc) · 2.08 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
import sys
import asyncio
from pathlib import Path
from PIL import Image, ImageOps
import logging
# Add current directory to path so we can import app
sys.path.append(".")
# Mock fastapi stuff if needed, but we are just importing functions
try:
from app import _ocr_text, _caption_image, _parse_data, _ensure_ocr_reader, _ensure_model
except ImportError as e:
print(f"Import failed: {e}")
sys.exit(1)
# Setup logging
logging.basicConfig(level=logging.WARNING)
# also silence app logger
logging.getLogger("app").setLevel(logging.WARNING)
def test_extraction():
# List of extensions to check
extensions = {".jpg", ".jpeg", ".png", ".JPG", ".JPEG", ".PNG"}
image_files = [
f for f in Path(".").iterdir()
if f.is_file() and f.suffix in extensions
]
if not image_files:
print("No images found.")
return
for image_path in sorted(image_files):
print(f"\n{'='*20}")
print(f"Processing {image_path.name}...")
print(f"{'='*20}")
try:
image = Image.open(image_path).convert("RGB")
image = ImageOps.exif_transpose(image)
except Exception as e:
print(f"Failed to open image: {e}")
continue
print("Running OCR...")
try:
ocr_text = _ocr_text(image, filename=image_path.name)
print("--- OCR Text ---")
print(ocr_text)
print("--- Repr ---")
print(repr(ocr_text))
print("----------------")
except Exception as e:
print(f"OCR failed: {e}")
ocr_text = ""
print("Running Captioning...")
try:
caption = _caption_image(image)
print("--- Caption ---")
print(caption)
print("---------------")
except Exception as e:
print(f"Captioning failed: {e}")
caption = ""
print("Extracting Fields...")
data = _parse_data(ocr_text, caption)
import json
print(json.dumps(data, indent=2))
if __name__ == "__main__":
test_extraction()