Created
September 30, 2019 19:56
-
-
Save aymericdelab/72cf25ccf640bcc4c723d383ab83c543 to your computer and use it in GitHub Desktop.
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 98, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import cv2\n", | |
"import os\n", | |
"\n", | |
"dim1,dim2 = 28,28\n", | |
"\n", | |
"founders=['Jeff Bezos','Larry Page','Bill Gates']\n", | |
"\n", | |
"for founder in founders:\n", | |
"\n", | |
" #prefix=r'.\\data\\google\\{}'.format(founder)\n", | |
" prefix=r'.\\data\\bing\\{}'.format(founder)\n", | |
" folder=prefix+'\\\\faces_{}x{}'.format(str(dim1),str(dim2))\n", | |
" os.makedirs(folder,exist_ok=True)\n", | |
"\n", | |
" image_list=[os.path.join(prefix, partial) for partial in os.listdir(prefix)]\n", | |
"\n", | |
" count=0\n", | |
" for i in range(len(image_list[:-1])-1):\n", | |
" image = cv2.imread(image_list[i])\n", | |
" gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n", | |
" faceCascade = cv2.CascadeClassifier(cv2.data.haarcascades + \"haarcascade_frontalface_default.xml\")\n", | |
" faces = faceCascade.detectMultiScale(\n", | |
" gray,\n", | |
" scaleFactor=1.1,\n", | |
" minNeighbors=4,\n", | |
" minSize=(100, 100)\n", | |
" ) \n", | |
" for (x, y, w, h) in faces:\n", | |
" crop_img = gray[y:y+h, x:x+w]\n", | |
" resized_img=cv2.resize(crop_img,(dim1,dim2))\n", | |
" path=folder+\"\\\\{}_{}.jpg\".format(founder.replace(\" \",\"_\"),count)\n", | |
" cv2.imwrite(path, resized_img)\n", | |
" count+=1" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "sagemaker", | |
"language": "python", | |
"name": "sagemaker" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.9" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment