Skip to content

Instantly share code, notes, and snippets.

@ardrian
Created April 6, 2018 00:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ardrian/535781f9b063dc81e9387de09f4cfd62 to your computer and use it in GitHub Desktop.
Save ardrian/535781f9b063dc81e9387de09f4cfd62 to your computer and use it in GitHub Desktop.
import face_recognition
import glob, os
def contains_face(filename):
image = face_recognition.load_image_file(filename)
face_locations = face_recognition.face_locations(image)
return len(face_locations) > 0
def main():
if not os.path.isdir("photos_with_faces"):
os.mkdir("photos_with_faces")
if not os.path.isdir("photos_without_faces"):
os.mkdir("photos_without_faces")
for file in os.listdir("input_photos"):
if file.endswith(".jpg") or file.endswith(".png"):
photo_path = os.path.join("input_photos", file)
try:
face = contains_face(photo_path)
if face:
os.rename(photo_path,os.path.join("photos_with_faces",file))
else:
os.rename(photo_path,os.path.join("photos_without_faces",file))
print("{} contains face? {}".format(photo_path,face))
except:
print("Error while reading {}".format(photo_path))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment