Spaces:
Running
Running
File size: 8,586 Bytes
1569310 3878fb6 1569310 2d7b88a c82795d 1569310 0cc7d4a 1569310 3c29c5e 32b4d4b 3c29c5e 03ceac8 a1c4f2e 51e91cb 8308970 51e91cb 98c7b0e 51e91cb cb88ed2 c506677 cb88ed2 51e91cb eec34b9 51e91cb 1569310 1650e5f 1569310 98c7b0e 1569310 4f50644 98c7b0e 1569310 618bbc5 1569310 03ceac8 17da5da 9127753 e0a4914 cb88ed2 17c3a92 cb88ed2 5a6f415 98c7b0e 0be025c 0cc7d4a 0be025c 7cd3a92 51b3915 7cd3a92 f6127e6 51e91cb f6127e6 51e91cb f6127e6 f10f992 6c40d37 1569310 3c29c5e 1569310 e9dff8c 28f43ce ba52c7e 4d21d95 ecdecda b9ebda9 1569310 ba52c7e 933fcc0 ecdecda 1569310 19c148f 1569310 03ceac8 8439022 51e91cb 1569310 55b140e |
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
import gradio as gr
import requests
import tensorflow as tf
import keras_ocr
import cv2
import os
import csv
import numpy as np
import pandas as pd
import huggingface_hub
from huggingface_hub import Repository
from datetime import datetime
import scipy.ndimage.interpolation as inter
import easyocr
from datasets import load_dataset, Image, Features, Array3D
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
HF_TOKEN = os.environ.get("HF_TOKEN")
# mydataset_name = "pragnakalp/OCR-img-to-text"
# print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$",type(mydataset_name))
# hf_writer = gr.HuggingFaceDatasetSaver(HF_TOKEN,mydataset_name)
DATASET_REPO_URL = "https://huggingface.co/datasets/pragnakalp/OCR-img-to-text"
DATA_FILENAME = "ocr_data.csv"
DATA_FILE = os.path.join("ocr_data", DATA_FILENAME)
DATA_FILENAME2 = "ocr_image.csv"
DATA_FILE2 = os.path.join("ocr_image", DATA_FILENAME2)
HF_TOKEN = os.environ.get("HF_TOKEN")
DATASET_REPO_ID = "pragnakalp/OCR-img-to-text"
print("is none?", HF_TOKEN is None)
try:
hf_hub_download(
repo_id=DATASET_REPO_ID,
filename=DATA_FILENAME,
cache_dir=DATA_DIRNAME,
force_filename=DATA_FILENAME
)
except:
print("file not found")
repo = Repository(
local_dir="ocr_data", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
)
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
# def store_single_disk(image, image_id, label):
# """ Stores a single image as a .png file on disk.
# Parameters:
# ---------------
# image image array, (32, 32, 3) to be stored
# image_id integer unique ID for image
# label image label
# """
# Image.fromarray(image).save(disk_dir / f"{image_id}.png")
# with open(disk_dir / f"{image_id}.csv", "wt") as csvfile:
# writer = csv.writer(
# csvfile, delimiter=" ", quotechar="|", quoting=csv.QUOTE_MINIMAL
# )
# writer.writerow([label])
"""
Generate OCR
"""
def generate_ocr(Method,img):
try:
text_output = ''
add_csv = []
image_id = 1
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)
new_data=img.reshape(img.shape)
imge = Image.fromarray(new_data.astype(np.uint8),'RGB')
add_csv = [Method,imge,text_output]
with open(DATA_FILE, "a") as f:
writer = csv.writer(f)
# write the data
writer.writerow(add_csv)
commit_url = repo.push_to_hub()
print(commit_url)
Image.fromarray(image).save(DATA_FILE2 / f"{image_id}.png")
with open(DATA_FILE2, "wt") as csvfile:
writer = csv.writer(
csvfile, delimiter=" ", quotechar="|", quoting=csv.QUOTE_MINIMAL
)
writer.writerow([0])
# try:
# dataset = load_dataset("pragnakalp/OCR-img-to-text", streaming=True)
# print(dataset.features)
# except Exception as e:
# print("error in loading data",e)
# with open(DATA_FILE, "a") as csvfile:
# writer = csv.Writer(csvfile)
# writer.writerow(add_csv)
# commit_url = repo.push_to_hub()
# print(commit_url)
# save_details(Method,text_output,img)
# sender="[email protected]"
# password="httscgatatbbxxur"
# reciever="[email protected]"
# s = smtplib.SMTP('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"],value="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",
css=".gradio-container {background-color: lightgray} #radio_div {background-color: #FFD8B4; font-size: 40px;}",
allow_flagging = "manual"
# flagging_dir = "flagged",
# flagging_callback=hf_writer
)
demo.launch(enable_queue = False) |