Download app/utils/ocr.py from Maulidaaa/predict: direct link, hf CLI and curl.
- Browser
- Download file 670 Bytes
-
https://huggingface.co/spaces/Maulidaaa/predict/resolve/main/app/utils/ocr.py
- Command line
-
hf download hf://spaces/Maulidaaa/predict/app/utils/ocr.py
-
curl -L -o ocr.py https://huggingface.co/spaces/Maulidaaa/predict/resolve/main/app/utils/ocr.py
670 Bytes
| 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 "" | |