Skip to content

Instantly share code, notes, and snippets.

@aymericdelab
Created October 16, 2019 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aymericdelab/73c5264375ea133933db4edaba4b10ac to your computer and use it in GitHub Desktop.
Save aymericdelab/73c5264375ea133933db4edaba4b10ac to your computer and use it in GitHub Desktop.
import cv2
import os
dim1,dim2 = 28,28
founders=['Jeff Bezos','Larry Page','Bill Gates']
for founder in founders:
#prefix=r'.\data\google\{}'.format(founder)
prefix=r'.\data\bing\{}'.format(founder)
folder=prefix+'\\faces_{}x{}'.format(str(dim1),str(dim2))
os.makedirs(folder,exist_ok=True)
image_list=[os.path.join(prefix, partial) for partial in os.listdir(prefix)]
count=0
for i in range(len(image_list[:-1])-1):
image = cv2.imread(image_list[i])
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faceCascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=4,
minSize=(100, 100)
)
for (x, y, w, h) in faces:
crop_img = gray[y:y+h, x:x+w]
resized_img=cv2.resize(crop_img,(dim1,dim2))
path=folder+"\\{}_{}.jpg".format(founder.replace(" ","_"),count)
cv2.imwrite(path, resized_img)
count+=1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment