人脸识别,这个看似高大尚的问题,其实没有那么难。
一个是使用face_recognition
一个是使用OpenCV

import face_recognition  #导入库,这个库依赖dlib,安装dilib有些麻烦,建议安装whl文件,注意对应Python版本
face_image1 = face_recognition.load_image_file(r'F:\PycharmProjects.jpg')  #加载图片
face_encodings1 = face_recognition.face_encodings(face_image1)      #检测人脸并做标记
face_image2 = face_recognition.load_image_file(r'F:\PycharmProjects.jpg')
face_encodings2 = face_recognition.face_encodings(face_image2)
face1 = face_encodings1[0]
face2 = face_encodings2[0]
result = face_recognition.face_distance([face1],face2)  #计算距离
print(result)
xiangsi = face_recognition.compare_faces([face1],face2,tolerance=0.4)  #判断两个人脸的是否为一个人
print(xiangsi)
import cv2  #导入OpenCV
img = cv2.imread('12.jpg') #读取图片文件
face = cv2.CascadeClassifier("haarcascade_frontalface_alt.xml")  #加载人脸模型
gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)  #转化为灰度文件
faces = face.detectMultiScale(gray)    #检测人脸
for (x,y,w,h) in faces:
    cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)  #绘制矩形框左上角右下角坐标
cv2.namedWindow('jia')   #
cv2.imshow('jiahaifa',img) #窗口显示
cv2.waitKey(0)     #等待键盘输入
cv2.destroyAllWindows()  #关闭销毁各种窗口

我还是新手,有些问题可能没有写清楚,完善参考了一些代码,记不得从哪里学来的,如有冒犯请见谅。对于人脸识别如有问题可以加q36704532

本文地址:https://blog.csdn.net/weixin_44581236/article/details/112600699