forked from pythontoday/face_recognition_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8e35af2
commit 5869144
Showing
2 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.idea/ | ||
*.pyc | ||
/dlib/ | ||
/venvFR/ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import os | ||
import pickle | ||
import sys | ||
import face_recognition | ||
|
||
|
||
def train_model_by_img(name): | ||
|
||
if not os.path.exists("dataset"): | ||
print("[ERROR] there is no directory 'dataset'") | ||
sys.exit() | ||
|
||
known_encodings = [] | ||
images = os.listdir("dataset") | ||
|
||
# print(images) | ||
|
||
for(i, image) in enumerate(images): | ||
print(f"[+] processing img {i + 1}/{len(images)}") | ||
# print(image) | ||
|
||
face_img = face_recognition.load_image_file(f"dataset/{image}") | ||
face_enc = face_recognition.face_encodings(face_img)[0] | ||
|
||
# print(face_enc) | ||
|
||
if len(known_encodings) == 0: | ||
known_encodings.append(face_enc) | ||
else: | ||
for item in range(0, len(known_encodings)): | ||
result = face_recognition.compare_faces([face_enc], known_encodings[item]) | ||
# print(result) | ||
|
||
if result[0]: | ||
known_encodings.append(face_enc) | ||
# print("Same person!") | ||
break | ||
else: | ||
# print("Another person!") | ||
break | ||
|
||
# print(known_encodings) | ||
# print(f"Length {len(known_encodings)}") | ||
|
||
data = { | ||
"name": name, | ||
"encodings": known_encodings | ||
} | ||
|
||
with open(f"{name}_encodings.pickle", "wb") as file: | ||
file.write(pickle.dumps(data)) | ||
|
||
return f"[INFO] File {name}_encodings.pickle successfully created" | ||
|
||
|
||
def main(): | ||
print(train_model_by_img("person_name")) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |