├── Face_cascade.xml ├── LICENSE ├── README.md ├── detect_face.py └── extras ├── 1.png ├── 2.png └── 3.png /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Jeevesh Narang 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Face-Extractor 2 | A Python tool that can extract Faces from Images. 3 | 4 | I also built a similar tool capable of **[Facial recognition](https://github.com/JeeveshN/Facial-Recognition-Tool)**. 5 | 6 | ## Installations 7 | ``` 8 | virtualenv venv 9 | source venv/bin/activate 10 | pip install opencv-python 11 | ``` 12 | ## Usage: 13 | ``` 14 | git clone https://github.com/JeeveshN/Face-Detect.git 15 | cd Face-Detect 16 | python detect_face.py "Image path or just name if in same folder" 17 | ``` 18 | ## Demo: 19 | 20 | ### Command 21 | ![](/extras/2.png?raw=True) 22 | ### Detected Faces 23 | ![](/extras/1.png?raw=True) 24 | ### The Extracted Images 25 | ![](/extras/3.png?raw=True) 26 | 27 | **Note**:If some faces are not detected try reducing the value of scalefactor in Detect_face.py on Line 20 28 | 29 | -------------------------------------------------------------------------------- /detect_face.py: -------------------------------------------------------------------------------- 1 | from random import randint 2 | import cv2 3 | import sys 4 | import os 5 | import traceback 6 | 7 | CASCADE="Face_cascade.xml" 8 | FACE_CASCADE=cv2.CascadeClassifier(CASCADE) 9 | 10 | def detect_faces(image_path,display=True): 11 | 12 | image=cv2.imread(image_path) 13 | image_grey=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) 14 | 15 | faces = FACE_CASCADE.detectMultiScale(image_grey,scaleFactor=1.16,minNeighbors=5,minSize=(25,25),flags=0) 16 | 17 | for x,y,w,h in faces: 18 | sub_img=image[y-10:y+h+10,x-10:x+w+10] 19 | os.chdir("Extracted") 20 | cv2.imwrite(str(randint(0,10000))+".jpg",sub_img) 21 | os.chdir("../") 22 | cv2.rectangle(image,(x,y),(x+w,y+h),(255, 255,0),2) 23 | 24 | if display: 25 | cv2.imshow("Faces Found",image) 26 | # if (cv2.waitKey(0) & 0xFF == ord('q')) or (cv2.waitKey(0) & 0xFF == ord('Q')): 27 | # cv2.destroyAllWindows() 28 | 29 | if __name__ == "__main__": 30 | 31 | if not "Extracted" in os.listdir("."): 32 | os.mkdir("Extracted") 33 | 34 | if len(sys.argv) < 2: 35 | print("Usage: python Detect_face.py 'image path'") 36 | sys.exit() 37 | 38 | if os.path.isdir(sys.argv[1]): 39 | for image in os.listdir(sys.argv[1]): 40 | try: 41 | print ("Processing.....",os.path.abspath(os.path.join(sys.argv[1],image))) 42 | detect_faces(os.path.abspath(os.path.join(sys.argv[1],image)),False) 43 | except Exception: 44 | print ("Could not process ",os.path.abspath(os.path.join(sys.argv[1],image))) 45 | else: 46 | detect_faces(sys.argv[1]) 47 | -------------------------------------------------------------------------------- /extras/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JeeveshN/Face-Detect/b9eab15523c2fc35285f56d37d9dc5cad8f2aa0b/extras/1.png -------------------------------------------------------------------------------- /extras/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JeeveshN/Face-Detect/b9eab15523c2fc35285f56d37d9dc5cad8f2aa0b/extras/2.png -------------------------------------------------------------------------------- /extras/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JeeveshN/Face-Detect/b9eab15523c2fc35285f56d37d9dc5cad8f2aa0b/extras/3.png --------------------------------------------------------------------------------