# Install necessary libraries !pip install easyocr opencv-python gradio # Import required libraries import cv2 import easyocr import numpy as np import gradio as gr # Function to process the uploaded image and extract text def extract_text_from_image(image): # Save the uploaded image to disk image_path = "uploaded_image.jpg" cv2.imwrite(image_path, image) # Read the image with OpenCV img = cv2.imread(image_path) # Initialize the EasyOCR reader reader = easyocr.Reader(['en', 'ar'], gpu=False) # Perform text detection results = reader.readtext(image_path) # Draw bounding boxes and overlay text on the image conf_threshold = 0.2 for (bbox, text, conf) in results: if conf > conf_threshold: # Get coordinates top_left = tuple(map(int, bbox[0])) bottom_right = tuple(map(int, bbox[2])) # Draw rectangle and text img = cv2.rectangle(img, top_left, bottom_right, (0, 0, 255), 2) img = cv2.putText(img, text, top_left, cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2) # Convert the image to RGB (Gradio requires RGB format) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) return img, results # Define the Gradio interface interface = gr.Interface( fn=extract_text_from_image, inputs=gr.Image(type="numpy", label="Upload Image"), outputs=[ gr.Image(type="numpy", label="Processed Image"), gr.Text(label="Extracted Text") ], title="Image Text Extractor", description="Upload an image to extract text using EasyOCR.", ) # Launch the Gradio app interface.launch()