tomofi commited on
Commit
18d9ff0
1 Parent(s): 1900688

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -0
app.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import gradio as gr
3
+ import pandas as pd
4
+ from PIL import Image, ImageDraw
5
+
6
+ def infer(im):
7
+ im.save('converted.png')
8
+ url = 'https://ajax.thehive.ai/api/demo/classify?endpoint=text_recognition'
9
+ files = {
10
+ 'image': ('converted.png', open('converted.png', 'rb'), 'image/png'),
11
+ 'model_type': (None, 'detection'),
12
+ 'media_type': (None, 'photo'),
13
+ }
14
+ res = requests.post(url, files=files).json()
15
+ img = im.convert('RGB')
16
+
17
+ words = []
18
+ draw = ImageDraw.Draw(img,'RGBA')
19
+ for output in res['response']['output']:
20
+ for poly in output['bounding_poly']:
21
+ words += [c['class'] for c in poly['classes']]
22
+ draw.rectangle((poly['dimensions']['left']-2,poly['dimensions']['top']-2,poly['dimensions']['right']+2,poly['dimensions']['bottom']+2), outline=(0,255,0,255), fill=(0,255,0,50),width=2)
23
+
24
+ img.save('result.png')
25
+ return 'result.png', '\n'.join([o['block_text'] for o in res['response']['output']]), pd.DataFrame(words)
26
+
27
+ iface = gr.Interface(
28
+ fn=infer,
29
+ title="Hive OCR",
30
+ description="Demo for Hive OCR.Transcribe and analyze media depicting typed, written, or graphic text",
31
+ inputs=[gr.inputs.Image(label='image', type='pil')],
32
+ outputs=['image', 'text', gr.outputs.Dataframe(headers=['word'])],
33
+ examples=[],
34
+ article="<a href=\"https://thehive.ai/hive-ocr-solutions\">Hive OCR</a>",
35
+ ).launch()