Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
# app.py
|
2 |
|
3 |
import gradio as gr
|
4 |
import torch
|
@@ -30,32 +30,41 @@ import matplotlib.pyplot as plt
|
|
30 |
ethiopic_font = fm.FontProperties(fname=FONT_PATH, size=15)
|
31 |
pil_font = ImageFont.truetype(FONT_PATH, size=20)
|
32 |
|
33 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
"""
|
35 |
Function to recognize text and overlay it on the image.
|
36 |
-
Returns both the modified image and the recognized text.
|
37 |
"""
|
38 |
recognized_text = recognize_text(image)
|
39 |
|
40 |
-
# Create a copy of the image to draw on
|
41 |
-
image_with_text = image.copy()
|
42 |
-
|
43 |
# Overlay text on the image
|
44 |
-
draw = ImageDraw.Draw(
|
45 |
text_position = (10, 10) # Top-left corner
|
46 |
text_color = (255, 0, 0) # Red color
|
47 |
draw.text(text_position, f"Recognized: {recognized_text}", font=pil_font, fill=text_color)
|
48 |
|
49 |
-
return
|
50 |
|
51 |
# Define Gradio Interface
|
52 |
iface = gr.Interface(
|
53 |
fn=recognize_and_overlay,
|
54 |
inputs=gr.Image(type="pil", label="Upload Image"),
|
55 |
-
outputs=
|
56 |
-
gr.Image(type="pil", label="Image with Recognized Text"),
|
57 |
-
gr.Textbox(label="Recognized Text")
|
58 |
-
],
|
59 |
title="Amharic Text Recognition",
|
60 |
description="Upload an image containing Amharic text. The app will recognize and overlay the text on the image."
|
61 |
)
|
|
|
1 |
+
# app.py
|
2 |
|
3 |
import gradio as gr
|
4 |
import torch
|
|
|
30 |
ethiopic_font = fm.FontProperties(fname=FONT_PATH, size=15)
|
31 |
pil_font = ImageFont.truetype(FONT_PATH, size=20)
|
32 |
|
33 |
+
def recognize_text(image: Image.Image) -> str:
|
34 |
+
"""
|
35 |
+
Function to recognize text from an image.
|
36 |
+
"""
|
37 |
+
# Preprocess the image
|
38 |
+
input_tensor = preprocess_image(image).unsqueeze(0).to(device) # [1, 3, 224, 224]
|
39 |
+
|
40 |
+
# Perform inference
|
41 |
+
with torch.no_grad():
|
42 |
+
log_probs = model(input_tensor) # [H*W, 1, vocab_size]
|
43 |
+
|
44 |
+
# Decode predictions
|
45 |
+
recognized_texts = decode_predictions(log_probs)
|
46 |
+
|
47 |
+
return recognized_texts[0]
|
48 |
+
|
49 |
+
def recognize_and_overlay(image: Image.Image) -> Image.Image:
|
50 |
"""
|
51 |
Function to recognize text and overlay it on the image.
|
|
|
52 |
"""
|
53 |
recognized_text = recognize_text(image)
|
54 |
|
|
|
|
|
|
|
55 |
# Overlay text on the image
|
56 |
+
draw = ImageDraw.Draw(image)
|
57 |
text_position = (10, 10) # Top-left corner
|
58 |
text_color = (255, 0, 0) # Red color
|
59 |
draw.text(text_position, f"Recognized: {recognized_text}", font=pil_font, fill=text_color)
|
60 |
|
61 |
+
return image
|
62 |
|
63 |
# Define Gradio Interface
|
64 |
iface = gr.Interface(
|
65 |
fn=recognize_and_overlay,
|
66 |
inputs=gr.Image(type="pil", label="Upload Image"),
|
67 |
+
outputs=gr.Image(type="pil", label="Image with Recognized Text"),
|
|
|
|
|
|
|
68 |
title="Amharic Text Recognition",
|
69 |
description="Upload an image containing Amharic text. The app will recognize and overlay the text on the image."
|
70 |
)
|