Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,23 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from PIL import Image
|
| 3 |
-
from
|
| 4 |
-
import
|
| 5 |
|
| 6 |
-
# Load
|
| 7 |
-
|
| 8 |
-
processor = AutoProcessor.from_pretrained(model_name)
|
| 9 |
-
model = VisionEncoderDecoderModel.from_pretrained(model_name)
|
| 10 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 11 |
-
model.to(device)
|
| 12 |
|
| 13 |
def recognize_handwriting(image):
|
| 14 |
if image is None:
|
| 15 |
return "Please upload an image."
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
return decoded.strip()
|
| 23 |
|
| 24 |
with gr.Blocks() as demo:
|
| 25 |
gr.Markdown("")
|
| 26 |
-
gr.Markdown("
|
| 27 |
|
| 28 |
with gr.Row():
|
| 29 |
image_input = gr.Image(type="pil", label="Upload Handwritten Image")
|
|
@@ -32,5 +26,4 @@ with gr.Blocks() as demo:
|
|
| 32 |
extract_btn = gr.Button("Extract Text")
|
| 33 |
extract_btn.click(fn=recognize_handwriting, inputs=image_input, outputs=text_output)
|
| 34 |
|
| 35 |
-
|
| 36 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from PIL import Image
|
| 3 |
+
from doctr.models import ocr_predictor
|
| 4 |
+
from doctr.io import DocumentFile
|
| 5 |
|
| 6 |
+
# Load the OCR model
|
| 7 |
+
model = ocr_predictor(det_arch='db_resnet50', reco_arch='parseq', pretrained=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def recognize_handwriting(image):
|
| 10 |
if image is None:
|
| 11 |
return "Please upload an image."
|
| 12 |
|
| 13 |
+
doc = DocumentFile.from_images(image)
|
| 14 |
+
result = model(doc)
|
| 15 |
+
text = "\n".join([block.value for page in result.pages for block in page.blocks])
|
| 16 |
+
return text.strip()
|
|
|
|
|
|
|
| 17 |
|
| 18 |
with gr.Blocks() as demo:
|
| 19 |
gr.Markdown("")
|
| 20 |
+
gr.Markdown("# 📝 Handwriting Recognition with Doctr")
|
| 21 |
|
| 22 |
with gr.Row():
|
| 23 |
image_input = gr.Image(type="pil", label="Upload Handwritten Image")
|
|
|
|
| 26 |
extract_btn = gr.Button("Extract Text")
|
| 27 |
extract_btn.click(fn=recognize_handwriting, inputs=image_input, outputs=text_output)
|
| 28 |
|
| 29 |
+
demo.launch()
|
|
|