Advertisement
slik1977

Faces_photo_recognition 1.0

Feb 11th, 2022
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.64 KB | None | 0 0
  1. import face_recognition
  2. from PIL import Image, ImageDraw
  3.  
  4.  
  5. def face_rectangle():
  6.     metallica_face_img = face_recognition.load_image_file("img/metallica.jpg")
  7.     metallica_face_location = face_recognition.face_locations(metallica_face_img)
  8.  
  9.     russian_img = face_recognition.load_image_file("img/Russia.jpg")
  10.     russian_faces_locations = face_recognition.face_locations(russian_img)
  11.  
  12.     pil_img1 = Image.fromarray(metallica_face_img)
  13.     draw1 = ImageDraw.Draw(pil_img1)
  14.  
  15.     for(top, right, bottom, left) in metallica_face_location:
  16.         draw1.rectangle(((left, top), (right, bottom)), outline=(255, 255, 0), width=3)
  17.  
  18.     del draw1
  19.     pil_img1.save("img/new_metallica.jpg")
  20.  
  21.     pil_img2 = Image.fromarray(russian_img)
  22.     draw2 = ImageDraw.Draw(pil_img2)
  23.  
  24.     for(top, right, bottom, left) in russian_faces_locations:
  25.         draw2.rectangle(((left, top), (right, bottom)), outline=(255, 255, 0), width=3)
  26.  
  27.     del draw2
  28.     pil_img2.save("img/new_Russian.jpg")
  29.  
  30.  
  31. def extracting_faces(img_path):
  32.     count = 0
  33.     faces = face_recognition.load_image_file(img_path)
  34.     faces_locations = face_recognition.face_locations(faces)
  35.  
  36.     for face_location in faces_locations:
  37.         top, right, bottom, left = face_location
  38.  
  39.         face_img = faces[top:bottom, left:right]
  40.         pil_img = Image.fromarray(face_img)
  41.         pil_img.save(f"img/{count}_face_img.jpg")
  42.         count += 1
  43.  
  44.     return f"Found {count} face(s) in this photo"
  45.  
  46.  
  47.  
  48.  
  49. def main():
  50.     face_rectangle()
  51.     print(extracting_faces("img/new_Russian.jpg"))
  52.     #print(extracting_faces("img/new_metallica.jpg"))
  53.  
  54.  
  55. if __name__ == '__main__':
  56.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement