Skip to content

Commit

Permalink
Reformatter the whole code (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
devkapilbansal authored Aug 25, 2020
1 parent 4b3030c commit a427890
Show file tree
Hide file tree
Showing 10 changed files with 297 additions and 238 deletions.
43 changes: 31 additions & 12 deletions freshlybuiltimagebol/OCR_Printed_Text.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
from cv2 import (
fastNlMeansDenoisingColored, cvtColor, bitwise_not, threshold,
getRotationMatrix2D, warpAffine,filter2D, imread, imshow,
THRESH_BINARY, COLOR_BGR2GRAY, THRESH_OTSU,
INTER_CUBIC, BORDER_REPLICATE, minAreaRect,
BORDER_REPLICATE,
COLOR_BGR2GRAY,
INTER_CUBIC,
THRESH_BINARY,
THRESH_OTSU,
bitwise_not,
cvtColor,
fastNlMeansDenoisingColored,
filter2D,
getRotationMatrix2D,
imread,
imshow,
minAreaRect,
threshold,
warpAffine,
)
from numpy import column_stack, array, where
from pytesseract import image_to_string,pytesseract
from numpy import array, column_stack, where
from PIL import Image
from pytesseract import image_to_string, pytesseract


class ImageProcess:
'''this function is removing noise from the image'''

def remove_noise(image):
image = fastNlMeansDenoisingColored(image,None,20,10,7,21)
image = fastNlMeansDenoisingColored(image, None, 20, 10, 7, 21)
return image

'''this function is removing skewness.
first, it calculate the angle and accordingly rotate image'''

def remove_skew(image):
in_gray = cvtColor(image, COLOR_BGR2GRAY)
in_gray = bitwise_not(in_gray)
thresh_pic = threshold(in_gray, 0, 255,THRESH_BINARY | THRESH_OTSU)[1]
thresh_pic = threshold(in_gray, 0, 255, THRESH_BINARY | THRESH_OTSU)[1]
coords_x_y = column_stack(where(thresh_pic > 0))
angle = minAreaRect(coords_x_y)[-1]
if angle < -45:
Expand All @@ -29,21 +43,26 @@ def remove_skew(image):
(h, w) = image.shape[:2]
center_of_pic = (w // 2, h // 2)
M = getRotationMatrix2D(center_of_pic, angle, 1.0)
image = warpAffine(image, M, (w, h),flags=INTER_CUBIC, borderMode=BORDER_REPLICATE)
image = warpAffine(
image, M, (w, h), flags=INTER_CUBIC, borderMode=BORDER_REPLICATE
)
return image

'''for removing blurness from the image,
this function increase sharpness of the image.'''

def sharpness_blur(image):
sharpen_kernel = array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]])
sharpen_kernel = array([[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]])
image = filter2D(image, -1, sharpen_kernel)
return image

'''using pytesseract, this function extracting text from the image.'''

def to_text(image):
string_from_image = image_to_string(image,lang='eng')
string_from_image = image_to_string(image, lang='eng')
return string_from_image

'''show image on screen'''

def show_image(image):
imshow(' ',image)
imshow(' ', image)
8 changes: 4 additions & 4 deletions freshlybuiltimagebol/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Class import

from freshlybuiltimagebol.photo_se_nikali_awaj import PhotoAwaj
from freshlybuiltimagebol.text_bol_uthega import ShabdDhwani
from freshlybuiltimagebol.photo_se_text import PhotoShabd
from freshlybuiltimagebol.downloader import imagebol_model_downloader
from freshlybuiltimagebol.natural_photo_se_text import NaturalPhotoShabd
from freshlybuiltimagebol.OCR_Printed_Text import ImageProcess
from freshlybuiltimagebol.downloader import imagebol_model_downloader
from freshlybuiltimagebol.page import Page
from freshlybuiltimagebol.photo_se_nikali_awaj import PhotoAwaj
from freshlybuiltimagebol.photo_se_text import PhotoShabd
from freshlybuiltimagebol.text_bol_uthega import ShabdDhwani
4 changes: 2 additions & 2 deletions freshlybuiltimagebol/bhasha_codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
'yo': 'yoruba',
'zu': 'zulu',
'fil': 'Filipino',
'he': 'Hebrew'
}
'he': 'Hebrew',
}

bhasha_codes = dict(map(reversed, bhasha_kosh.items()))
161 changes: 89 additions & 72 deletions freshlybuiltimagebol/downloader.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from requests import get
from os import mkdir,path,remove
from tqdm import tqdm
from hashlib import md5
from os import mkdir, path, remove

import colorama as col
from requests import get
from tqdm import tqdm

"""
freshlybuiltimagebol library model downloader
Expand All @@ -20,129 +21,145 @@
1009 - signature mismatch
"""


class imagebol_model_downloader:
status_code=0000

def __init__(self,model_name,status_code=0000):
self.download_model(model_name,status_code)
status_code = 0000

def download_model(self,model_name,status_code):
def __init__(self, model_name, status_code=0000):
self.download_model(model_name, status_code)

def download_model(self, model_name, status_code):
dir_path = path.dirname(path.realpath(__file__))

available_models={
"F_est":["frozen_east_text_detection","94.4MB","8a9b7f2ebd9bcf8212bfa856b065e6f0"]
}

if path.isdir(dir_path+"models/")==False:

available_models = {
"F_est": [
"frozen_east_text_detection",
"94.4MB",
"8a9b7f2ebd9bcf8212bfa856b065e6f0",
]
}

if path.isdir(dir_path + "models/") == False:
try:
mkdir(dir_path+"/models")
mkdir(dir_path + "/models")
except:
print("models directory found")

if model_name in available_models:
model_name=available_models[model_name]
if path.isfile(dir_path+"/models/"+model_name[0]+".pb")==False:
model_name = available_models[model_name]
if path.isfile(dir_path + "/models/" + model_name[0] + ".pb") == False:
try:
print('starting model download')
print("don't quit until downlaoding completes")
print('download can take time depending upon your internet conection')
print(Fore.BLUE+model_name[0]+" is of "+model_name[1])
choice=input(Fore.YELLOW+"do you wish to download type 'y':")
if (choice=='y'):
return self.start_downloading(model_name,dir_path,status_code)
print(
'download can take time depending upon your internet conection'
)
print(Fore.BLUE + model_name[0] + " is of " + model_name[1])
choice = input(Fore.YELLOW + "do you wish to download type 'y':")
if choice == 'y':
return self.start_downloading(model_name, dir_path, status_code)
else:
print('download canceled')
status_code=0000
status_code = 0000
return status_code
print('model download successful')
self.status_code=1003
self.status_code = 1003
return status_code
except:
print('download interrupted')
try:
remove(dir_path+"/models/"+model_name[0] +".pb")
self.status_code=1002
remove(dir_path + "/models/" + model_name[0] + ".pb")
self.status_code = 1002
return status_code
except:
self.status_code=1002
self.status_code = 1002
return status_code
else:
print('model found')
print('checking encryption signature')
self.hash_signature_match(model_name,available_models,dir_path,status_code)
if self.status_code==1000:
self.hash_signature_match(
model_name, available_models, dir_path, status_code
)
if self.status_code == 1000:
return self.status_code
else:
print(self.status_code)
print(Fore.CYAN+model_name[0]+" is of "+model_name[1])
re_choice=input(Fore.YELLOW+"press 'y' to start re-downloading: ")
if re_choice =='y':
remove(dir_path+"/models/"+model_name[0] +".pb")
self.start_downloading(model_name,dir_path,status_code)
self.hash_signature_match(model_name,available_models,dir_path,status_code)
print(Fore.CYAN + model_name[0] + " is of " + model_name[1])
re_choice = input(
Fore.YELLOW + "press 'y' to start re-downloading: "
)
if re_choice == 'y':
remove(dir_path + "/models/" + model_name[0] + ".pb")
self.start_downloading(model_name, dir_path, status_code)
self.hash_signature_match(
model_name, available_models, dir_path, status_code
)
return self.status_code
else:
print("no reference found for "+model_name)
self.status_code=1001

else:
print("no reference found for " + model_name)
self.status_code = 1001
return status_code


def start_downloading(self,model_name,dir_path,status_code):
model_url= "https://raw.githubusercontent.com/FreshlyBuilt/freshlybuiltimagebol/master/freshlybuiltimagebol/models/"
response = get(model_url+model_name[0]+".pb", stream=True)

def start_downloading(self, model_name, dir_path, status_code):
model_url = "https://raw.githubusercontent.com/FreshlyBuilt/freshlybuiltimagebol/master/freshlybuiltimagebol/models/"
response = get(model_url + model_name[0] + ".pb", stream=True)
try:
response.raise_for_status()
response.raise_for_status()
except response.exceptions.HTTPError as errh:
print ("Http Error:",errh)
self.status_code=1004
print("Http Error:", errh)
self.status_code = 1004
return self.status_code
except response.exceptions.ConnectionError as errc:
print ("Error Connecting:",errc)
self.status_code=1005
print("Error Connecting:", errc)
self.status_code = 1005
return self.status_code
except response.exceptions.Timeout as errt:
print ("Timeout Error:",errt)
self.status_code=1006
print("Timeout Error:", errt)
self.status_code = 1006
return self.status_code
except response.exceptions.RequestException as err:
print ("OOps: Something Else",err)
self.status_code=1007
print("OOps: Something Else", err)
self.status_code = 1007
return self.status_code
with open(dir_path+"/models/"+model_name[0] +".pb", "wb") as f:
with open(dir_path + "/models/" + model_name[0] + ".pb", "wb") as f:
total_length = int(response.headers.get('content-length'))
if total_length is None:
f.write(response.content)
else:
chunk_size=1024
for data in tqdm(iterable = response.iter_content(chunk_size),total=total_length/chunk_size, unit = 'KB'):
chunk_size = 1024
for data in tqdm(
iterable=response.iter_content(chunk_size),
total=total_length / chunk_size,
unit='KB',
):
try:
f.write(data)
except:
pass
def hash_signature_match(self,model_name,available_models,dir_path,status_code):
model_checksum=dir_path+"/models/"+model_name[0]+".pb"
md5_hash=md5()
model_handler=open(model_checksum,"rb").read()

def hash_signature_match(self, model_name, available_models, dir_path, status_code):
model_checksum = dir_path + "/models/" + model_name[0] + ".pb"
md5_hash = md5()
model_handler = open(model_checksum, "rb").read()
md5_hash.update(model_handler)
hash_code=md5_hash.hexdigest()
hash_code = md5_hash.hexdigest()
if hash_code == model_name[2]:
col.init(autoreset=True)
print(col.Fore.GREEN+"signature matched")
print(col.Fore.GREEN + "signature matched")
col.deinit()
self.status_code=1000
self.status_code = 1000
return status_code
else:
col.init(autoreset=True)
print(Fore.RED+"warning signature mismatched, model may not work properly ")
print(
Fore.RED + "warning signature mismatched, model may not work properly "
)
col.deinit()
self.status_code=1009
self.status_code = 1009
return status_code




"""downloader_debugger"""
#model_name=input("model name: ")
#print(imagebol_model_downloader(model_name).status_code)

# model_name=input("model name: ")
# print(imagebol_model_downloader(model_name).status_code)
Loading

0 comments on commit a427890

Please sign in to comment.