Home | 简体中文 | 繁体中文 | 杂文 | 知乎专栏 | 51CTO学院 | CSDN程序员研修院 | Github | OSChina 博客 | 腾讯云社区 | 阿里云栖社区 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏多维度架构

16.3. 定位头像位置

		
import face_recognition as face

image = face.load_image_file("face.jpeg")
face_locations = face.face_locations(image)

print(face_locations)		
		
		
		
neo@MacBook-Pro-Neo ~/workspace/python/face % python3.9 /Users/neo/workspace/python/face/face_locations.py
[(231, 676, 498, 409)]		
		
		

使用深度学习模型达到更加精准的人脸定位

		
import face_recognition

image = face_recognition.load_image_file("my_picture.jpg")
face_locations = face_recognition.face_locations(image, model="cnn")		
		
		

例 16.1. 找出图片中头像

			
import PIL.Image as img
import face_recognition as face

file = "face.jpeg"
image = face.load_image_file(file)
face_locations = face.face_locations(image)

print(face_locations)

if face_locations:
    (top, right, bottom, left) = face_locations[0]
    im = img.open(file)
    box = (left, top, right, bottom)
    head = im.crop(box)
    head.save('head.jpg')