Python实现人脸识别的应用实例

现在人脸识别很火,现在我将举几个例子实现人脸识别的实例。
首先,要先安装相应的模块,在Python中有一个专门的模块叫做face_recognition模块,所以Python来做人工智能还是有很大的优势的。
Github网址为(https://github.com/ageitgey/face_recognition)
在windows上面安装Python模块自古以来就是一个很难解决的问题,最好是在anaconda上面安装,我很菜,所以在anaconda上面的一个虚拟环境中安装的,命令依次是:activate tensorflow #这是进入tensorflow的虚拟环境
然后:pip install face_recognition 大约是100多兆的模块
还有另一个安装方法,就是先下载dlib模块,在(https://pypi.org/simple/dlib/)找到自己电脑版本的下载包,然后pip安装
但是在安装dlib模块前,之前的工作是安装好dlib,在安装dlib的前面的工作是:
pip install cmake#安装cmake模块
pip install boost#安装boost模块
之后:pip install dlib//安装dlib模块
最后pip install face_recognition//安装face_recognition模块
下面是实例;附上参考链接face_recognition的5个应用实例
实例一 检测给定图像中的所有人脸

import cv2
import face_recognition

def main():
    img = face_recognition.load_image_file("dulante.jpg")  # 给图片命名的时候不要用中文 

    face_locations = face_recognition.face_locations(img)
    print(face_locations)

    # 调用opencv函数显示图片
    img = cv2.imread("dulante.jpg")
    cv2.namedWindow("原图")
    cv2.imshow("原图", img)
    # 遍历每个人脸,并标注
    faceNum = len(face_locations)
    for i in range(0, faceNum):
        top = face_locations[i][0]
        right = face_locations[i][1]
        bottom = face_locations[i][2]
        left = face_locations[i][3]

        start = (left, top)
        end = (right, bottom)

        color = (55, 255, 155)
        thickness = 3
        cv2.rectangle(img, start, end, color, thickness)

    # 显示识别结果
    cv2.namedWindow("识别")
    cv2.imshow("识别", img)

    cv2.waitKey(0)
    cv2.destroyAllWindows()
if __name__=='__main__':
    # 读取图片并识别人脸
    main()



效果图:
在这里插入图片描述
实例二 识别图像中的人脸
在这里插入图片描述
img文件夹中的图片:

在这里插入图片描述

# -*- coding: utf-8 -*-
import os
import face_recognition
def main():
    # 制作所有可用图像的列表
    images = os.listdir('img')
    # print(images)
    # 加载图像
    image_to_be_matched = face_recognition.load_image_file('yaodao.jpg')

    # 将加载图像编码为特征向量

    image_to_be_matched_encoded = face_recognition.face_encodings(

        image_to_be_matched)[0]

    # 遍历每张图像
    for image in images:
        # 加载图像
        current_image = face_recognition.load_image_file("img/" + image)
        # print(type(current_image))
        # 将加载图像编码为特征向量
        current_image_encoded = face_recognition.face_encodings(current_image)[0]

        # 将你的图像和图像对比,看是否为同一人

        result = face_recognition.compare_faces(

            [image_to_be_matched_encoded], current_image_encoded)

        # 检查是否一致

        if result[0] == True:

            print("Matched: " + image)

        else:

            print("Not matched: " + image)


if __name__=='__main__':
    main()

结果展示:
在这里插入图片描述

实例三 实时人脸识别

# -*- coding: utf-8 -*-
import face_recognition
import cv2
def main():
    video_capture = cv2.VideoCapture(0)

    obama_img = face_recognition.load_image_file("wh.jpg")
    obama_face_encoding = face_recognition.face_encodings(obama_img)[0]

    face_locations = []
    face_encodings = []
    face_names = []
    process_this_frame = True

    while True:
        ret, frame = video_capture.read()

        small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)

        if process_this_frame:
            face_locations = face_recognition.face_locations(small_frame)
            face_encodings = face_recognition.face_encodings(small_frame, face_locations)

            face_names = []
            for face_encoding in face_encodings:
                match = face_recognition.compare_faces([obama_face_encoding], face_encoding)

                if match[0]:
                    name = "lq"
                else:
                    name = "unkonwn"

                face_names.append(name)

        process_this_frame = not process_this_frame

        for (top, right, bottom, left), name in zip(face_locations, face_names):
            top *= 4
            right *= 4
            bottom *= 4
            left *= 4

            cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)

            cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), 2)
            font = cv2.FONT_HERSHEY_DUPLEX
            cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)

        cv2.imshow('Video', frame)
        # 按Q退出,结束程序
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    video_capture.release()
    cv2.destroyAllWindows()

if __name__=='main':
    main()

实例四 检测和标记图像中的人脸特征

# -*- coding: utf-8 -*-
import face_recognition
from PIL import Image,ImageDraw
def main():
    # 将jpg文件加载到numpy 数组中
    image = face_recognition.load_image_file("yaodao.jpg")

    # 查找图像中所有面部的所有面部特征
    face_landmarks_list = face_recognition.face_landmarks(image)
    # 打印发现的脸张数
    print("I found {} face(s) in this photograph.".format(len(face_landmarks_list)))

    for face_landmarks in face_landmarks_list:

        # 打印此图像中每个面部特征的位置
        facial_features = [
            'chin',
            'left_eyebrow',
            'right_eyebrow',
            'nose_bridge',
            'nose_tip',
            'left_eye',
            'right_eye',
            'top_lip',
            'bottom_lip'
        ]

        for facial_feature in facial_features:
            print("The {} in this face has the following points: {}".format(facial_feature,
                                                                            face_landmarks[facial_feature]))

            # 让我们在图像中描绘出每个人脸特征!
        pil_image = Image.fromarray(image)
        d = ImageDraw.Draw(pil_image)

        for facial_feature in facial_features:
            d.line(face_landmarks[facial_feature], width=5)

        pil_image.show()

if __name__ == '__main__':
    main()

在这里插入图片描述

实例五 识别人脸并美颜

在这里插入图片描述这是原图

# -*- coding: utf-8 -*-
from PIL import Image, ImageDraw
import face_recognition
def main():
    # 将jpg文件加载到numpy数组中
    image = face_recognition.load_image_file("dulante.jpg")

    # 查找图像中所有面部的所有面部特征
    face_landmarks_list = face_recognition.face_landmarks(image)

    for face_landmarks in face_landmarks_list:
        pil_image = Image.fromarray(image)
        d = ImageDraw.Draw(pil_image, 'RGBA')

        # 让眉毛变成了一场噩梦
        d.polygon(face_landmarks['left_eyebrow'], fill=(68, 54, 39, 128))
        d.polygon(face_landmarks['right_eyebrow'], fill=(68, 54, 39, 128))
        d.line(face_landmarks['left_eyebrow'], fill=(68, 54, 39, 150), width=5)
        d.line(face_landmarks['right_eyebrow'], fill=(68, 54, 39, 150), width=5)

        # 光泽的嘴唇
        d.polygon(face_landmarks['top_lip'], fill=(150, 0, 0, 128))
        d.polygon(face_landmarks['bottom_lip'], fill=(150, 0, 0, 128))
        d.line(face_landmarks['top_lip'], fill=(150, 0, 0, 64), width=8)
        d.line(face_landmarks['bottom_lip'], fill=(150, 0, 0, 64), width=8)

        # 闪耀眼睛
        d.polygon(face_landmarks['left_eye'], fill=(255, 255, 255, 30))
        d.polygon(face_landmarks['right_eye'], fill=(255, 255, 255, 30))

        # 涂一些眼线
        d.line(face_landmarks['left_eye'] + [face_landmarks['left_eye'][0]], fill=(0, 0, 0, 110), width=6)
        d.line(face_landmarks['right_eye'] + [face_landmarks['right_eye'][0]], fill=(0, 0, 0, 110), width=6)

        pil_image.show()
if __name__=='__main__':
    main()

效果图
在这里插入图片描述真的可怕,哈哈哈哈哈哈

  • 8
    点赞
  • 99
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值