├── requirements.txt ├── icon.gif ├── test1.jpg ├── test2.jpg ├── face_repository ├── Beyonce.jpg ├── Rihanna.jpg ├── Will Smith.jpg ├── Johnny Depp.jpg └── Leonardo DiCaprio.jpg ├── README.md ├── LICENSE └── face.py /requirements.txt: -------------------------------------------------------------------------------- 1 | cmake 2 | dlib 3 | face_recognition 4 | numpy 5 | opencv-python -------------------------------------------------------------------------------- /icon.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/serhanelmacioglu/Face-Recognition_Coding-with-Python/HEAD/icon.gif -------------------------------------------------------------------------------- /test1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/serhanelmacioglu/Face-Recognition_Coding-with-Python/HEAD/test1.jpg -------------------------------------------------------------------------------- /test2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/serhanelmacioglu/Face-Recognition_Coding-with-Python/HEAD/test2.jpg -------------------------------------------------------------------------------- /face_repository/Beyonce.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/serhanelmacioglu/Face-Recognition_Coding-with-Python/HEAD/face_repository/Beyonce.jpg -------------------------------------------------------------------------------- /face_repository/Rihanna.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/serhanelmacioglu/Face-Recognition_Coding-with-Python/HEAD/face_repository/Rihanna.jpg -------------------------------------------------------------------------------- /face_repository/Will Smith.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/serhanelmacioglu/Face-Recognition_Coding-with-Python/HEAD/face_repository/Will Smith.jpg -------------------------------------------------------------------------------- /face_repository/Johnny Depp.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/serhanelmacioglu/Face-Recognition_Coding-with-Python/HEAD/face_repository/Johnny Depp.jpg -------------------------------------------------------------------------------- /face_repository/Leonardo DiCaprio.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/serhanelmacioglu/Face-Recognition_Coding-with-Python/HEAD/face_repository/Leonardo DiCaprio.jpg -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 |

Face Recognition (Coding with Python)

3 | 4 |
5 |
6 | 7 | ### Run Script in Visual Studio Code 8 | 9 | - #### A few python modules and packages are required to download in order to complete this project and have it run properly. 10 | - [Install Visual Studio Community](https://visualstudio.microsoft.com/vs/community) and then download Desktop Development with C++ workload in Visual Studio 11 | - [requirements.txt](https://github.com/serhanelmacioglu/Face-Recognition_Coding-with-Python/blob/main/requirements.txt) 12 | ``` python 13 | 14 | # Install from a list of packages 15 | pip install -r requirements.txt 16 | 17 | ``` 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Serhan Elmacıoğlu 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 | -------------------------------------------------------------------------------- /face.py: -------------------------------------------------------------------------------- 1 | import face_recognition as fr 2 | import os 3 | import cv2 4 | import face_recognition 5 | import numpy as np 6 | from time import sleep 7 | 8 | 9 | def get_encoded_faces(): 10 | 11 | encoded = {} 12 | 13 | for dirpath, dnames, fnames in os.walk("./face_repository"): 14 | for f in fnames: 15 | if f.endswith(".jpg") or f.endswith(".png"): 16 | face = fr.load_image_file("face_repository/" + f) 17 | encoding = fr.face_encodings(face)[0] 18 | encoded[f.split(".")[0]] = encoding 19 | 20 | return encoded 21 | 22 | 23 | def unknown_image_encoded(img): 24 | 25 | face = fr.load_image_file("face_repository/" + img) 26 | encoding = fr.face_encodings(face)[0] 27 | 28 | return encoding 29 | 30 | 31 | def classify_face(im): 32 | 33 | faces = get_encoded_faces() 34 | faces_encoded = list(faces.values()) 35 | known_face_names = list(faces.keys()) 36 | 37 | img = cv2.imread(im, 1) 38 | 39 | face_locations = face_recognition.face_locations(img) 40 | unknown_face_encodings = face_recognition.face_encodings(img, face_locations) 41 | 42 | face_names = [] 43 | for face_encoding in unknown_face_encodings: 44 | 45 | matches = face_recognition.compare_faces(faces_encoded, face_encoding) 46 | name = "Unknown" 47 | 48 | 49 | face_distances = face_recognition.face_distance(faces_encoded, face_encoding) 50 | best_match_index = np.argmin(face_distances) 51 | if matches[best_match_index]: 52 | name = known_face_names[best_match_index] 53 | 54 | face_names.append(name) 55 | 56 | for (top, right, bottom, left), name in zip(face_locations, face_names): 57 | 58 | cv2.rectangle(img, (left-20, top-10), (right+20, bottom+15), (300, 0, 0), 2) 59 | 60 | 61 | cv2.rectangle(img, (left-20, bottom -10), (right+20, bottom+15), (500, 0, 0), cv2.FILLED) 62 | font = cv2.FONT_HERSHEY_DUPLEX 63 | cv2.putText(img, name, (left -10, bottom + 10), font, 0.5, (300, 300, 300), 1) 64 | 65 | 66 | 67 | while True: 68 | 69 | cv2.imshow('Whom are you looking for?', img) 70 | if cv2.waitKey(1) & 0xFF == ord('q'): 71 | return face_names 72 | 73 | 74 | print(classify_face("test1.jpg")) # You can either try to find people "test2.jpg" or "test1.jpg" in the string. 75 | --------------------------------------------------------------------------------