网页设计师培训在哪里网站怎样优化关键词好
最近在配置人脸属性识别的服务,用过faceboxes_detector(faster 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, size=256)i = 0for face in faces:face_image = np.array(face).astype(np.uint8)#可保存查看cv2.imwrite("result"+str(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)i+=1return result