OCR_Tutorial / app.py
llmat's picture
Update app.py
f5a94e3 verified
raw
history blame
1.44 kB
import gradio as gr
import cv2
import easyocr
from PIL import Image
def get_grayscale(image):
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
def thresholding(src):
return cv2.threshold(src,127,255, cv2.THRESH_TOZERO)[1]
def ocr_with_easy(img):
gray_scale_image = get_grayscale(img)
thresholding(gray_scale_image)
cv2.imwrite('image.png', gray_scale_image)
reader = easyocr.Reader(['en'])
bounds = reader.readtext('image.png', paragraph="False", detail=0)
bounds = ''.join(bounds)
return bounds
def generate_ocr(img):
text_output = ''
if (img).any():
if img is not None:
text_output = ocr_with_easy(img)
else:
raise gr.Error("Please upload an image!!!!")
return text_output
image = gr.Image()
output = gr.Textbox(label="Output")
demo = gr.Interface(
generate_ocr,
image,
output,
title="Optical Character Recognition",
css=".gradio-container {background-color: lightgray} #radio_div {background-color: #FFD8B4; font-size: 40px;}",
article = """<p style='text-align: center;'>Feel free to give us your thoughts on this demo and please contact us at
<a href="mailto:[email protected]" target="_blank">[email protected]</a>
<p style='text-align: center;'>Developed by: <a href="https://www.pragnakalp.com" target="_blank">Pragnakalp Techlabs</a></p>"""
)
demo.launch()