Skip to content

Commit b6100cc

Browse files
committed
added gradio demo and instructions to run it in the readme
1 parent 3a68240 commit b6100cc

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,20 @@ model, processor = load_model(), load_processor()
8484
predictions = batch_inference([image], model, processor)
8585
```
8686

87+
### Gradio Demo
88+
89+
Install Gradio
90+
91+
```python
92+
pip install gradio
93+
```
94+
95+
Run the app gradio_demo.py
96+
97+
```python
98+
python gradio_demo.py
99+
```
100+
87101
## Text recognition
88102

89103
Coming soon.

gradio_demo.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import gradio as gr
2+
from PIL import Image, ImageDraw
3+
from surya.detection import batch_inference
4+
from surya.model.segformer import load_model, load_processor
5+
6+
model, processor = load_model(), load_processor()
7+
8+
def surya(img):
9+
10+
# surya predictions is a list of dicts for the given image
11+
predictions = batch_inference([img], model, processor)
12+
bboxes = predictions[0]['bboxes']
13+
vertical_lines = predictions[0]['vertical_lines']
14+
horizontal_lines = predictions[0]['horizontal_lines']
15+
16+
# Initialize the drawing context with the image as background
17+
draw = ImageDraw.Draw(img)
18+
19+
# OCR predictions (replace the sample data with your actual OCR output)
20+
predictions = {
21+
'bboxes': bboxes, # bounding boxes data here
22+
'vertical_lines': vertical_lines, # vertical lines data here
23+
'horizontal_lines': horizontal_lines # your horizontal lines data here
24+
}
25+
26+
# Draw bounding boxes
27+
for bbox in predictions['bboxes']:
28+
draw.rectangle(bbox, outline='red', width=2)
29+
30+
# Draw vertical lines
31+
for vline in predictions['vertical_lines']:
32+
x1, y1, x2, y2 = vline['bbox']
33+
draw.line((x1, y1, x2, y2), fill='blue', width=2)
34+
35+
# Draw horizontal lines
36+
for hline in predictions['horizontal_lines']:
37+
x1, y1, x2, y2 = hline['bbox']
38+
draw.line((x1, y1, x2, y2), fill='green', width=2)
39+
40+
# return the final image
41+
return img
42+
43+
# Blocks API
44+
with gr.Blocks() as demo:
45+
# title for the app
46+
gr.HTML("<h1><center> SURYA Demo </h1></center>")
47+
# input image component
48+
input_image = gr.Image(label="Input Image", type='pil')
49+
# run inference on the input image
50+
btn = gr.Button("Run Surya")
51+
# output image component
52+
output_image = gr.Image(label="Surya Output")
53+
btn.click(fn=surya, inputs=input_image, outputs=output_image, api_name="surya")
54+
55+
56+
if __name__ == "__main__":
57+
demo.launch()

0 commit comments

Comments
 (0)