基于python的面部识别(需导入opencv、face_recognition)

基于python的面部识别:

完成此项目需要导入两个库 opencv、face_recognition

<1>

opevcv 比较好安装,首先导入 opencv 库
命令符输入 pip --version 看是否安装有 pip(没有的话去官网下载一个 pip 并解压,然后 cmd 打开命令输入

python setup.py install  (即可安装pip)

如果确认安装了,直接输入:

pip install opencv-python

安装 opencv 包,等待导入完成即可。

<2>

导入 face_recognition,这个可能比较复杂,关于这个库github上有介绍,你可以去看看 :https://github.com/ageitgey/face_recognition#face-recognition

一、如果你本机没有安装 vistual studio,就先下载安装,我下载的是目前最新版本:

通过此链接https://www.visualstudio.com/zh-hans/?rr=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3D5_Ryn-MeARNb9X8OwevGZ1ws1sOoXa-aqfaRbnEf0iuPHwUrSPhYk_7YfudLNNLh%26wd%3D%26eqid%3Df7742a880001b4cb000000055ad6bd7b

在这里插入图片描述
下载安装好后,提示重新启动电脑。重启后接下来下面步骤。

二、接下来安装 boost

通过此链接:https://www.boost.org/users/download/

进入如下页面:
在这里插入图片描述
解压 boost压缩文件,在 cmd 界面中,进入 boost 解压目录中
输入:bootstrap.bat 回车

出现如下界面:在这里插入图片描述
继续输入:.\b2 回车 这个过程比较长,大概四十分钟左右,请耐心等待。
在这里插入图片描述
出现下列界面情况,则说明安装成功。
在这里插入图片描述

三、接下来安装 cmake

通过此链接:https://cmake.org/download/

进入如下页面:在这里插入图片描述
选择加入系统环境( Add CMake to the system PATH for all users)
在这里插入图片描述
安装成功后

四、接下来安装 dlib

通过此链接:http://dlib.net/

进入如下页面:
在这里插入图片描述

下载解压后,打开 cmd 界面内,进入dlib的解压目录,输入:python setup.py install
在这里插入图片描述

最后,重新打开 cmd 界面内,输入:pip install face_recognition 导入完成即可。在这里插入图片描述
pip list 查看安装库列表 看是否成功导入以上库。
在这里插入图片描述
如果确认都完成了。接下来打开 python 输入代码即可:
代码如下:

import face_recognition
import cv2

video_capture = cv2.VideoCapture(0)


miaojianxi_image = face_recognition.load_image_file("D:\python工作\PyCharm 工作空间i/08A15DD447960583165D6464CA74CC3C.png")#自己选择图片作为识别依据图片
aobama_image2 = face_recognition.load_image_file("D:\python工作\PyCharm 工作空间i/70JP-fyrwsqk0881071.png")#自己选择图片作为识别依据图片
miaojianxi_face_encoding =face_recognition.face_encodings(miaojianxi_image)[0]
aobama_face_encoding2 =face_recognition.face_encodings(aobama_image2)[0]

known_face_encodings = [
    miaojianxi_face_encoding,aobama_face_encoding2
]


known_face_names = [
    "Miao miao","aobama"  #名字(对应前面识别图片顺序)
]

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)

    rgb_small_frame = small_frame[:, :, ::-1]

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

        face_names = []
        for face_encoding in face_encodings:
            matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
            name = "UNKONWN"

            if True in matches:
                first_match_index = matches.index(True)
                name = known_face_names[first_match_index]
            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), (255, 0, 255), 2)


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


    cv2.imshow('Face monitor', frame)


    if cv2.waitKey(1) & 0xFF == ord(' '):  #设置以空格结束程序
        break

video_capture.release()
cv2.destroyAllWindows()

运行效果如图:
在这里插入图片描述

到这了,希望大家成功完成!

转载请注明明出处谢谢https://blog.csdn.net/weixin_44609873/article/details/88385566

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个基于PythonOpenCVface_recognition的简单人脸识别代码示例: ```python import cv2 import face_recognition # 加载已知的人脸图像和对应的名字 obama_image = face_recognition.load_image_file("obama.jpg") obama_face_encoding = face_recognition.face_encodings(obama_image)[0] biden_image = face_recognition.load_image_file("biden.jpg") biden_face_encoding = face_recognition.face_encodings(biden_image)[0] known_face_encodings = [ obama_face_encoding, biden_face_encoding ] known_face_names = [ "Barack Obama", "Joe Biden" ] # 打开摄像头 cap = cv2.VideoCapture(0) while True: # 读取一帧图像 ret, frame = cap.read() # 转换为RGB图像 rgb_frame = frame[:, :, ::-1] # 检测人脸 face_locations = face_recognition.face_locations(rgb_frame) face_encodings = face_recognition.face_encodings(rgb_frame, face_locations) # 遍历每个检测到的人脸 for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings): # 判断是否和已知人脸匹配 matches = face_recognition.compare_faces(known_face_encodings, face_encoding) name = "Unknown" # 如果匹配到了已知人脸,则获取对应的名字 if True in matches: first_match_index = matches.index(True) name = known_face_names[first_match_index] # 在图像上绘制人脸矩形和名字 cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2) cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED) 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 # 释放摄像头和窗口资源 cap.release() cv2.destroyAllWindows() ``` 注意,这个代码示例要在已经安装了face_recognitionOpenCVPython环境中运行,还要把`obama.jpg`和`biden.jpg`两个已知人脸图像放在代码所在目录下。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值