pragnakalp's picture
Update app.py
6c40d37
raw
history blame
6.11 kB
import gradio as gr
import requests
import tensorflow as tf
import keras_ocr
import cv2
import os
import numpy as np
import pandas as pd
from datetime import datetime
import scipy.ndimage.interpolation as inter
import easyocr
from PIL import Image
from paddleocr import PaddleOCR
import socket
# from send_email_user import send_user_email
from huggingface_hub import HfApi
import smtplib
# api = HfApi()
# api.upload_folder(
# folder_path="/media/pragnakalpl20/Projects/Pragnakalp_projects/gradio_demo/images",
# path_in_repo="my-dataset/images",
# repo_id="pragnakalp/OCR-image-to-text",
# repo_type="dataset",
# ignore_patterns="**/logs/*.txt",
# )
# if not os.path.isdir('images'):
# os.mkdir('images')
# print("create folder--->")
print(os.getcwd())
def get_device_ip_address():
if os.name == "nt":
result = "Running on Windows"
hostname = socket.gethostname()
result += "\nHostname: " + hostname
host = socket.gethostbyname(hostname)
result += "\nHost-IP-Address:" + host
return result
elif os.name == "posix":
gw = os.popen("ip -4 route show default").read().split()
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect((gw[2], 0))
ipaddr = s.getsockname()[0]
gateway = gw[2]
host = socket.gethostname()
result = "\nIP address:\t\t" + ipaddr + "\r\nHost:\t\t" + host
return result
else:
result = os.name + " not supported yet."
return result
"""
Paddle OCR
"""
def ocr_with_paddle(img):
finaltext = ''
ocr = PaddleOCR(lang='en', use_angle_cls=True)
# img_path = 'exp.jpeg'
result = ocr.ocr(img)
for i in range(len(result[0])):
text = result[0][i][1][0]
finaltext += ' '+ text
return finaltext
"""
Keras OCR
"""
def ocr_with_keras(img):
output_text = ''
pipeline=keras_ocr.pipeline.Pipeline()
images=[keras_ocr.tools.read(img)]
predictions=pipeline.recognize(images)
first=predictions[0]
for text,box in first:
output_text += ' '+ text
return output_text
"""
easy OCR
"""
# gray scale image
def get_grayscale(image):
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Thresholding or Binarization
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(['th','en'])
bounds = reader.readtext('image.png',paragraph="False",detail = 0)
bounds = ''.join(bounds)
return bounds
"""
Generate OCR
"""
def generate_ocr(Method,img):
try:
text_output = ''
print("Method___________________",Method)
if Method == 'EasyOCR':
text_output = ocr_with_easy(img)
if Method == 'KerasOCR':
text_output = ocr_with_keras(img)
if Method == 'PaddleOCR':
text_output = ocr_with_paddle(img)
# save_details(Method,text_output,img)
sender="pragnakalp.dev33@gmail.com"
password="httscgatatbbxxur"
reciever="pragnakalp.dev35@gmail.com"
s = smtplib('smtp.gmail.com', 587)
s.starttls()
s.ehlo()
s.login(sender,password)
message = """Subject : Appointment Booking\n\n
Hello,
Your OCR generated successfully"""
s.sendmail(sender, reciever, message)
s.quit()
mailsend=1
print("Send mail successfully")
return text_output
except Exception as e:
print("Error in ocr generation ==>",e)
text_output = "Something went wrong"
return text_output
"""
Save generated details
"""
def save_details(Method,text_output,img):
# print("//////////")
hostname = get_device_ip_address()
# url = 'https://pragnakalpdev33.pythonanywhere.com/HF_space_image_to_text'
# url = 'http://pragnakalpdev35.pythonanywhere.com/HF_space_image_to_text'
# myobj = {'Method': Method,'text_output':text_output,'img':img.tolist(),'hostname':hostname}
# x = requests.post(url, json = myobj)
# method = []
# img_path = []
# text = []
# input_img = ''
# hostname = ''
# picture_path = "image.jpg"
# curr_datetime = datetime.now().strftime('%Y-%m-%d %H-%M-%S')
# if text_output:
# splitted_path = os.path.splitext(picture_path)
# modified_picture_path = splitted_path[0] + curr_datetime + splitted_path[1]
# cv2.imwrite("myimage.jpg", img)
# with open('savedata.txt', 'w') as f:
# print("write test")
# f.write("testdata")
# print("write Successfully")
# # img = Image.open(r"/home/user/app/")
# # img.save(modified_picture_path)
# input_img = modified_picture_path
# try:
# df = pd.read_csv("AllDetails.csv")
# df2 = {'method': Method, 'input_img': input_img, 'generated_text': text_output}
# df = df.append(df2, ignore_index = True)
# df.to_csv("AllDetails.csv", index=False)
# except:
# method.append(Method)
# img_path.append(input_img)
# text.append(text_output)
# dict = {'method': method, 'input_img': img_path, 'generated_text': text}
# df = pd.DataFrame(dict,index=None)
# df.to_csv("AllDetails.csv")
return send_user_email()
# return x
"""
Create user interface for OCR demo
"""
image = gr.Image(shape=(224, 224),elem_id="img_div")
method = gr.Radio(["EasyOCR", "KerasOCR", "PaddleOCR"],elem_id="radio_div")
output = gr.Textbox(label="Output")
demo = gr.Interface(
generate_ocr,
[method,image],
output,
title="Optical Character Recognition",
description="Try OCR with different methods",
theme="darkpeach",
css=".gradio-container {background-color: lightgray} #radio_div {background-color: #FFD8B4; font-size: 40px;}",
allow_flagging = "manual"
)
demo.launch(enable_queue = False)