使用python进行人脸识别

1、安装 git 、cmake 、 python-pip

# 安装 git
$ sudo apt-get install -y git
# 安装 cmake
$ sudo apt-get install -y cmake
# 安装 python-pip
$ sudo apt-get install -y python-pip

2、安装编译dlib (安装face_recognition之前需要先安装编译dlib)

# 编译dlib前先安装 boost
$ sudo apt-get install libboost-all-dev

# 开始编译dlib
# 克隆dlib源代码
$ git clone https://github.com/davisking/dlib.git
$ cd dlib
$ mkdir build
$ cd build
$ cmake .. -DDLIB_USE_CUDA=0 -DUSE_AVX_INSTRUCTIONS=1
$ cmake --build .(注意中间有个空格)
$ cd ..
$ python setup.py install --yes USE_AVX_INSTRUCTIONS --no DLIB_USE_CUDA

3、安装face_recognition

# 安装 face_recognition
$ pip install face_recognition
# 安装face_recognition过程中会自动安装 numpy、scipy 等

一、识别1(简单代码实现人脸识别)

1.创建注册图像文件夹,照片命名为识别ID;

2.准备测试图像集,里面存放需要识别的图像;

3.运行face_recognition命令,把刚才准备的两个目录作为参数传入;

face_recognition  注册目录  识别目录

4.返回的结果即为识别的最终结果。

二、识别2(检测人脸并裁剪显示)

1.编写识别程序face_detect.py

# filename : face_recognize.py
# -*- coding: utf-8 -*-
# 导入pil模块 ,可用命令安装 apt-get install python-Imaging
from PIL import Image
# 导入face_recogntion模块,可用命令安装 pip install face_recognition
import face_recognition

# 将jpg文件加载到numpy 数组中
image = face_recognition.load_image_file("/opt/face/unknown_pic/all_star.jpg")

# 使用默认的给予HOG模型查找图像中所有人脸
# 这个方法已经相当准确了,但还是不如CNN模型那么准确,因为没有使用GPU加速
# 另请参见: find_faces_in_picture_cnn.py
face_locations = face_recognition.face_locations(image)

# 使用CNN模型
# face_locations = face_recognition.face_locations(image, number_of_times_to_upsample=0, model="cnn")

# 打印:我从图片中找到了 多少 张人脸
print("I found {} face(s) in this photograph.".format(len(face_locations)))

# 循环找到的所有人脸
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)) 
# 指定人脸的位置信息,然后显示人脸图片
        face_image = image[top:bottom, left:right]
        pil_image = Image.fromarray(face_image)
        pil_image.show()

2.执行识别程序;

python face_detect.py

备注:默认使用的是HOG模型查找照片中的人脸,也可选择使用CNN模型进行人脸检测。

三、识别3(检测人脸并提取特征)

1、编写识别程序face_feature.py

# filename : face_feature.py
# -*- coding: utf-8 -*-
# 导入pil模块 ,可用命令安装 apt-get install python-Imaging
from PIL import Image, ImageDraw
# 导入face_recogntion模块,可用命令安装 pip install face_recognition
import face_recognition

# 将jpg文件加载到numpy 数组中
image = face_recognition.load_image_file("biden.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()

2.执行程序

python face_feature.py

四、识别4(识别人脸并显示ID)

1.编写识别程序face_id.py

# filename : face_id.py
# -*- conding: utf-8 -*-
# 导入face_recogntion模块,可用命令安装 pip install face_recognition
import face_recognition

#将jpg文件加载到numpy数组中
babe_image = face_recognition.load_image_file("/opt/face/known_people/babe.jpeg")
Rong_zhu_er_image = face_recognition.load_image_file("/opt/face/known_people/Rong zhu er.jpg")
unknown_image = face_recognition.load_image_file("/opt/face/unknown_pic/babe2.jpg")

#获取每个图像文件中每个面部的面部编码
#由于每个图像中可能有多个面,所以返回一个编码列表。
#但是由于我知道每个图像只有一个脸,我只关心每个图像中的第一个编码,所以我取索引0。
babe_face_encoding = face_recognition.face_encodings(babe_image)[0]
Rong_zhu_er_face_encoding = face_recognition.face_encodings(Rong_zhu_er_image)[0]
unknown_face_encoding = face_recognition.face_encodings(unknown_image)[0]

known_faces = [
    babe_face_encoding,
    Rong_zhu_er_face_encoding
]
#结果是True/false的数组,未知面孔known_faces阵列中的任何人相匹配的结果
results = face_recognition.compare_faces(known_faces, unknown_face_encoding)

print("这个未知面孔是 Babe 吗? {}".format(results[0]))
print("这个未知面孔是 容祖儿 吗? {}".format(results[1]))
print("这个未知面孔是 我们从未见过的新面孔吗? {}".format(not True in results))

2.执行人脸识别程序

python face_id.py



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
抱歉,我是一名语言模型,无法进行本地代码实现,但可以提供大致思路: 要使用OpenCV C库打开摄像头,可以使用cv::VideoCapture类,它提供了一个简单的接口来从摄像头读取视频帧。可以使用以下代码来打开摄像头并显示视频流: ``` #include <opencv2/opencv.hpp> using namespace cv; int main(int argc, char** argv) { VideoCapture cap(0); // 打开默认的摄像头,如果有多个摄像头,可以使用1,2,3等来选择 if (!cap.isOpened()) { std::cout << "Failed to open camera." << std::endl; return -1; } while (true) { Mat frame; cap.read(frame); // 读取一帧视频 imshow("Camera", frame); // 显示视频帧 if (waitKey(30) == 27) { // 等待30毫秒,如果用户按下ESC键,则退出循环 break; } } return 0; } ``` 要进行人脸识别,可以使用OpenCV提供的人脸识别器类cv::CascadeClassifier。可以使用以下代码来进行人脸检测并在视频流中标记出人脸: ``` #include <opencv2/opencv.hpp> using namespace cv; int main(int argc, char** argv) { CascadeClassifier face_cascade; if (!face_cascade.load("haarcascade_frontalface_alt.xml")) { std::cout << "Failed to load face cascade." << std::endl; return -1; } VideoCapture cap(0); if (!cap.isOpened()) { std::cout << "Failed to open camera." << std::endl; return -1; } while (true) { Mat frame; cap.read(frame); std::vector<Rect> faces; face_cascade.detectMultiScale(frame, faces, 1.1, 2, 0|CASCADE_SCALE_IMAGE, Size(30, 30)); for (size_t i = 0; i < faces.size(); i++) { rectangle(frame, faces[i], Scalar(0, 0, 255), 2); // 标记出人脸 } imshow("Camera", frame); if (waitKey(30) == 27) { break; } } return 0; } ``` 这里使用的是OpenCV提供的Haar级联分类器来进行人脸检测,检测的结果是一个矩形框,可以使用opencv的rectangle函数来标记出来。 希望这些代码能帮助你开始实现你的程序。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值