Python实现人脸识别

直接上代码:

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实现人脸识别算法

相关推荐
DianSan_ERP2 小时前
电商架构演进:如何在高并发场景下实现多平台API的标准化履约?
运维·前端·网络·安全·架构·自动化
nothing&nowhere2 小时前
用 Python 做问卷数据清洗:无效样本检测与处理实战
开发语言·python·数据清洗·数据处理·问卷星·问卷星脚本·刷问卷
花酒锄作田3 小时前
如何发布自己的 Python 库到 PyPI
python
researcher-Jiang3 小时前
Design Patterns——Template Method入门到情景实战
python·设计模式·模板方法模式
碎_浪4 小时前
给键盘党的英语记忆工具:我做了一款「打字背单词」桌面应用
前端·程序员·ai编程
阿成学长_Cain5 小时前
Linux dirs命令详解|Bash目录堆栈管理快速切换目录实战教程
linux·运维·前端·数据库
多加点辣也没关系6 小时前
JavaScript|第4章:类型转换
开发语言·javascript
yqcoder6 小时前
httpOnly 是什么,又有什么用?
开发语言·前端·javascript