Skip to content

Instantly share code, notes, and snippets.

View santhalakshminarayana's full-sized avatar

Santha Lakshmi Narayana santhalakshminarayana

View GitHub Profile
@santhalakshminarayana
santhalakshminarayana / imagetopdf_rgbtogray.py
Created September 27, 2019 14:33
Medium:Imagetopdf Load images and convert to gray scale
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)
@santhalakshminarayana
santhalakshminarayana / imagetopdf_fitcontours.py
Created September 27, 2019 14:40
Medium:Imagetopdf find and fit contours
#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
@santhalakshminarayana
santhalakshminarayana / imagetopdf_closerect.py
Last active September 27, 2019 14:46
Medium:Imagetopdf draw closest rectangle
#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))
@santhalakshminarayana
santhalakshminarayana / imagetopdf_pst.py
Created September 27, 2019 14:53
Medium:Imagetopdf perspective transformation
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))
@santhalakshminarayana
santhalakshminarayana / imagetopdf_sacnnedimage.py
Created September 27, 2019 14:56
Medium:Imagetopdf import image contrast and save image
#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'
@santhalakshminarayana
santhalakshminarayana / imagetopdf_savepdf.py
Created September 27, 2019 14:59
Medium:Imagetopdf save as pdf
#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)
@santhalakshminarayana
santhalakshminarayana / face_recogniton_detect_face.py
Created October 16, 2019 15:55
Face recognition detect faces in image
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
@santhalakshminarayana
santhalakshminarayana / face_recognition_vgg_face.py
Created October 16, 2019 16:08
Face recognition vgg face build
# 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
@santhalakshminarayana
santhalakshminarayana / face_recognition_train_test_data.py
Last active July 7, 2020 12:11
Face recognition train and test data preparation
# 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))
@santhalakshminarayana
santhalakshminarayana / face_recognition_softmax_classifier.py
Created October 16, 2019 16:28
Face recognition softmax classifier
# 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))