|
import gradio as gr
|
|
import easyocr
|
|
import cv2
|
|
import numpy as np
|
|
from PIL import Image
|
|
|
|
|
|
reader = easyocr.Reader(['en'])
|
|
|
|
def process_image(image):
|
|
|
|
image_np = np.array(image)
|
|
|
|
|
|
image_rgb = cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB)
|
|
|
|
|
|
result = reader.readtext(image_rgb)
|
|
|
|
|
|
for (bbox, text, prob) in result:
|
|
(top_left, top_right, bottom_right, bottom_left) = bbox
|
|
top_left = tuple(map(int, top_left))
|
|
bottom_right = tuple(map(int, bottom_right))
|
|
cv2.rectangle(image_np, top_left, bottom_right, (0, 255, 0), 2)
|
|
|
|
|
|
result_image = Image.fromarray(cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB))
|
|
|
|
|
|
detected_text = "\n".join([f"Detected text: {text}, Confidence: {prob:.2f}" for (_, text, prob) in result])
|
|
|
|
return result_image, detected_text
|
|
|
|
|
|
interface = gr.Interface(
|
|
fn=process_image,
|
|
inputs="image",
|
|
outputs=["image", "text"],
|
|
title="OCR with EasyOCR",
|
|
description="Upload an image, and the system wi ll detect text using EasyOCR and display it."
|
|
)
|
|
|
|
|
|
interface.launch()
|
|
|