Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
CuFFaz authored Jun 2, 2021
1 parent f766a11 commit 31ae876
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 0 deletions.
66 changes: 66 additions & 0 deletions face_extractor_caffe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#Importing Required Libraries
import os
import cv2
import numpy as np
import time

#Starting time counter
start = time.time()

#Specifying paths of training data
root_dir = 'trainset'

#Specifying paths to Model architecture and Pretrained Weights
prototxt_path = 'model_data/deploy.prototxt'
caffemodel_path = 'model_data/weights.caffemodel'

#Empty Var for storing our Extracted Faces just for fun and info
IMAGES = []

#Implementing our pretrained DNN with Caffe Model for detecting frontal faces from our training data.
model = cv2.dnn.readNetFromCaffe(prototxt_path, caffemodel_path)

i = 1

#Iterating through directories & subdirectories and extracting the path of every jpg file present in "trainset" folder
#Outcome would be that the Extracted Face inside a particular image will be written in the same destination with the same name to that of the original file
for subdir, dirs,files in os.walk(root_dir):
for file in files:
#Reading/Loading the image
image = cv2.imread(os.path.join(subdir, file))
#Grabbing image dimensions
(h, w) = image.shape[:2]
#Resizing image to a specific dimension and creating a blob
blob = cv2.dnn.blobFromImage(cv2.resize(image, (256, 256)), 1.0, (256, 256), (104.0, 177.0, 123.0))
#Feeding the blob to our pretrained DNN
model.setInput(blob)
detections = model.forward()
#Looping through every single face detection in our image
for i in range(0, detections.shape[2]):
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
#getting the dims of the extracted box containing our face
(startX, startY, endX, endY) = box.astype("int")
#grabbing the confidence value of the predicted box that its a face
confidence = detections[0, 0, i, 2]
#Only those Detections with more than 50% confidence will be extracted
if (confidence > 0.5):
#Slicing out the face
crop = image[startY:endY, startX:endX]
#Exception created for empty images.
#7-10 recurrent empty images were found after image processing done above thus had to exclude them
try:
#Saving the image in the same folder with same names
#thus replacing our previous image and keeping the Extracted Face image only
cv2.imwrite(os.path.join(subdir, file), crop)
IMAGES.append(crop)
except:
#exception of empty images that couldnt be written/saved. (Got 7-10 such images, couldnt understand why facing issue with this method)
print(f"Number of Empty Ones found:-{i}")
i+=1

#Seizing Time Counter
end = time.time()

#Just some details for self understanding (Total Number of Faces extracted from the dataset, type of the variable
#and total time taken to process)
print(len(IMAGES), type(IMAGES), ((start-end)/60))
54 changes: 54 additions & 0 deletions face_extractor_cascade.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#Importing Required Libraries
import os
import cv2
import time

#Starting time counter
start = time.time()

#Specifying path of training data
root_dir = 'trainset'

#Empty List for storing our Extracted Faces
IMAGES = []

#Implementing our trained HaarClassifier for detecting frontal faces from our training data.
kascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")

#Iterating through directories & subdirectories and extracting the path of every jpg file present in "trainset" folder
#Outcome would be that the Extracted Face inside a particular image will be written in the same destination with the same name to that of the original file
for subdir, dirs,files in os.walk(root_dir):
for file in files:
#Reading/Loading the image
img = cv2.imread(os.path.join(subdir, file))
#resizing the image but keeping aspect ratio of the image unchanged (this is with.respect.to HEIGHT)
#this is achieved by the formula (original width x new height)/ original height = new width
height = 256
width = img.shape[1]*height/img.shape[0]
img = cv2.resize(img, (int(width), height), None, 0.5, 0.5, interpolation=cv2.INTER_AREA)
#Conversion to a grayscale image
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#Feeding the grayscale image to our Classifier
face = kascade.detectMultiScale(
gray,
scaleFactor=1.3,
minNeighbors=5
)
#HaarClassifier returned a 2D array, thus I'll extract these dimensions
#and use the same dims to crop out the portion from our original image. Hence preserving our color channels.
for (x,y,w,h) in face:
#Slicing out the face as mentioned above
crop = img[y:y+h,x:x+w]
#Saving the image in the same folder with same names
#thus replacing our previous image and keeping the Extracted Face image only
cv2.imwrite(os.path.join(subdir, file), crop)
#Adding every extracted face to our empty list
IMAGES.append(crop)


#Seizing Time Counter
end = time.time()

#Just some details for self understanding (Total Number of Faces extracted from the dataset, type of the variable
#and total time taken to process)
print(len(IMAGES), type(IMAGES), ((start-end)/60))
40 changes: 40 additions & 0 deletions face_extractor_fr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#Importing Required Libraries
import os
import time
import face_recognition
from PIL import Image

#Starting time counter
start = time.time()

#Specifying path of training data
root_dir = 'trainset'

#Empty List for storing our Extracted Faces just for fun and info
IMAGES = []

#Iterating through directories & subdirectories and extracting the path of every jpg file present in "trainset" folder
#Outcome would be that the Extracted Face inside a particular image will be written in the same destination with the same name to that of the original file
for subdir, dirs,files in os.walk(root_dir):
for file in files:
#Loading the Image file, it is stored in the form of a numpy array.
img = face_recognition.load_image_file(os.path.join(subdir, file))
#Extracting the face locations from the array based image
face_locs = face_recognition.face_locations(img)
#face_recognition returns an array of the face locations,
#hence I'll use the same dims to crop out the portion from our original image.
for (top, right, bottom, left) in face_locs:
crop = img[top:bottom, left:right]
#Convert the face image to a PIL-format image so that we can save it in the same folder with same names
#thus replacing our previous image and keeping the Extracted Face image only
pil_image = Image.fromarray(crop)
#saving the resulting face image
pil_image.save(os.path.join(subdir, file))
#Adding every resulting extracted face to our empty list
IMAGES.append(pil_image)

#Seizing the counter
end = time.time()
#Just some details for self understanding (Total Number of Faces extracted from the dataset, type of the variable
#and total time taken to process)
print(len(IMAGES), type(IMAGES), ((end-start)/60))

0 comments on commit 31ae876

Please sign in to comment.