Spaces:
Build error
Build error
File size: 1,867 Bytes
c28abeb c11fb3a 8f557ef c28abeb 8f557ef c28abeb c11fb3a c28abeb c11fb3a c28abeb c11fb3a cfcd3d5 c11fb3a c28abeb c11fb3a c28abeb c11fb3a 1362c73 c28abeb c11fb3a c28abeb c11fb3a cfcd3d5 c11fb3a cfcd3d5 c28abeb c11fb3a 42b463a 393f381 c28abeb c11fb3a |
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 |
import numpy as np
import re
import cv2
from PIL import Image
import easyocr
import os
# Initialize OCR Reader
reader = easyocr.Reader(['en'], gpu=False)
def preprocess_image(image):
"""Preprocess the image to improve OCR accuracy"""
# Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
# Apply threshold to isolate digits
_, thresh = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
return thresh
def extract_weight_from_image(pil_image):
try:
# Convert PIL to OpenCV format
image = np.array(pil_image.convert("RGB"))
# Print image shape for debugging
print("Image shape:", image.shape)
# Preprocess for better OCR accuracy
processed = preprocess_image(image)
# Save debug image
debug_img = Image.fromarray(processed)
debug_path = "debug_processed_image.png"
debug_img.save(debug_path)
print(f"✅ Processed image saved to: {debug_path}")
# Run OCR on processed image
result = reader.readtext(processed)
print("✅ OCR Results:")
for r in result:
print(f"Text: '{r[1]}' | Confidence: {r[2] * 100:.2f}%")
# Try to find numeric weight
weight = None
confidence = 0.0
for detection in result:
text = detection[1]
conf = detection[2]
# Match numbers like 53.25 or 45
match = re.search(r"\b\d+(\.\d+)?\b", text)
if match:
weight = match.group()
confidence = conf
break
if weight:
return weight, round(confidence * 100, 2)
else:
return "No weight detected", 0.0
except Exception as e:
print("❌ Exception during OCR:", str(e))
return f"Error: {str(e)}", 0.0
|