Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,45 @@
|
|
| 1 |
-
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
def process(
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
|
|
|
| 14 |
langs = pytesseract.get_languages()
|
| 15 |
|
|
|
|
| 16 |
interface = gr.Interface(
|
| 17 |
-
process,
|
| 18 |
-
[
|
|
|
|
|
|
|
|
|
|
| 19 |
outputs="text",
|
| 20 |
css="footer {visibility: hidden}",
|
| 21 |
-
title="Optical Character Recognition | Image To Text",
|
| 22 |
-
article
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
)
|
| 24 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pytesseract
|
| 3 |
+
import cv2
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
+
def process(images, lang: str = 'eng') -> str:
|
| 7 |
+
results = []
|
| 8 |
+
for image in images:
|
| 9 |
+
try:
|
| 10 |
+
img = cv2.imread(image)
|
| 11 |
+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 12 |
+
_, threshold_img = cv2.threshold(gray, 127, 255, cv2.THRESH_TOZERO)
|
| 13 |
+
result = pytesseract.image_to_string(threshold_img, lang=lang)
|
| 14 |
+
results.append(result)
|
| 15 |
+
except Exception as e:
|
| 16 |
+
results.append(f"Error processing {image}: {str(e)}")
|
| 17 |
+
finally:
|
| 18 |
+
os.remove(image)
|
| 19 |
+
return "\n\n".join(results)
|
| 20 |
|
| 21 |
+
# Get the available languages for pytesseract
|
| 22 |
langs = pytesseract.get_languages()
|
| 23 |
|
| 24 |
+
# Define the Gradio interface
|
| 25 |
interface = gr.Interface(
|
| 26 |
+
fn=process,
|
| 27 |
+
inputs=[
|
| 28 |
+
gr.Image(type="filepath", multiple=True),
|
| 29 |
+
gr.Dropdown(label="Select Language", choices=langs, type="value")
|
| 30 |
+
],
|
| 31 |
outputs="text",
|
| 32 |
css="footer {visibility: hidden}",
|
| 33 |
+
title="Optical Character Recognition | Batch Image To Text",
|
| 34 |
+
article="""
|
| 35 |
+
<p style='text-align: center;'>
|
| 36 |
+
Hello, thanks for coming! Visit AI tools:
|
| 37 |
+
<a href="https://www.genelify.com" target="_blank">Genelify</a>,
|
| 38 |
+
and check out Social Media tools:
|
| 39 |
+
<a href="https://www.tubtic.com" target="_blank">Tubtic</a>
|
| 40 |
+
</p>
|
| 41 |
+
"""
|
| 42 |
)
|
| 43 |
+
|
| 44 |
+
# Launch the interface
|
| 45 |
+
interface.launch(show_api=False)
|