Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +65 -0
- report.jpeg +0 -0
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
+
import easyocr
|
| 5 |
+
from docx import Document
|
| 6 |
+
|
| 7 |
+
torch.hub.download_url_to_file('https://github.com/JaidedAI/EasyOCR/raw/master/examples/english.png', 'english.png')
|
| 8 |
+
|
| 9 |
+
def inference(img):
|
| 10 |
+
# Here, you can add your image processing logic using EasyOCR
|
| 11 |
+
# For demonstration purposes, let's extract text from the input image and save it in a Word document
|
| 12 |
+
# Perform OCR on the input image
|
| 13 |
+
reader = easyocr.Reader(["en"])
|
| 14 |
+
bounds = reader.readtext(img)
|
| 15 |
+
# im = PIL.Image.open(img.name)
|
| 16 |
+
# draw_boxes(im, bounds)
|
| 17 |
+
# im.save('result.jpg')
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
# Download the input image
|
| 21 |
+
# response = requests.get(img, stream=True)
|
| 22 |
+
# response.raise_for_status()
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
# reader = easyocr.Reader(['en'])
|
| 26 |
+
result = pd.DataFrame(bounds).iloc[: , 1:]
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
# Save the extracted text to a Word document
|
| 30 |
+
output_file_path = "output_document.docx"
|
| 31 |
+
document = Document()
|
| 32 |
+
for text in result[1]:
|
| 33 |
+
document.add_paragraph(text)
|
| 34 |
+
# print(text)
|
| 35 |
+
document.save(output_file_path)
|
| 36 |
+
df = result.rename(index = {"1": "Text","2":"Confidence"},inplace = True)
|
| 37 |
+
# print(df.columns.values[0])
|
| 38 |
+
#pd.DataFrame(result , columns=["Text", "Confidence"])
|
| 39 |
+
return output_file_path , pd.DataFrame(bounds).iloc[: , 1:]#.rename(index = {1: "Text",2:"Confidence"},inplace = True)
|
| 40 |
+
# return pd.DataFrame(bounds).iloc[: , 1:]
|
| 41 |
+
|
| 42 |
+
title = 'EasyOCR'
|
| 43 |
+
description = 'Gradio demo for EasyOCR. EasyOCR demo supports 80+ languages.To use it, simply upload your image and choose a language from the dropdown menu, or click one of the examples to load them. Read more at the links below.'
|
| 44 |
+
article = "<p style='text-align: center'><a href='https://www.jaided.ai/easyocr/'>Ready-to-use OCR with 80+ supported languages and all popular writing scripts including Latin, Chinese, Arabic, Devanagari, Cyrillic and etc.</a> | <a href='https://github.com/JaidedAI/EasyOCR'>Github Repo</a></p>"
|
| 45 |
+
examples = [['english.png',['en']],['report.jpeg',['en']]]
|
| 46 |
+
css = ".output_image, .input_image {height: 40rem !important; width: 100% !important;}"
|
| 47 |
+
choices = [
|
| 48 |
+
|
| 49 |
+
"en",
|
| 50 |
+
"ms",
|
| 51 |
+
|
| 52 |
+
]
|
| 53 |
+
gr.Interface(
|
| 54 |
+
fn=inference,
|
| 55 |
+
# inputs="image", outputs="text",
|
| 56 |
+
inputs=[gr.Image( label='Input')],
|
| 57 |
+
outputs=[ gr.File(label="Output Word Document"),gr.Dataframe(label='Result', type="pandas",headers=['Text', 'Confidence'], wrap=True)],
|
| 58 |
+
title=title,
|
| 59 |
+
description=description,
|
| 60 |
+
article=article,
|
| 61 |
+
examples=examples,
|
| 62 |
+
css=css
|
| 63 |
+
).launch(debug=True)
|
| 64 |
+
|
| 65 |
+
# gr.Interface(fn=predict, inputs="image", outputs="text")
|
report.jpeg
ADDED
|