File size: 2,053 Bytes
1c73aa0 8d3334b 1c73aa0 8d3334b 1c73aa0 8d3334b 5925098 8d3334b 5925098 8d3334b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
import gradio as gr
import numpy as np
import cv2
import onnxruntime as ort
import os
from huggingface_hub import hf_hub_download
# Function to download model files from Hugging Face
def download_model_files():
model_repo = "SWHL/RapidOCR"
det_model_filename = "PP-OCRv4/en_PP-OCRv3_det_infer.onnx"
rec_model_filename = "PP-OCRv4/ch_PP-OCRv4_rec_server_infer.onnx"
cls_model_filename = "PP-OCRv3/ch_ppocr_mobile_v2.0_cls_train.onnx"
det_model_path = hf_hub_download(repo_id=model_repo, filename=det_model_filename)
rec_model_path = hf_hub_download(repo_id=model_repo, filename=rec_model_filename)
cls_model_path = hf_hub_download(repo_id=model_repo, filename=cls_model_filename)
return det_model_path, rec_model_path, cls_model_path
# Download model files
det_model_path, rec_model_path, cls_model_path = download_model_files()
# Load models
det_session = ort.InferenceSession(det_model_path)
rec_session = ort.InferenceSession(rec_model_path)
cls_session = ort.InferenceSession(cls_model_path)
def preprocess_image(image):
# Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Resize or pad image to desired size if necessary
return gray
import cv2
from rapidocr_onnxruntime import RapidOCR
# Initialize the RapidOCR engine
ocr_engine = RapidOCR()
def ocr_predict(image):
"""
Perform OCR on the input image and return the extracted text.
"""
# Convert the image from RGB to BGR format as OpenCV uses BGR
image_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
# Perform OCR
result, _ = ocr_engine(image_bgr)
# Extract text from the result
extracted_text = "\n".join([item[1] for item in result])
return extracted_text
# Define Gradio interface
iface = gr.Interface(
fn=ocr_predict,
inputs=gr.Image(type="numpy", label="Upload Image"),
outputs=gr.Textbox(label="Detected Text"),
title="RapidOCR Gradio Demo",
description="Upload an image to extract text using RapidOCR."
)
if __name__ == "__main__":
iface.launch() |