Skip to content

Commit

Permalink
added gradio demo and instructions to run it in the readme
Browse files Browse the repository at this point in the history
  • Loading branch information
yvrjsharma committed Jan 16, 2024
1 parent 3a68240 commit b6100cc
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,20 @@ model, processor = load_model(), load_processor()
predictions = batch_inference([image], model, processor)
```

### Gradio Demo

Install Gradio

```python
pip install gradio
```

Run the app gradio_demo.py

```python
python gradio_demo.py
```

## Text recognition

Coming soon.
Expand Down
57 changes: 57 additions & 0 deletions gradio_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import gradio as gr
from PIL import Image, ImageDraw
from surya.detection import batch_inference
from surya.model.segformer import load_model, load_processor

model, processor = load_model(), load_processor()

def surya(img):

# surya predictions is a list of dicts for the given image
predictions = batch_inference([img], model, processor)
bboxes = predictions[0]['bboxes']
vertical_lines = predictions[0]['vertical_lines']
horizontal_lines = predictions[0]['horizontal_lines']

# Initialize the drawing context with the image as background
draw = ImageDraw.Draw(img)

# OCR predictions (replace the sample data with your actual OCR output)
predictions = {
'bboxes': bboxes, # bounding boxes data here
'vertical_lines': vertical_lines, # vertical lines data here
'horizontal_lines': horizontal_lines # your horizontal lines data here
}

# Draw bounding boxes
for bbox in predictions['bboxes']:
draw.rectangle(bbox, outline='red', width=2)

# Draw vertical lines
for vline in predictions['vertical_lines']:
x1, y1, x2, y2 = vline['bbox']
draw.line((x1, y1, x2, y2), fill='blue', width=2)

# Draw horizontal lines
for hline in predictions['horizontal_lines']:
x1, y1, x2, y2 = hline['bbox']
draw.line((x1, y1, x2, y2), fill='green', width=2)

# return the final image
return img

# Blocks API
with gr.Blocks() as demo:
# title for the app
gr.HTML("<h1><center> SURYA Demo </h1></center>")
# input image component
input_image = gr.Image(label="Input Image", type='pil')
# run inference on the input image
btn = gr.Button("Run Surya")
# output image component
output_image = gr.Image(label="Surya Output")
btn.click(fn=surya, inputs=input_image, outputs=output_image, api_name="surya")


if __name__ == "__main__":
demo.launch()

2 comments on commit b6100cc

@yvrjsharma
Copy link
Contributor Author

@yvrjsharma yvrjsharma commented on b6100cc Feb 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @VikParuchuri , when are you planning to merge these changes from the dev to the main branch? Are you considering any changes to the demo UI/structure? Let me know if I can assist with anything.

@VikParuchuri
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yvrjsharma I couldn't get everything to work in gradio, so I ended up using streamlit. I'm happy to switch back to gradio if you feel like you could implement all the functionality in ocr_app.py. Thanks for the PR and following up.

Please sign in to comment.