Spaces:
Runtime error
Runtime error
TomatoFT
commited on
Commit
•
c92b58a
1
Parent(s):
4a624ee
Initial upload for the app
Browse files- app.py +45 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import PIL
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image, ImageDraw, ImageFont
|
5 |
+
from translate import Translator
|
6 |
+
|
7 |
+
def translate_text(text, src_lang='en', dest_lang='vi'):
|
8 |
+
translator = Translator(to_lang=dest_lang, from_lang=src_lang)
|
9 |
+
translation = translator.translate(text)
|
10 |
+
return translation
|
11 |
+
|
12 |
+
def process_image(input_image):
|
13 |
+
# Convert the uploaded image data to a PIL Image
|
14 |
+
image = Image.open(input_image.name).convert('RGB')
|
15 |
+
|
16 |
+
# Sample code to perform OCR and draw translated text (replace with your logic)
|
17 |
+
# Simulated results for demonstration purposes
|
18 |
+
result = [
|
19 |
+
([(100, 100), (200, 100), (200, 200), (100, 200)], ("Hello, how are you?", 0.95))
|
20 |
+
]
|
21 |
+
|
22 |
+
draw = PIL.ImageDraw.Draw(image)
|
23 |
+
for i, box in enumerate(result):
|
24 |
+
text = translate_text(box[1][0])
|
25 |
+
box = np.array(box[0]).astype(np.int32)
|
26 |
+
xmin = min(box[:, 0])
|
27 |
+
ymin = min(box[:, 1])
|
28 |
+
xmax = max(box[:, 0])
|
29 |
+
ymax = max(box[:, 1])
|
30 |
+
draw.rectangle((xmin, ymin, xmax, ymax), outline="red", width=1)
|
31 |
+
draw.text((xmin + 40, ymin + 40), f"{text}", fill="black", font=ImageFont.load_default())
|
32 |
+
|
33 |
+
# Save the processed image
|
34 |
+
processed_image_path = 'processed_image.jpg'
|
35 |
+
image.save(processed_image_path)
|
36 |
+
return processed_image_path
|
37 |
+
|
38 |
+
iface = gr.Interface(
|
39 |
+
fn=process_image,
|
40 |
+
inputs=gr.inputs.Image(type="pil", label="Upload Image"),
|
41 |
+
outputs="image",
|
42 |
+
title="OCR Translation App",
|
43 |
+
description="Upload an image and see the processed image with translated text."
|
44 |
+
)
|
45 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
paddlepaddle
|
2 |
+
Pillow
|
3 |
+
translate
|
4 |
+
gradio
|
5 |
+
paddleocr>=2.0.1
|