专栏名称: 新机器视觉
最前沿的机器视觉与计算机视觉技术
目录
相关文章推荐
爱可可-爱生活  ·  《AI的“鱼与熊掌”:既要跑得快,又要学得好 ... ·  昨天  
爱可可-爱生活  ·  《AI偷懒的艺术:如何用“草稿”撬动“大模型 ... ·  昨天  
爱可可-爱生活  ·  #用音频给生活转个场# #音频充电站# ... ·  昨天  
爱可可-爱生活  ·  【[166星]filedb:基于Bitcas ... ·  2 天前  
51好读  ›  专栏  ›  新机器视觉

手把手教你运用Python实现进阶版人脸识别

新机器视觉  · 公众号  · AI  · 2025-06-07 21:45

正文

请到「今天看啥」查看全文


( 'Being processed picture %s' % index)
# 从摄像头读取照片
success, img = camera.read()
# 转为灰度图片
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 使用detector进行人脸检测
dets = detector(gray_img, 1)

for i, d in enumerate(dets):
x1 = d.top() if d.top() > 0 else 0
y1 = d.bottom() if d.bottom() > 0 else 0
x2 = d.left() if d.left() > 0 else 0
y2 = d.right() if d.right() > 0 else 0

face = img[x1:y1,x2:y2]
# 调整图片的对比度与亮度, 对比度与亮度值都取随机数,这样能增加样本的多样性
face = relight(face, random.uniform(0.5, 1.5), random.randint(-50, 50))

face = cv2.resize(face, (size,size))

cv2.imshow( 'image' , face)

cv2.imwrite(output_dir+ '/' +str(index)+ '.jpg' , face)

index += 1
key = cv2.waitKey(30) & 0xff
if key == 27:
break
else :
print ( 'Finished!' )
# 释放摄像头 release camera
camera.release()
# 删除建立的窗口 delete all the windows
cv2.destroyAllWindows()
break

运行效果:

图片
图片

2.2 分析每张人脸的特征值并存入csv文件

根据抓取的图片和人脸识别模型->训练得到的20个的68个特征数据集以及1个平均特征值存入csv文件

“每张图片的68个特征数据集可以不用存取,他们只是中间量,计算平均值以后就可以抛弃了,这里把他们输出出来只是为了方便学习。

代码:

# 从人脸图像文件中提取人脸特征存入 CSV  
# Features extraction from images and save into features_all.csv  

# return_128d_features()          获取某张图像的128D特征  
# compute_the_mean()              计算128D特征均值  

from cv2 import cv2 as cv2  
import os  
import dlib  
from skimage import io  
import csv  
import numpy as np  

# 要读取人脸图像文件的路径  
path_images_from_camera = "D:/No1WorkSpace/JupyterNotebook/Facetrainset/"

# Dlib 正向人脸检测器  
detector = dlib.get_frontal_face_detector()  

# Dlib 人脸预测器  
predictor = dlib.shape_predictor("D:/No1WorkSpace/JupyterNotebook/model/shape_predictor_68_face_landmarks.dat")  

# Dlib 人脸识别模型  
# Face recognition model, the object maps human faces into 128D vectors  
face_rec = dlib.face_recognition_model_v1("D:/No1WorkSpace/JupyterNotebook/model/dlib_face_recognition_resnet_model_v1.dat")  


# 返回单张图像的 128D 特征  
def return_128d_features(path_img):  
    img_rd = io.imread(path_img)  
    img_gray = cv2.cvtColor(img_rd, cv2.COLOR_BGR2RGB)  
    faces = detector(img_gray, 1)  

    print("%-40s %-20s" % ("检测到人脸的图像 / image with faces detected:", path_img), '\n')  

    # 因为有可能截下来的人脸再去检测,检测不出来人脸了  
    # 所以要确保是 检测到人脸的人脸图像 拿去算特征  
    if len(faces) != 0:  
        shape = predictor(img_gray, faces[0])  
        face_descriptor = face_rec.compute_face_descriptor(img_gray, shape)  
    else:  
        face_descriptor = 0  
        print("no face")  

    return face_descriptor  


# 将文件夹中照片特征提取出来, 写入 CSV  
def return_features_mean_personX(path_faces_personX):  
    features_list_personX = []  
    photos_list = os.listdir(path_faces_personX)  
    if photos_list:  
        for i in range(len(photos_list)):  
            with open("D:/No1WorkSpace/JupyterNotebook/feature/featuresGiao"+str(i)+".csv""w", newline="") as csvfile:  
                writer = csv.writer(csvfile)  
            # 调用return_128d_features()得到128d特征  
                print("%-40s %-20s" % ("正在读的人脸图像 / image to read:", path_faces_personX + "/" + photos_list[i]))  
                features_128d = return_128d_features(path_faces_personX + "/" + photos_list[i])  
                print(features_128d)  
                writer.writerow(features_128d)  
            # 遇到没有检测出人脸的图片跳过  
                if features_128d == 0:  
                    i += 1  
                else:  
                    features_list_personX.append(features_128d)  
    else:  
        print("文件夹内图像文件为空 / Warning: No images in " + path_faces_personX + '/''\n')  

    # 计算 128D 特征的均值  
    # N x 128D -> 1 x 128D  
    if






请到「今天看啥」查看全文