Andrii Fedorenko commited on
Commit
8d3334b
·
1 Parent(s): 1c73aa0

Implement OCR functionality with Gradio interface and model downloads

Browse files
Files changed (2) hide show
  1. app.py +51 -4
  2. requirements.txt +7 -0
app.py CHANGED
@@ -1,7 +1,54 @@
1
  import gradio as gr
 
 
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
 
 
 
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import numpy as np
3
+ import cv2
4
+ import onnxruntime as ort
5
+ import os
6
+ from huggingface_hub import hf_hub_download
7
 
8
+ # Function to download model files from Hugging Face
9
+ def download_model_files():
10
+ model_repo = "SWHL/RapidOCR"
11
+ det_model_filename = "PP-OCRv4/en_PP-OCRv3_det_infer.onnx"
12
+ rec_model_filename = "PP-OCRv4/ch_PP-OCRv4_rec_server_infer.onnx"
13
+ cls_model_filename = "PP-OCRv3/ch_ppocr_mobile_v2.0_cls_train.onnx"
14
 
15
+ det_model_path = hf_hub_download(repo_id=model_repo, filename=det_model_filename)
16
+ rec_model_path = hf_hub_download(repo_id=model_repo, filename=rec_model_filename)
17
+ cls_model_path = hf_hub_download(repo_id=model_repo, filename=cls_model_filename)
18
+
19
+ return det_model_path, rec_model_path, cls_model_path
20
+
21
+ # Download model files
22
+ det_model_path, rec_model_path, cls_model_path = download_model_files()
23
+
24
+ # Load models
25
+ det_session = ort.InferenceSession(det_model_path)
26
+ rec_session = ort.InferenceSession(rec_model_path)
27
+ cls_session = ort.InferenceSession(cls_model_path)
28
+
29
+ def preprocess_image(image):
30
+ # Convert to grayscale
31
+ gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
32
+ # Resize or pad image to desired size if necessary
33
+ return gray
34
+
35
+ def ocr_predict(image):
36
+ # Preprocess the image
37
+ preprocessed_image = preprocess_image(image)
38
+ # Perform detection, classification, and recognition using the ONNX models
39
+ # This is a placeholder for the actual OCR pipeline
40
+ # Replace with your model's inference code
41
+ text = "Detected text goes here"
42
+ return text
43
+
44
+ # Define Gradio interface
45
+ iface = gr.Interface(
46
+ fn=ocr_predict,
47
+ inputs=gr.Image(type="numpy", label="Upload Image"),
48
+ outputs=gr.Textbox(label="Detected Text"),
49
+ title="RapidOCR Gradio Demo",
50
+ description="Upload an image to extract text using RapidOCR."
51
+ )
52
+
53
+ if __name__ == "__main__":
54
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ gradio
2
+ transformers
3
+ onnxruntime
4
+ numpy
5
+ opencv-python
6
+ torch
7
+ huggingface-hub