Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import face_recognition
- from PIL import Image, ImageDraw
- def face_rectangle():
- metallica_face_img = face_recognition.load_image_file("img/metallica.jpg")
- metallica_face_location = face_recognition.face_locations(metallica_face_img)
- russian_img = face_recognition.load_image_file("img/Russia.jpg")
- russian_faces_locations = face_recognition.face_locations(russian_img)
- pil_img1 = Image.fromarray(metallica_face_img)
- draw1 = ImageDraw.Draw(pil_img1)
- for(top, right, bottom, left) in metallica_face_location:
- draw1.rectangle(((left, top), (right, bottom)), outline=(255, 255, 0), width=3)
- del draw1
- pil_img1.save("img/new_metallica.jpg")
- pil_img2 = Image.fromarray(russian_img)
- draw2 = ImageDraw.Draw(pil_img2)
- for(top, right, bottom, left) in russian_faces_locations:
- draw2.rectangle(((left, top), (right, bottom)), outline=(255, 255, 0), width=3)
- del draw2
- pil_img2.save("img/new_Russian.jpg")
- def extracting_faces(img_path):
- count = 0
- faces = face_recognition.load_image_file(img_path)
- faces_locations = face_recognition.face_locations(faces)
- for face_location in faces_locations:
- top, right, bottom, left = face_location
- face_img = faces[top:bottom, left:right]
- pil_img = Image.fromarray(face_img)
- pil_img.save(f"img/{count}_face_img.jpg")
- count += 1
- return f"Found {count} face(s) in this photo"
- def main():
- face_rectangle()
- print(extracting_faces("img/new_Russian.jpg"))
- #print(extracting_faces("img/new_metallica.jpg"))
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement