Spaces:
Running
Running
| import os | |
| import sys | |
| import numpy as np | |
| from PIL import Image | |
| sys.stdout.reconfigure(line_buffering=True) | |
| sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) | |
| # Disable oneDNN PIR bug on Windows | |
| os.environ["FLAGS_use_onednn"] = "0" | |
| os.environ["FLAGS_use_mkldnn"] = "0" | |
| os.environ["FLAGS_enable_pir_api"] = "0" | |
| os.environ["FLAGS_enable_pir_in_executor"] = "0" | |
| import torch | |
| import paddle | |
| from paddleocr import PaddleOCR | |
| print("Initializing PaddleOCR for bounding box extraction...", flush=True) | |
| ocr = PaddleOCR(lang="en", show_log=False) | |
| img_path = "sample_data/sample_receipt.png" | |
| img = Image.open(img_path).convert("RGB") | |
| img_np = np.array(img) | |
| print(f"Running OCR on {img_path} ({img.width}x{img.height} px)...", flush=True) | |
| results = ocr.ocr(img_np) | |
| print(f"Results returned! Type: {type(results)}", flush=True) | |
| if results and results[0]: | |
| print(f"Total detected text boxes: {len(results[0])}", flush=True) | |
| for i, line in enumerate(results[0][:5]): | |
| box = line[0] # [[x1,y1], [x2,y2], [x3,y3], [x4,y4]] | |
| text, conf = line[1] | |
| x1 = min(p[0] for p in box) | |
| y1 = min(p[1] for p in box) | |
| x2 = max(p[0] for p in box) | |
| y2 = max(p[1] for p in box) | |
| print(f"Box #{i+1}: [{int(x1)}, {int(y1)}, {int(x2)}, {int(y2)}] | Conf: {conf:.2f} | Text: '{text}'", flush=True) | |
| else: | |
| print("No results returned.", flush=True) | |