Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,47 +1,55 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
|
|
|
|
4 |
import easyocr
|
5 |
-
import
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
#
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
36 |
|
37 |
# Define the Gradio interface
|
38 |
interface = gr.Interface(
|
39 |
-
fn=
|
40 |
-
inputs=gr.Image(type="
|
41 |
-
outputs=
|
42 |
-
|
|
|
|
|
|
|
|
|
43 |
)
|
44 |
|
45 |
# Launch the Gradio app
|
46 |
-
print("Launching the Gradio interface...")
|
47 |
interface.launch()
|
|
|
1 |
+
# Install necessary libraries
|
2 |
+
!pip install easyocr opencv-python gradio
|
3 |
+
|
4 |
+
# Import required libraries
|
5 |
+
import cv2
|
6 |
import easyocr
|
7 |
+
import numpy as np
|
8 |
+
import gradio as gr
|
9 |
+
|
10 |
+
# Function to process the uploaded image and extract text
|
11 |
+
def extract_text_from_image(image):
|
12 |
+
# Save the uploaded image to disk
|
13 |
+
image_path = "uploaded_image.jpg"
|
14 |
+
cv2.imwrite(image_path, image)
|
15 |
+
|
16 |
+
# Read the image with OpenCV
|
17 |
+
img = cv2.imread(image_path)
|
18 |
+
|
19 |
+
# Initialize the EasyOCR reader
|
20 |
+
reader = easyocr.Reader(['en', 'ar'], gpu=False)
|
21 |
+
|
22 |
+
# Perform text detection
|
23 |
+
results = reader.readtext(image_path)
|
24 |
+
|
25 |
+
# Draw bounding boxes and overlay text on the image
|
26 |
+
conf_threshold = 0.2
|
27 |
+
for (bbox, text, conf) in results:
|
28 |
+
if conf > conf_threshold:
|
29 |
+
# Get coordinates
|
30 |
+
top_left = tuple(map(int, bbox[0]))
|
31 |
+
bottom_right = tuple(map(int, bbox[2]))
|
32 |
+
|
33 |
+
# Draw rectangle and text
|
34 |
+
img = cv2.rectangle(img, top_left, bottom_right, (0, 0, 255), 2)
|
35 |
+
img = cv2.putText(img, text, top_left, cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
|
36 |
+
|
37 |
+
# Convert the image to RGB (Gradio requires RGB format)
|
38 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
39 |
+
|
40 |
+
return img, results
|
41 |
|
42 |
# Define the Gradio interface
|
43 |
interface = gr.Interface(
|
44 |
+
fn=extract_text_from_image,
|
45 |
+
inputs=gr.Image(type="numpy", label="Upload Image"),
|
46 |
+
outputs=[
|
47 |
+
gr.Image(type="numpy", label="Processed Image"),
|
48 |
+
gr.Text(label="Extracted Text")
|
49 |
+
],
|
50 |
+
title="Image Text Extractor",
|
51 |
+
description="Upload an image to extract text using EasyOCR.",
|
52 |
)
|
53 |
|
54 |
# Launch the Gradio app
|
|
|
55 |
interface.launch()
|