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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import tensorflow as tf
|
| 4 |
+
import keras_ocr
|
| 5 |
+
import cv2
|
| 6 |
+
import os
|
| 7 |
+
import numpy as np
|
| 8 |
+
import pandas as pd
|
| 9 |
+
from datetime import datetime
|
| 10 |
+
import scipy.ndimage.interpolation as inter
|
| 11 |
+
import easyocr
|
| 12 |
+
from PIL import Image
|
| 13 |
+
from paddleocr import PaddleOCR
|
| 14 |
+
import socket
|
| 15 |
+
from send_email_user import send_user_email
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def get_device_ip_address():
|
| 19 |
+
|
| 20 |
+
if os.name == "nt":
|
| 21 |
+
result = "Running on Windows"
|
| 22 |
+
hostname = socket.gethostname()
|
| 23 |
+
result += "\nHostname: " + hostname
|
| 24 |
+
host = socket.gethostbyname(hostname)
|
| 25 |
+
result += "\nHost-IP-Address:" + host
|
| 26 |
+
return result
|
| 27 |
+
elif os.name == "posix":
|
| 28 |
+
gw = os.popen("ip -4 route show default").read().split()
|
| 29 |
+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
| 30 |
+
s.connect((gw[2], 0))
|
| 31 |
+
ipaddr = s.getsockname()[0]
|
| 32 |
+
gateway = gw[2]
|
| 33 |
+
host = socket.gethostname()
|
| 34 |
+
result = "\nIP address:\t\t" + ipaddr + "\r\nHost:\t\t" + host
|
| 35 |
+
return result
|
| 36 |
+
else:
|
| 37 |
+
result = os.name + " not supported yet."
|
| 38 |
+
return result
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
"""
|
| 42 |
+
Paddle OCR
|
| 43 |
+
"""
|
| 44 |
+
def ocr_with_paddle(img):
|
| 45 |
+
finaltext = ''
|
| 46 |
+
ocr = PaddleOCR(lang='en', use_angle_cls=True)
|
| 47 |
+
# img_path = 'exp.jpeg'
|
| 48 |
+
result = ocr.ocr(img)
|
| 49 |
+
|
| 50 |
+
for i in range(len(result[0])):
|
| 51 |
+
text = result[0][i][1][0]
|
| 52 |
+
finaltext += ' '+ text
|
| 53 |
+
return finaltext
|
| 54 |
+
|
| 55 |
+
"""
|
| 56 |
+
Keras OCR
|
| 57 |
+
"""
|
| 58 |
+
def ocr_with_keras(img):
|
| 59 |
+
output_text = ''
|
| 60 |
+
pipeline=keras_ocr.pipeline.Pipeline()
|
| 61 |
+
images=[keras_ocr.tools.read(img)]
|
| 62 |
+
predictions=pipeline.recognize(images)
|
| 63 |
+
first=predictions[0]
|
| 64 |
+
for text,box in first:
|
| 65 |
+
output_text += ' '+ text
|
| 66 |
+
return output_text
|
| 67 |
+
|
| 68 |
+
"""
|
| 69 |
+
easy OCR
|
| 70 |
+
"""
|
| 71 |
+
# gray scale image
|
| 72 |
+
def get_grayscale(image):
|
| 73 |
+
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 74 |
+
|
| 75 |
+
# Thresholding or Binarization
|
| 76 |
+
def thresholding(src):
|
| 77 |
+
return cv2.threshold(src,127,255, cv2.THRESH_TOZERO)[1]
|
| 78 |
+
|
| 79 |
+
def ocr_with_easy(img):
|
| 80 |
+
gray_scale_image=get_grayscale(img)
|
| 81 |
+
thresholding(gray_scale_image)
|
| 82 |
+
cv2.imwrite('image.png',gray_scale_image)
|
| 83 |
+
reader = easyocr.Reader(['th','en'])
|
| 84 |
+
bounds = reader.readtext('image.png',paragraph="False",detail = 0)
|
| 85 |
+
bounds = ''.join(bounds)
|
| 86 |
+
return bounds
|
| 87 |
+
"""
|
| 88 |
+
Generate OCR
|
| 89 |
+
"""
|
| 90 |
+
def generate_ocr(Method,img):
|
| 91 |
+
try:
|
| 92 |
+
text_output = ''
|
| 93 |
+
|
| 94 |
+
print("Method___________________",Method)
|
| 95 |
+
if Method == 'EasyOCR':
|
| 96 |
+
text_output = ocr_with_easy(img)
|
| 97 |
+
if Method == 'KerasOCR':
|
| 98 |
+
text_output = ocr_with_keras(img)
|
| 99 |
+
if Method == 'PaddleOCR':
|
| 100 |
+
text_output = ocr_with_paddle(img)
|
| 101 |
+
save_details(Method,text_output,img)
|
| 102 |
+
|
| 103 |
+
return text_output
|
| 104 |
+
# hostname = socket.gethostname()
|
| 105 |
+
# IPAddr = socket.gethostbyname(hostname)
|
| 106 |
+
# print(hostname)
|
| 107 |
+
# print("\nHost-IP-Address:" + IPAddr)
|
| 108 |
+
except Exception as e:
|
| 109 |
+
print("Error in ocr generation ==>",e)
|
| 110 |
+
text_output = "Something went wrong"
|
| 111 |
+
return text_output
|
| 112 |
+
"""
|
| 113 |
+
Save generated details
|
| 114 |
+
"""
|
| 115 |
+
def save_details(Method,text_output,img):
|
| 116 |
+
method = []
|
| 117 |
+
img_path = []
|
| 118 |
+
text = []
|
| 119 |
+
picture_path = "image.jpg"
|
| 120 |
+
|
| 121 |
+
curr_datetime = datetime.now().strftime('%Y-%m-%d %H-%M-%S')
|
| 122 |
+
if text_output:
|
| 123 |
+
splitted_path = os.path.splitext(picture_path)
|
| 124 |
+
modified_picture_path = splitted_path[0] + curr_datetime + splitted_path[1]
|
| 125 |
+
cv2.imwrite('images/'+ modified_picture_path, img)
|
| 126 |
+
input_img = 'images/'+ modified_picture_path
|
| 127 |
+
try:
|
| 128 |
+
df = pd.read_csv("AllDetails.csv")
|
| 129 |
+
df2 = {'method': Method, 'input_img': input_img, 'generated_text': text_output}
|
| 130 |
+
df = df.append(df2, ignore_index = True)
|
| 131 |
+
df.to_csv("AllDetails.csv", index=False)
|
| 132 |
+
except:
|
| 133 |
+
method.append(Method)
|
| 134 |
+
img_path.append(input_img)
|
| 135 |
+
text.append(text_output)
|
| 136 |
+
dict = {'method': method, 'input_img': img_path, 'generated_text': text}
|
| 137 |
+
df = pd.DataFrame(dict,index=None)
|
| 138 |
+
df.to_csv("AllDetails.csv")
|
| 139 |
+
|
| 140 |
+
hostname = get_device_ip_address()
|
| 141 |
+
return send_user_email(input_img,hostname,text_output,Method)
|
| 142 |
+
# return hostname
|
| 143 |
+
|
| 144 |
+
"""
|
| 145 |
+
Create user interface for OCR demo
|
| 146 |
+
"""
|
| 147 |
+
|
| 148 |
+
image = gr.Image(shape=(224, 224),elem_id="img_div")
|
| 149 |
+
method = gr.Radio(["EasyOCR", "KerasOCR", "PaddleOCR"],elem_id="radio_div")
|
| 150 |
+
output = gr.Textbox(label="Output")
|
| 151 |
+
|
| 152 |
+
demo = gr.Interface(
|
| 153 |
+
generate_ocr,
|
| 154 |
+
[method,image],
|
| 155 |
+
output,
|
| 156 |
+
title="Optical Character Recognition",
|
| 157 |
+
description="Try OCR with different methods",
|
| 158 |
+
theme="darkpeach",
|
| 159 |
+
css=".gradio-container {background-color: lightgray} #radio_div {background-color: #FFD8B4; font-size: 40px;}"
|
| 160 |
+
)
|
| 161 |
+
# .gradio-container.gap-2 {gap: 10rem;row-gap: 10rem;column-gap: 10rem;}
|
| 162 |
+
# .gradio-container {background-color: lightgray}
|
| 163 |
+
demo.launch()
|
| 164 |
+
|
| 165 |
+
# with gr.Blocks(css=".gradio-container {background-color: red}") as demo:
|
| 166 |
+
# input = [gr.Image(shape=(224, 224)), gr.Radio(["EasyOCR", "KerasOCR", "PaddleOCR"],text_color="blue")]
|
| 167 |
+
# sub_btn = gr.Button("Submit")
|
| 168 |
+
# output = gr.Textbox(label="Output")
|
| 169 |
+
# event = sub_btn.click(generate_ocr, inputs=input, outputs=output)
|
| 170 |
+
|
| 171 |
+
# demo.launch()
|