Autoweight / ocr_engine.py
Sanjayraju30's picture
Update ocr_engine.py
b902588 verified
raw
history blame
1.6 kB
import re
import cv2
import numpy as np
from PIL import Image
from paddleocr import PaddleOCR
# βœ… Initialize PaddleOCR once
ocr = PaddleOCR(use_angle_cls=False, lang='en', show_log=False)
def preprocess_image(image):
"""
Convert to grayscale and enhance contrast to help OCR.
"""
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
processed = cv2.adaptiveThreshold(
gray, 255,
cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY_INV,
11, 2
)
return processed
def extract_weight_from_image(pil_image):
try:
# βœ… Convert to OpenCV format
image = np.array(pil_image.convert("RGB"))
# βœ… Preprocess image
processed = preprocess_image(image)
# Optional: Save debug image
debug_path = "debug_processed_image.png"
Image.fromarray(processed).save(debug_path)
print(f"[DEBUG] Saved preprocessed image to {debug_path}")
# βœ… Run OCR on original (RGB) image (not preprocessed)
result = ocr.ocr(image, cls=False)
print("πŸ” PaddleOCR Results:")
for line in result[0]:
text = line[1][0]
conf = line[1][1]
print(f" β€’ Text: '{text}' | Confidence: {conf*100:.2f}%")
# Try to extract number like 53.25 or 100
match = re.search(r"\b\d{1,4}(\.\d{1,2})?\b", text)
if match:
return match.group(), round(conf * 100, 2)
return "No weight detected", 0.0
except Exception as e:
print(f"❌ OCR Error: {e}")
return f"Error: {str(e)}", 0.0