# 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 extracted_text = [] # To store extracted text for (bbox, text, conf) in results: if conf > conf_threshold: # Append text to the list extracted_text.append(text) # 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) # Join the extracted text into a single string with line breaks extracted_text_str = "\n".join(extracted_text) return img, extracted_text_str # 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 (Line by Line)") ], title="Image Text Extractor", description="Upload an image to extract text using EasyOCR. The extracted text will be displayed line by line.", ) # Launch the Gradio app interface.launch()