Spaces:
Running
Running
sinful1992 commited on
Commit ·
d3ab1b0
1
Parent(s): b364fa7
chore(ocr): add per-step timing logs to identify bottleneck
Browse files- main.py +9 -0
- ocr/reader.py +10 -1
main.py
CHANGED
|
@@ -89,6 +89,9 @@ async def ocr_receipt(
|
|
| 89 |
detail=f"Unsupported media type: {content_type}. Upload an image file.",
|
| 90 |
)
|
| 91 |
|
|
|
|
|
|
|
|
|
|
| 92 |
raw_bytes = await file.read()
|
| 93 |
if not raw_bytes:
|
| 94 |
raise HTTPException(status_code=400, detail="Empty file uploaded")
|
|
@@ -97,14 +100,20 @@ async def ocr_receipt(
|
|
| 97 |
image = preprocess_image(raw_bytes)
|
| 98 |
except (ValueError, FileNotFoundError) as exc:
|
| 99 |
raise HTTPException(status_code=400, detail=f"Image preprocessing failed: {exc}")
|
|
|
|
|
|
|
| 100 |
|
| 101 |
try:
|
| 102 |
blocks = _reader.extract(image)
|
| 103 |
except Exception as exc:
|
| 104 |
logger.exception("OCR extraction failed")
|
| 105 |
raise HTTPException(status_code=500, detail=f"OCR failed: {exc}")
|
|
|
|
|
|
|
| 106 |
|
| 107 |
result = parse_blocks(blocks)
|
|
|
|
|
|
|
| 108 |
|
| 109 |
if debug:
|
| 110 |
result["_raw_blocks"] = blocks
|
|
|
|
| 89 |
detail=f"Unsupported media type: {content_type}. Upload an image file.",
|
| 90 |
)
|
| 91 |
|
| 92 |
+
import time
|
| 93 |
+
t0 = time.perf_counter()
|
| 94 |
+
|
| 95 |
raw_bytes = await file.read()
|
| 96 |
if not raw_bytes:
|
| 97 |
raise HTTPException(status_code=400, detail="Empty file uploaded")
|
|
|
|
| 100 |
image = preprocess_image(raw_bytes)
|
| 101 |
except (ValueError, FileNotFoundError) as exc:
|
| 102 |
raise HTTPException(status_code=400, detail=f"Image preprocessing failed: {exc}")
|
| 103 |
+
t1 = time.perf_counter()
|
| 104 |
+
logger.info("TIMING preprocess: %.2fs | image size: %dx%d", t1 - t0, image.shape[1], image.shape[0])
|
| 105 |
|
| 106 |
try:
|
| 107 |
blocks = _reader.extract(image)
|
| 108 |
except Exception as exc:
|
| 109 |
logger.exception("OCR extraction failed")
|
| 110 |
raise HTTPException(status_code=500, detail=f"OCR failed: {exc}")
|
| 111 |
+
t2 = time.perf_counter()
|
| 112 |
+
logger.info("TIMING ocr extract: %.2fs | blocks found: %d", t2 - t1, len(blocks))
|
| 113 |
|
| 114 |
result = parse_blocks(blocks)
|
| 115 |
+
t3 = time.perf_counter()
|
| 116 |
+
logger.info("TIMING parse: %.2fs | items: %d | total: %.2fs", t3 - t2, len(result.get("line_items", [])), t3 - t0)
|
| 117 |
|
| 118 |
if debug:
|
| 119 |
result["_raw_blocks"] = blocks
|
ocr/reader.py
CHANGED
|
@@ -53,8 +53,17 @@ class PaddleOCRReader:
|
|
| 53 |
img_height = image.shape[0]
|
| 54 |
logo_cutoff_y = img_height * 0.10
|
| 55 |
|
|
|
|
|
|
|
| 56 |
clean = boost_contrast(image)
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
# Apply normal confidence threshold to everything outside the logo zone
|
| 60 |
filtered = []
|
|
|
|
| 53 |
img_height = image.shape[0]
|
| 54 |
logo_cutoff_y = img_height * 0.10
|
| 55 |
|
| 56 |
+
import time
|
| 57 |
+
t0 = time.perf_counter()
|
| 58 |
clean = boost_contrast(image)
|
| 59 |
+
t1 = time.perf_counter()
|
| 60 |
+
logger.info("TIMING boost_contrast: %.2fs", t1 - t0)
|
| 61 |
+
clahe = _enhance_clahe(clean)
|
| 62 |
+
t2 = time.perf_counter()
|
| 63 |
+
logger.info("TIMING clahe: %.2fs", t2 - t1)
|
| 64 |
+
merged = self._run_ocr(clahe, logo_confidence)
|
| 65 |
+
t3 = time.perf_counter()
|
| 66 |
+
logger.info("TIMING model predict: %.2fs", t3 - t2)
|
| 67 |
|
| 68 |
# Apply normal confidence threshold to everything outside the logo zone
|
| 69 |
filtered = []
|