seaoctopusredchicken commited on
Commit
7141fe9
1 Parent(s): c0349f4

Cut some stuff

Browse files
Files changed (4) hide show
  1. README.md +2 -2
  2. app.py +36 -33
  3. receipt.webp +0 -0
  4. testocr.png +0 -0
README.md CHANGED
@@ -1,10 +1,10 @@
1
  ---
2
- title: Hive OCR
3
  emoji: 🦀
4
  colorFrom: pink
5
  colorTo: blue
6
  sdk: gradio
7
- sdk_version: 2.9.1
8
  app_file: app.py
9
  pinned: false
10
  license: mit
 
1
  ---
2
+ title: Hive OCR simple
3
  emoji: 🦀
4
  colorFrom: pink
5
  colorTo: blue
6
  sdk: gradio
7
+ sdk_version: 3.14.0
8
  app_file: app.py
9
  pinned: false
10
  license: mit
app.py CHANGED
@@ -1,40 +1,43 @@
 
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
- headers = {
15
- 'referer': 'https://thehive.ai/'
16
- }
17
- r = requests.post(url, headers=headers, files=files)
18
- res = r.json()
19
- print(r, res)
20
- img = im.convert('RGB')
21
-
22
- words = []
23
- draw = ImageDraw.Draw(img,'RGBA')
24
- for output in res['response']['output']:
25
- for poly in output['bounding_poly']:
26
- words += [c['class'] for c in poly['classes']]
27
- 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)
28
-
29
- img.save('result.png')
30
- return 'result.png', '\n'.join([o['block_text'] for o in res['response']['output']]), pd.DataFrame(words)
 
 
 
31
 
32
  iface = gr.Interface(
33
  fn=infer,
34
- title="Hive OCR",
35
- description="Demo for Hive OCR.Transcribe and analyze media depicting typed, written, or graphic text",
36
- inputs=[gr.inputs.Image(label='image', type='pil')],
37
- outputs=['image', 'text', gr.outputs.Dataframe(headers=['word'])],
38
- examples=['testocr.png', 'receipt.webp', '20131216170659.jpg'],
39
- article="<a href=\"https://thehive.ai/hive-ocr-solutions\">Hive OCR</a>",
40
- ).launch()
 
1
+ from codecs import encode, decode
2
  import requests
3
  import gradio as gr
4
+
 
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
+ headers = {"referer": "https://thehive.ai/"}
15
+
16
+ res = requests.post(url, headers=headers, files=files)
17
+
18
+ text = ""
19
+ blocks = []
20
+ for output in res.json()["response"]["output"]:
21
+ text += output["block_text"]
22
+ for poly in output["bounding_poly"]:
23
+ blocks.append(
24
+ {
25
+ "text": "".join([c["class"] for c in poly["classes"]]),
26
+ "rect": poly["dimensions"],
27
+ }
28
+ )
29
+
30
+ text = decode(encode(text, "latin-1", "backslashreplace"), "unicode-escape")
31
+
32
+ return text, blocks
33
+
34
 
35
  iface = gr.Interface(
36
  fn=infer,
37
+ title="Hive OCR simple",
38
+ description="Demo for Hive OCR. Transcribe and analyze media depicting typed, written, or graphic text",
39
+ inputs=[gr.Image(type="pil")],
40
+ outputs=["text", "json"],
41
+ examples=["20131216170659.jpg"],
42
+ article='<a href="https://thehive.ai/hive-ocr-solutions">Hive OCR</a>',
43
+ ).launch()
receipt.webp DELETED
Binary file (98.4 kB)
 
testocr.png DELETED
Binary file (23.4 kB)