直接上代码:
python
import face_recognition
import time
from PIL import Image, ImageDraw
def faceRecognition(fileName):
# 加载图片
image = face_recognition.load_image_file(fileName)
# 人脸定位
beginTime = time.time()
face_locations = face_recognition.face_locations(image)
image2 = Image.open(fileName)
pil_image = ImageDraw.Draw(image2)
for face_location in face_locations:
# 打印位置
top, right, bottom, left = face_location
print("A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right))
# 红色的边框颜色
red_color =(255, 0, 0)
# 边框的宽度
border_width = 3
# 要画红框的坐标 (x, y, x+width, y+height)
box_coordinates = (left, top, right, bottom)
# 画红框
pil_image.rectangle(box_coordinates, width=border_width, outline=red_color)
# 人脸图
# face_image = image[top:bottom, left:right]
# pil_image = Image.fromarray(face_image)
# pil_image.show()
image2.show()
if __name__ == '__main__':
faceRecognition('10010.jpg')
运行效果为:
完整代码地址:Python实现人脸识别算法