This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
files_in_dir=os.listdir() | |
curr_path=os.getcwd() | |
#get image file names in current directory | |
image_names=[] | |
conventions=['jpeg','png','jpg'] | |
for file in files_in_dir: | |
ext=file.split('.')[-1] | |
if ext in conventions: | |
image_names.insert(0,file) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Find contours in image using (tree retrival method) for hierarchy | |
image_conts=[] | |
for img in thsh_images: | |
contours,_=cv2.findContours(img.copy(),cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) | |
image_conts.append(contours) | |
#Look for maximum area contour which describes page/rectangle structure in image | |
max_area_conts=[] | |
for contour in image_conts: | |
max_ind,max_area=None,0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Draw closest four sided shape around maximum contour which is our | |
#area of interest in image | |
approx_cont=[] | |
for ind in range(len(images_read)): | |
epsilon=0.02*cv2.arcLength(image_conts[ind][max_area_conts[ind]],True) | |
approx=cv2.approxPolyDP(image_conts[ind][max_area_conts[ind]],epsilon,True) | |
approx_cont.append(np.squeeze(approx)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rect_images=[] | |
for ind in range(len(images_read)): | |
#top-left,bottom-left,bottom-right,top-right | |
tl,bl,br,tr=approx_cont[ind].tolist() | |
top_width=np.sqrt((tl[0]-tr[0])**2 + (tl[1]-tr[1])**2) | |
bottom_width=np.sqrt((bl[0]-br[0])**2 + (bl[1]-br[1])**2) | |
left_height=np.sqrt((tl[0]-bl[0])**2 + (tl[1]-bl[1])**2) | |
right_height=np.sqrt((tr[0]-br[0])**2 + (tr[1]-br[1])**2) | |
width=int(max(top_width,bottom_width)) | |
height=int(max(left_height,right_height)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Digitise image in black and white as a scanned document | |
digitised_image_names=[] | |
for ind in range(len(rect_images)): | |
img_gray=cv2.cvtColor(rect_images[ind].copy(),cv2.COLOR_BGR2GRAY) | |
th=threshold_local(img_gray.copy(),101,offset=10,method="gaussian") | |
img_gray=(img_gray>th) | |
imgg=Image.fromarray(img_gray) | |
size=(images_read[ind].shape[0],images_read[ind].shape[1]) | |
imgg.resize(size) | |
name=curr_path+"/digitised_"+image_names[ind].split('.')[0]+'.jpg' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Convert all digitised images to pdf format | |
digitised_images=[] | |
for name in digitised_image_names: | |
imgg=Image.open(name) | |
digitised_images.append(imgg) | |
name=curr_path+"/digitised_images"+'.pdf' | |
if len(digitised_images)>1: | |
digitised_images[0].save(name,save_all=True,append_images=digitised_images[1:],resolution=100.0) | |
else: | |
digitised_images[0].save(name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import dlib | |
# Load cnn_face_detector with 'mmod_face_detector' | |
dnnFaceDetector=dlib.cnn_face_detection_model_v1("mmod_human_face_detector.dat") | |
# Load image | |
img=cv2.imread('path_to_image') | |
# Convert to gray scale |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Tensorflow version == 2.0.0 | |
import numpy as np | |
import tensorflow as tf | |
from tensorflow import keras | |
from tensorflow.keras.models import Sequential,Model | |
from tensorflow.keras.layers import ZeroPadding2D,Convolution2D,MaxPooling2D | |
from tensorflow.keras.layers import Dense,Dropout,Softmax,Flatten,Activation,BatchNormalization | |
from tensorflow.keras.preprocessing.image import load_img,img_to_array | |
from tensorflow.keras.applications.imagenet_utils import preprocess_input | |
import tensorflow.keras.backend as K |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Prepare Train Data | |
x_train=[] | |
y_train=[] | |
person_rep=dict() | |
person_folders=os.listdir(path+'/Images_crop/') | |
for i,person in enumerate(person_folders): | |
person_rep[i]=person | |
image_names=os.listdir('Images_crop/'+person+'/') | |
for image_name in image_names: | |
img=load_img(path+'/Images_crop/'+person+'/'+image_name,target_size=(224,224)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Softmax regressor to classify images based on encoding | |
classifier_model=Sequential() | |
classifier_model.add(Dense(units=100,input_dim=x_train.shape[1],kernel_initializer='glorot_uniform')) | |
classifier_model.add(BatchNormalization()) | |
classifier_model.add(Activation('tanh')) | |
classifier_model.add(Dropout(0.3)) | |
classifier_model.add(Dense(units=10,kernel_initializer='glorot_uniform')) | |
classifier_model.add(BatchNormalization()) | |
classifier_model.add(Activation('tanh')) | |
classifier_model.add(Dropout(0.2)) |