Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from surya.detection import batch_inference
|
| 3 |
+
from surya.model.segformer import load_model, load_processor
|
| 4 |
+
from surya.postprocessing.heatmap import draw_polys_on_image
|
| 5 |
+
|
| 6 |
+
model, processor = load_model(), load_processor()
|
| 7 |
+
|
| 8 |
+
HEADER = """
|
| 9 |
+
# Surya OCR Demo
|
| 10 |
+
This demo will let you try surya, a multilingual OCR model. It supports text detection now, but will support text recognition in the future.
|
| 11 |
+
Notes:
|
| 12 |
+
- This works best on documents with printed text.
|
| 13 |
+
- Set DETECTOR_MODEL_CHECKPOINT=vikp/line_detector_math before running this app if you want better math detection.
|
| 14 |
+
Learn more [here](https://github.com/VikParuchuri/surya).
|
| 15 |
+
""".strip()
|
| 16 |
+
|
| 17 |
+
def text_detection(img):
|
| 18 |
+
preds = batch_inference([img], model, processor)[0]
|
| 19 |
+
img = draw_polys_on_image(preds["polygons"], img)
|
| 20 |
+
return img, preds
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
with gr.Blocks() as app:
|
| 24 |
+
gr.Markdown(HEADER)
|
| 25 |
+
with gr.Row():
|
| 26 |
+
input_image = gr.Image(label="Input Image", type="pil")
|
| 27 |
+
output_image = gr.Image(label="Output Image", type="pil", interactive=False)
|
| 28 |
+
text_detection_btn = gr.Button("Run Text Detection")
|
| 29 |
+
|
| 30 |
+
json_output = gr.JSON(label="JSON Output")
|
| 31 |
+
text_detection_btn.click(fn=text_detection, inputs=input_image, outputs=[output_image, json_output], api_name="text_detection")
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
if __name__ == "__main__":
|
| 35 |
+
app.launch()
|