File size: 670 Bytes
030432c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import numpy as np
from PIL import Image
from io import BytesIO
import re
import logging
import easyocr
ocr_reader = easyocr.Reader(['en'], gpu=False)
def extract_text_from_image(image_file):
try:
image = Image.open(BytesIO(image_file.read())).convert("RGB")
image_np = np.array(image)
results = ocr_reader.readtext(image_np)
text = " ".join([res[1] for res in results])
text = re.sub(r'\b(Ingredients|Komposisi|Composition|Bahan|Daftar Bahan)\b', '', text, flags=re.IGNORECASE)
return re.sub(r'[^A-Za-z,.\s-]', '', text).strip()
except Exception as e:
logging.error(f"OCR error: {e}")
return ""
|