中国水利教育培训网站,各网站文风,网站建设为什么不给源代码,网页设计背景最近在配置人脸属性识别的服务#xff0c;用过faceboxes_detector#xff08;faster rcnn的包#xff09;#xff0c;也用过face_recognition的#xff0c;但是她们都没有做人脸对齐#xff0c;而且检测人脸的范围也不太一样。没有做人脸对齐的时候#xff0c;使用属性识…最近在配置人脸属性识别的服务用过faceboxes_detectorfaster rcnn的包也用过face_recognition的但是她们都没有做人脸对齐而且检测人脸的范围也不太一样。没有做人脸对齐的时候使用属性识别模型效果会较差。后面查怎么进行人脸对齐知道dlib可以做而且这个包也能做人脸检测那我就不需要再配置那么多用不到的包了只用这个工具就行。参考https://blog.csdn.net/superdont/article/details/126300274所写的因为服务资源有限不能上传太大的图像到model里所以我对图像的尺寸做了限制。这就需要最后的结果要把真实坐标还原。不过脸部的图像还是去原图里截取可以更加清晰不浪费高像素。修改如下步骤1初始化import dlib# 构造检测器detector dlib.get_frontal_face_detector()# 载入模型predictor dlib.shape_predictor(shape_predictor_68_face_landmarks.dat)# 模型链接https://pan.baidu.com/s/1Hp7IZnf2Wez_kYOYfToc_w 提取码p8ps 步骤2获取人脸框集合def face_detect(image):进行人脸检测Args:img:array输入原图opencv读取的bgr图片输出人脸检测框位置resize倍数h,w image.shape[:2]scale max(h,w)/1000 # 上服务必须限制尺寸太小的人脸可丢弃image cv2.resize(image, (int(w/scale), int(h/scale)))detections detector(image, 1)return detections, scale步骤3根据原始图像、人脸检测框位置还原原图的人脸检测框坐标位置步骤4根据原始图像、人脸关键点获取人脸对齐结果步骤5查看对齐后的人脸图像以上步骤全写在下面这个函数里def get_face_attributes(image):result []image_height, image_width, _ image.shapedetections, scale face_detect(image)#构建一个dlib.rectangles对象#因为需要把计算好的原图坐标做成rectangles格式输入dlib.get_face_chipsfaceBoxs dlib.rectangles() face_dect_list []#步骤3根据原始图像、人脸检测框位置还原原图的人脸检测框坐标位置for i in range(len(detections)):det_xmin int(detections[i].left() * scale)det_ymin int(detections[i].top() * scale)det_xmax int(detections[i].right() * scale)det_ymax int(detections[i].bottom() * scale)face_dect_list.append([det_xmin,det_ymin,det_xmax,det_ymax]) #原图坐标rectangle dlib.rectangle(det_xmin, det_ymin, det_xmax, det_ymax)faceBoxs.append(rectangle) #新的rectangles格式坐标#构造容器faces dlib.full_object_detections()#将所获取的人脸框集合逐个放入容器faces中。for faceBox in faceBoxs:faces.append(predictor(image, faceBox)) # 调用函数get_face_chips完成对人脸图像的对齐倾斜校正faces dlib.get_face_chips(img, faces, size256)i 0for face in faces:face_image np.array(face).astype(np.uint8)#可保存查看cv2.imwrite(resultstr(i).jpg,face_image)attributes_dict {}#我需要做的人脸属性检测这里不展开attributes_dict dete_attributes1(face_image,attributes_dict)attributes_dict dete_attributes2(face_image,attributes_dict)attributes_dict dete_attributes3(face_image,attributes_dict)person_dict {face_loc:face_dect_list[i],face_attributes:attributes_dict}result.append(person_dict)i1return result