python 人脸检测与人脸识别

python 复制代码
'''
安装库文件: pip install dlib face_recognition
'''

import dlib
import face_recognition
import cv2
from PIL import Image, ImageDraw

# 判断运行环境 cpu or gpu
def check_env():
    print(dlib.DLIB_USE_CUDA)
    print(dlib.cuda.get_num_devices())

# 判断人脸在图片当中的位置
def get_face_location(image_flle):
    image_fr = face_recognition.load_image_file(image_flle)
    face_locations = face_recognition.face_locations(image_fr)
    print(face_locations)

    # 标记人脸的位置
    image_cv = cv2.imread(image_flle)
    for location in face_locations:
        y0, x1, y1, x0 = location
        cv2.rectangle(image_cv,(x0,y0),(x1,y1),(0,0,255),4)
    cv2.imwrite(image_flle + '.new_image.jpg', image_cv)

    return face_locations


# 提取人脸画面保存到本地
def extract_face(image_file):
    image_cv = cv2.imread(image_file)
    face_recognitions = get_face_location(image_file)
    for i, location in enumerate(face_recognitions):
        y0,x1,y1,x0 = location
        face_image = image_cv[y0:y1,x0:x1]
        cv2.imwrite(f"{image_file}.face_{i}.jpg",face_image)


# 把人脸信息编码为一个128维的向量
def encode_face(image_file):
    image_fr = face_recognition.load_image_file(image_file)
    face_recognitions = face_recognition.face_locations(image_fr)
    face_encodings = face_recognition.face_encodings(image_fr,face_recognitions)
    return face_encodings[0]
    
# 判断2个人脸是否为同一个人
def compare_face(image_file1, image_file2):
    face_encoding1 = encode_face(image_file1)
    face_encoding2 = encode_face(image_file2)
    ret = face_recognition.compare_faces([face_encoding1],face_encoding2)
    return ret

# 标记人脸局部和标识
def mark_face(image_file):
    image_fr = face_recognition.load_image_file(image_file)
    face_marks = face_recognition.face_landmarks(image_fr)
    
    image_pil = Image.fromarray(image_fr)
    image_draw = ImageDraw.Draw(image_pil)
    for face_mark in face_marks:
        for facial_feature in face_mark.keys():
            image_draw.line(face_mark[facial_feature],width=5)
    image_pil.save(f"{image_file}_face_mark.jpg")


# 人脸补扮
def beautify_face(image_file):
    image_fr = face_recognition.load_image_file(image_file)
    face_marks = face_recognition.face_landmarks(image_fr)
    image_pil = Image.fromarray(image_fr)
    for i, face_mark in enumerate(face_marks):
        image_draw = ImageDraw.Draw(image_pil)
        # 眉毛
        image_draw.polygon(face_mark['left_eyebrow'],fill=(68,54,39,128))
        image_draw.polygon(face_mark['right_eyebrow'],fill=(68,54,39,128))
        image_draw.line(face_mark['left_eyebrow'],fill=(68,54,39,150),width=2)
        image_draw.line(face_mark['right_eyebrow'],fill=(68,54,39,150),width=2)
        # 嘴唇
        image_draw.polygon(face_mark['top_lip'],fill=(150,0,0,60))
        image_draw.polygon(face_mark['bottom_lip'],fill=(150,0,0,60))
        image_draw.line(face_mark['top_lip'],fill=(150,0,0,20),width=2)
        image_draw.line(face_mark['bottom_lip'],fill=(150,0,0,20),width=2)
        # 眼睛
        image_draw.polygon(face_mark['left_eye'],fill=(255,255,255,20))
        image_draw.polygon(face_mark['right_eye'],fill=(255,255,255,20))
        image_draw.line(face_mark['left_eye'] + [face_mark['left_eye'][0]],fill=(0,0,0,50),width=2)
        image_draw.line(face_mark['right_eye'] + [face_mark['right_eye'][0]],fill=(0,0,0,50),width=2)

    image_pil.save(f"{image_file}.beautify_face.png")


def main():
    check_env()

    face_locations = get_face_location('1.webp')
    print(face_locations)

    extract_face('3.jpg')

    face_encodings = encode_face('1.webp.face_0.jpg')
    print(face_encodings)

    ret = compare_face('1.webp.face_1.jpg','3.jpg.face_1.jpg')
    print(ret)

    mark_face('1.webp')

    beautify_face('1.webp')


if __name__=="__main__":
    main()
相关推荐
UnderTurrets3 分钟前
A_Survey_on_3D_object_Affordance
pytorch·深度学习·计算机视觉·3d
清水白石00827 分钟前
解构异步编程的两种哲学:从 asyncio 到 Trio,理解 Nursery 的魔力
运维·服务器·数据库·python
山海青风31 分钟前
图像识别零基础实战入门 1 计算机如何“看”一张图片
图像处理·python
yugi9878381 小时前
用于图像分类的EMAP:概念、实现与工具支持
人工智能·计算机视觉·分类
彼岸花开了吗1 小时前
构建AI智能体:八十、SVD知识整理与降维:从数据混沌到语义秩序的智能转换
人工智能·python·llm
MM_MS1 小时前
Halcon图像锐化和图像增强、窗口的相关算子
大数据·图像处理·人工智能·opencv·算法·计算机视觉·视觉检测
山土成旧客1 小时前
【Python学习打卡-Day40】从“能跑就行”到“工程标准”:PyTorch训练与测试的规范化写法
pytorch·python·学习
闲人编程1 小时前
消息通知系统实现:构建高可用、可扩展的企业级通知服务
java·服务器·网络·python·消息队列·异步处理·分发器
大神君Bob2 小时前
【AI办公自动化】如何使用Pytho让Excel表格处理自动化
python
Heorine2 小时前
数学建模 绘图 图表 可视化(6)
python·数学建模·数据可视化