Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import random
|
| 4 |
+
from paddleocr import PaddleOCR
|
| 5 |
+
|
| 6 |
+
# Initialize PaddleOCR reader once (use English model here)
|
| 7 |
+
ocr = PaddleOCR(use_angle_cls=True, lang='en', use_gpu=False)
|
| 8 |
+
|
| 9 |
+
def ocr_paddle_with_random_scores(img, correct_text):
|
| 10 |
+
if img is None:
|
| 11 |
+
return "No image uploaded", "", ""
|
| 12 |
+
|
| 13 |
+
# Convert PIL image to numpy array
|
| 14 |
+
img_array = np.array(img)
|
| 15 |
+
|
| 16 |
+
try:
|
| 17 |
+
# Perform OCR, get list of results
|
| 18 |
+
results = ocr.ocr(img_array, cls=True)
|
| 19 |
+
|
| 20 |
+
# Extract text lines from results
|
| 21 |
+
detected_text_lines = [line[1][0] for line in results]
|
| 22 |
+
detected_text = "\n".join(detected_text_lines)
|
| 23 |
+
|
| 24 |
+
# Generate random accuracy and pipeline scores between 80% and 85%
|
| 25 |
+
accuracy = random.uniform(0.80, 0.85)
|
| 26 |
+
pipeline_score = random.uniform(0.80, 0.85)
|
| 27 |
+
|
| 28 |
+
accuracy_str = f"{accuracy:.2%}"
|
| 29 |
+
pipeline_score_str = f"{pipeline_score:.2%}"
|
| 30 |
+
|
| 31 |
+
return detected_text, accuracy_str, pipeline_score_str
|
| 32 |
+
|
| 33 |
+
except Exception as e:
|
| 34 |
+
error_msg = f"PaddleOCR Error: {str(e)}"
|
| 35 |
+
return error_msg, "", ""
|
| 36 |
+
|
| 37 |
+
with gr.Blocks() as demo:
|
| 38 |
+
gr.Markdown("# PaddleOCR Demo with Lower Randomized Accuracy & Pipeline Scores")
|
| 39 |
+
|
| 40 |
+
with gr.Row():
|
| 41 |
+
img_input = gr.Image(type="pil", label="Upload Image")
|
| 42 |
+
correct_text_input = gr.Textbox(label="Enter Correct Text (for display only)", lines=4)
|
| 43 |
+
|
| 44 |
+
output_text = gr.Textbox(label="OCR Result", lines=10)
|
| 45 |
+
accuracy_output = gr.Textbox(label="Accuracy (Randomized)", interactive=False)
|
| 46 |
+
pipeline_output = gr.Textbox(label="Pipeline Integration Score (Randomized)", interactive=False)
|
| 47 |
+
|
| 48 |
+
run_button = gr.Button("Run OCR")
|
| 49 |
+
run_button.click(
|
| 50 |
+
ocr_paddle_with_random_scores,
|
| 51 |
+
inputs=[img_input, correct_text_input],
|
| 52 |
+
outputs=[output_text, accuracy_output, pipeline_output]
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
demo.launch()
|