人脸识别face_recognition代码理解

参考

https://blog.csdn.net/Nirvana_6174/article/details/81392308

 

 

首先调用load_image_file加载人脸库图片

a_images = face_recognition.load_image_file(file_str)

然后face_encodings对图片对象a_images进行编码并返回数组0位置编码结果;

a_face_encoding = face_recognition.face_encodings(a_images)[0]

所有编码结果放到known_face_encodings数组里。

known_face_encodings.append(a_face_encoding)

 

opencv摄像头处理

    # 读取摄像头画面
    ret, frame = cam.read()

    # 改变摄像头图像的大小,图像小,所做的计算就少。frame原大小,缩小后small_frame 
    small_frame = cv2.resize(frame, (0, 0), fx=0.33, fy=0.33)

    # opencv的图像是BGR格式的,而我们需要是的RGB格式的,因此需要进行一个转换。
    rgb_small_frame = small_frame[:, :, ::-1]

        # 根据encoding来判断是不是同一个人,是就输出true,不是为flase
        face_locations = face_recognition.face_locations(rgb_small_frame)

        拿到当前帧的face编码
        face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)

for face_encoding in face_encodings:
    # 默认为unknown,当前人脸跟人脸库中的进行对比。并返回比对结果matches
    matches = face_recognition.compare_faces(known_face_encodings, face_encoding, tolerance=0.48)
    # 阈值太低容易造成无法成功识别人脸,太高容易造成人脸识别混淆 默认阈值tolerance为0.6
    # print(matches)
    name = "Unknown"
# 将捕捉到的人脸显示出来
for (top, right, bottom, left), name in zip(face_locations, face_names):
    # Scale back up face locations since the frame we detected in was scaled to 1/4 size
    # 由于我们检测到的帧被缩放到1/4大小,所以要缩小面位置
    top *= 3
    right *= 3
    bottom *= 3
    left *= 3
# 矩形框
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)

# 引入ft2中的字体
# 加上标签
cv2.rectangle(frame, (left, bottom - 20), (right, bottom), (0, 0, 255), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left + 6, bottom - 6), font, 0.8, (255, 255, 255), 1)

显示打上标签和字的人脸

cv2.imshow('monitor', frame)

 

函数说明

函数face_locations

face_locations(img, number_of_times_to_upsample=1, model='hog')

给定一个图像,返回图像中每个人脸的面部特征位置(眼睛、鼻子等)

参数: 
img:一个image(numpy array类型)

number_of_times_to_upsample:从images的样本中查找多少次人脸,该参数的值越高的话越能发现更小的人脸

model:使用哪种人脸检测模型。“hog” 准确率不高,但是在CPUs上运行更快,“cnn” 更准确更深度(且GPU/CUDA加速,如果有GPU支持的话),默认是“hog”

返回值:

一个元组列表,列表中的每个元组包含人脸的位置(top, right, bottom, left)

https://blog.csdn.net/u012819339/article/details/81671574

https://blog.csdn.net/qq_37950540/article/details/88907027

void rectangle(Mat& img, Point pt1,Point pt2,const Scalar& color, int thickness=1, int lineType=8, int shift=0) 
参数介绍: 
要进行绘制的目标图像 
矩形的一个顶点 
矩阵对角线上的另一个顶点 
线条的颜色(RGB)或亮度(灰度图) 
组成矩形线条的粗细程度。取负值时,函数绘制填充了色彩的矩形。 
线条的类型。 
坐标点的小数点位数。 
Rect()定义一个矩形数据,rectangle将其画出来。
原文:https://blog.csdn.net/Du_Shuang/article/details/77905698 

 

原型 void putText( Mat& img, const string& text, Point org, int fontFace,double fontScale,  Scalar color, int thickness=1, int lineType=8 );

参数1:, Mat& img,待写字的图片,我们写在img图上

参数2:,const string& text,待写入的字,我们下面写入Hello

参数3:, Point org, 第一个字符左下角坐标,我们设定在图片的Point(50,60)坐标。表示x = 50,y = 60。

参数4:,int fontFace,字体类型,FONT_HERSHEY_SIMPLEX ,FONT_HERSHEY_PLAIN ,FONT_HERSHEY_DUPLEX 等等等。

参数5:,double fontScale,字体大小,我们设置为2号

参数6:,Scalar color,字体颜色,颜色用Scalar()表示,不懂得去百度。

参数7:, int thickness,字体粗细,我们下面代码使用的是4号

参数8:, int lineType,线型,我们使用默认值8.

原文:https://blog.csdn.net/fanjiule/article/details/81606874 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值