File size: 1,178 Bytes
a55334d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import gradio as gr

import cv2
import pytesseract
import numpy as np
from PIL import Image
import urllib

def process_image(image):
  image = np.array(image)
  gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
  gray, img_bin = cv2.threshold(gray,128,255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)
  gray = cv2.bitwise_not(img_bin)

  kernel = np.ones((2, 1), np.uint8)
  img = cv2.erode(gray, kernel, iterations=1)
  img = cv2.dilate(img, kernel, iterations=1)
  generated_text = pytesseract.image_to_string(img)

  return generated_text

title = "Interactive demo: TrOCR"
description = "Demo for tesseract ocr"
article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2109.10282'>TrOCR: Transformer-based Optical Character Recognition with Pre-trained Models</a> | <a href='https://github.com/microsoft/unilm/tree/master/trocr'>Github Repo</a></p>"
examples =[["image_0.png"]]

iface = gr.Interface(fn=process_image, 
                     inputs=gr.inputs.Image(type="pil"), 
                     outputs=gr.outputs.Textbox(),
                     title=title,
                     description=description,
                     article=article)
iface.launch(debug=True)