Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. #importing libraries
  2. import face_recognition
  3. import os
  4.  
  5.  
  6. #method for encoding the image
  7. #it takes path as argument, which is the address of the photo we want to encode
  8. def encoding(path):
  9. image = face_recognition.load_image_file(path)
  10. imageEncoding = face_recognition.face_encodings(image)[0]
  11. return imageEncoding
  12.  
  13.  
  14. #this mehod is for comparing the photo from unkwon folder with all photos in known folder
  15. #it takes 2 arguments, unkownPerson is the path of photo in unknown folder, while nameOfPerson is simple name of photo
  16. def compare(unkownPerson,nameOfPerson):
  17. isAttendant = False #it's used to check if the photo from unkown folder is already in known folder
  18. for image in os.listdir('/Users/yermukhanbet/PycharmProjects/dataBase/venv/lib/python3.7/known'): #iterates through photos in the known folder
  19. if image == '.DS_Store': #checks if the image is the hidden file ".DS_Store" and if yes then it passes and goes to the next file in the folder
  20. continue
  21. else:
  22. known_image_path = '/Users/yermukhanbet/PycharmProjects/dataBase/venv/lib/python3.7/known/'+image
  23. result = face_recognition.compare_faces([encoding(known_image_path)],encoding(unkownPerson), tolerance=0.45)
  24. print(image, str(result))
  25. if result[0] == True:
  26. print("Your face matched! You're attended")
  27. os.remove(unkownPerson)
  28. isAttendant = True
  29. break
  30. if isAttendant == False:
  31. print("You're added successfully!")
  32. os.rename(unkownPerson, "/Users/yermukhanbet/PycharmProjects/dataBase/venv/lib/python3.7/known/"+nameOfPerson)
  33.  
  34.  
  35.  
  36. def mainCompare():
  37. if len(os.listdir('/Users/yermukhanbet/PycharmProjects/dataBase/venv/lib/python3.7/unknown')) - 1 == 0:
  38. print("There are no pictures")
  39. else:
  40. for image in os.listdir('/Users/yermukhanbet/PycharmProjects/dataBase/venv/lib/python3.7/unknown'):
  41. if image == '.DS_Store':
  42. continue
  43. else:
  44. return compare('/Users/yermukhanbet/PycharmProjects/dataBase/venv/lib/python3.7/unknown/' + image, image)
  45.  
  46.  
  47.  
  48. mainCompare()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement