Andrii Fedorenko commited on
Commit ·
5925098
1
Parent(s): 8d3334b
Implement OCR functionality with Gradio interface and model downloads
Browse files- app.py +19 -7
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -32,14 +32,26 @@ def preprocess_image(image):
|
|
| 32 |
# Resize or pad image to desired size if necessary
|
| 33 |
return gray
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
def ocr_predict(image):
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
#
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
# Define Gradio interface
|
| 45 |
iface = gr.Interface(
|
|
|
|
| 32 |
# Resize or pad image to desired size if necessary
|
| 33 |
return gray
|
| 34 |
|
| 35 |
+
import cv2
|
| 36 |
+
from rapidocr_onnxruntime import RapidOCR
|
| 37 |
+
|
| 38 |
+
# Initialize the RapidOCR engine
|
| 39 |
+
ocr_engine = RapidOCR()
|
| 40 |
+
|
| 41 |
def ocr_predict(image):
|
| 42 |
+
"""
|
| 43 |
+
Perform OCR on the input image and return the extracted text.
|
| 44 |
+
"""
|
| 45 |
+
# Convert the image from RGB to BGR format as OpenCV uses BGR
|
| 46 |
+
image_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 47 |
+
|
| 48 |
+
# Perform OCR
|
| 49 |
+
result, _ = ocr_engine(image_bgr)
|
| 50 |
+
|
| 51 |
+
# Extract text from the result
|
| 52 |
+
extracted_text = "\n".join([item[1] for item in result])
|
| 53 |
+
|
| 54 |
+
return extracted_text
|
| 55 |
|
| 56 |
# Define Gradio interface
|
| 57 |
iface = gr.Interface(
|
requirements.txt
CHANGED
|
@@ -4,4 +4,5 @@ onnxruntime
|
|
| 4 |
numpy
|
| 5 |
opencv-python
|
| 6 |
torch
|
| 7 |
-
huggingface-hub
|
|
|
|
|
|
| 4 |
numpy
|
| 5 |
opencv-python
|
| 6 |
torch
|
| 7 |
+
huggingface-hub
|
| 8 |
+
rapidocr_onnxruntime
|