Spaces:
Running
Running
import easyocr | |
import numpy as np | |
import cv2 | |
import re | |
reader = easyocr.Reader(['en'], gpu=False) | |
def enhance_image(img): | |
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | |
# CLAHE (adaptive histogram equalization for better contrast) | |
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8)) | |
contrast = clahe.apply(gray) | |
# Sharpen image | |
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]]) | |
sharpened = cv2.filter2D(contrast, -1, kernel) | |
# Resize if very small | |
h, w = sharpened.shape | |
if max(h, w) < 500: | |
sharpened = cv2.resize(sharpened, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC) | |
return sharpened | |
def extract_weight_from_image(pil_img): | |
try: | |
img = np.array(pil_img) | |
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) # convert from PIL to OpenCV BGR | |
# Preprocess image | |
processed = enhance_image(img) | |
results = reader.readtext(processed) | |
best_weight = None | |
best_conf = 0.0 | |
for (bbox, text, conf) in results: | |
original_text = text | |
text = text.lower().strip() | |
# Fix common OCR errors | |
text = text.replace(",", ".") | |
text = text.replace("o", "0").replace("O", "0") | |
text = text.replace("s", "5").replace("S", "5") | |
text = text.replace("g", "9").replace("G", "6") | |
text = text.replace("kgs", "").replace("kg", "") | |
text = re.sub(r"[^\d\.]", "", text) | |
if re.fullmatch(r"\d{1,4}(\.\d{1,3})?", text): | |
if conf > best_conf: | |
best_weight = text | |
best_conf = conf | |
if not best_weight: | |
return "Not detected", 0.0 | |
# Format output | |
if "." in best_weight: | |
int_part, dec_part = best_weight.split(".") | |
int_part = int_part.lstrip("0") or "0" | |
best_weight = f"{int_part}.{dec_part}" | |
else: | |
best_weight = best_weight.lstrip("0") or "0" | |
return best_weight, round(best_conf * 100, 2) | |
except Exception as e: | |
return f"Error: {str(e)}", 0.0 | |