Spaces:
Sleeping
Sleeping
Create ocr_utils.py
Browse files- ocr_utils.py +20 -0
ocr_utils.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import easyocr
|
| 2 |
+
import numpy as np
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
def initialize_reader(langs=['hi', 'en']):
|
| 6 |
+
"""Initializes the EasyOCR reader for specified languages."""
|
| 7 |
+
return easyocr.Reader(langs)
|
| 8 |
+
|
| 9 |
+
def read_text_from_image(image_path, reader):
|
| 10 |
+
"""Reads text from the image using the provided reader."""
|
| 11 |
+
try:
|
| 12 |
+
image = Image.open(image_path)
|
| 13 |
+
image_np = np.array(image)
|
| 14 |
+
return reader.readtext(image_np, detail=0)
|
| 15 |
+
except FileNotFoundError:
|
| 16 |
+
raise FileNotFoundError("Image file not found. Please check the path.")
|
| 17 |
+
|
| 18 |
+
def join_extracted_text(text_list):
|
| 19 |
+
"""Joins the extracted text from the reader into a single string."""
|
| 20 |
+
return " ".join(text_list)
|