nit454's picture
Create app.py
4fedfa8 verified
raw
history blame
1.87 kB
import gradio as gr
import numpy as np
import random
from paddleocr import PaddleOCR
# Initialize PaddleOCR reader once (use English model here)
ocr = PaddleOCR(use_angle_cls=True, lang='en', use_gpu=False)
def ocr_paddle_with_random_scores(img, correct_text):
if img is None:
return "No image uploaded", "", ""
# Convert PIL image to numpy array
img_array = np.array(img)
try:
# Perform OCR, get list of results
results = ocr.ocr(img_array, cls=True)
# Extract text lines from results
detected_text_lines = [line[1][0] for line in results]
detected_text = "\n".join(detected_text_lines)
# Generate random accuracy and pipeline scores between 80% and 85%
accuracy = random.uniform(0.80, 0.85)
pipeline_score = random.uniform(0.80, 0.85)
accuracy_str = f"{accuracy:.2%}"
pipeline_score_str = f"{pipeline_score:.2%}"
return detected_text, accuracy_str, pipeline_score_str
except Exception as e:
error_msg = f"PaddleOCR Error: {str(e)}"
return error_msg, "", ""
with gr.Blocks() as demo:
gr.Markdown("# PaddleOCR Demo with Lower Randomized Accuracy & Pipeline Scores")
with gr.Row():
img_input = gr.Image(type="pil", label="Upload Image")
correct_text_input = gr.Textbox(label="Enter Correct Text (for display only)", lines=4)
output_text = gr.Textbox(label="OCR Result", lines=10)
accuracy_output = gr.Textbox(label="Accuracy (Randomized)", interactive=False)
pipeline_output = gr.Textbox(label="Pipeline Integration Score (Randomized)", interactive=False)
run_button = gr.Button("Run OCR")
run_button.click(
ocr_paddle_with_random_scores,
inputs=[img_input, correct_text_input],
outputs=[output_text, accuracy_output, pipeline_output]
)
demo.launch()