Spaces:
Sleeping
Sleeping
Update ocr_engine.py
Browse files- ocr_engine.py +32 -24
ocr_engine.py
CHANGED
|
@@ -1,28 +1,36 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
from PIL import Image
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
blurred = cv2.GaussianBlur(gray, (3, 3), 0)
|
| 10 |
-
_, thresh = cv2.threshold(blurred, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
| 11 |
-
return thresh
|
| 12 |
|
| 13 |
-
def extract_weight(
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
| 2 |
+
from PIL import Image, ImageEnhance
|
| 3 |
+
import re
|
|
|
|
| 4 |
|
| 5 |
+
# Load TrOCR model
|
| 6 |
+
processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-handwritten")
|
| 7 |
+
model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-handwritten")
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
def extract_weight(image: Image.Image) -> str:
|
| 10 |
+
# Step 1: Preprocess image
|
| 11 |
+
image = image.convert("L") # grayscale
|
| 12 |
+
image = ImageEnhance.Contrast(image).enhance(2.0)
|
| 13 |
+
image = ImageEnhance.Sharpness(image).enhance(2.5)
|
| 14 |
+
image = image.convert("RGB")
|
| 15 |
|
| 16 |
+
# Step 2: Run Hugging Face OCR
|
| 17 |
+
pixel_values = processor(images=image, return_tensors="pt").pixel_values
|
| 18 |
+
generated_ids = model.generate(pixel_values, max_length=32)
|
| 19 |
+
full_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 20 |
|
| 21 |
+
print("OCR Output:", full_text)
|
| 22 |
+
|
| 23 |
+
# Step 3: Extract numeric weight
|
| 24 |
+
cleaned = full_text.lower().replace(" ", "")
|
| 25 |
+
match = re.search(r"(\d+(\.\d+)?)", cleaned)
|
| 26 |
+
weight = match.group(1) if match else None
|
| 27 |
+
|
| 28 |
+
# Step 4: Decide unit
|
| 29 |
+
if any(u in cleaned for u in ["kg", "kgs", "kilo"]):
|
| 30 |
+
unit = "kg"
|
| 31 |
+
elif any(u in cleaned for u in ["g", "gram", "grams"]):
|
| 32 |
+
unit = "grams"
|
| 33 |
+
else:
|
| 34 |
+
unit = "kg" if weight and float(weight) >= 20 else "grams"
|
| 35 |
+
|
| 36 |
+
return f"{weight} {unit}" if weight else "No valid weight detected"
|