def get_face(self, img): h, w, _ = img.shape blob = cv2.dnn.blobFromImage(img, size=self.input_shape) self.net.setInput(blob) # output_names = ['loc', 'conf'] # loc, conf = self.net.forward(output_names) loc, conf = self.net.forward(self.net.getUnconnectedOutLayersNames()) # Decode bboxes and landmarks pb = PriorBox(input_shape=self.input_shape, output_shape=(w, h)) dets = pb.decode(np.squeeze(loc, axis=0), np.squeeze(conf, axis=0)) # Ignore low scores idx = np.where(dets[:, -1] > self.conf_thresh)[0] dets = dets[idx] # NMS if dets.shape[0] > 0: dets = nms(dets, self.nms_thresh) faces = dets[:self.keep_top_k, :] # Draw boudning boxes and landmarks on the original image boxs, face_rois = [], [] for i in range(faces.shape[0]): # score = faces[i,-1] box = (faces[i, :4]).astype(np.int32).tolist() face_roi = img[box[1]:box[3], box[0]:box[2]] landmark = faces[i, 4:14].reshape((5, 2)) if self.align: face_roi = align_process(img, faces[i, :4], landmark, (224, 224)) box.extend(landmark.astype(np.int32).ravel().tolist()) boxs.append(tuple(box)) face_rois.append(face_roi) return boxs, face_rois else: return [], []
def detect(self, img): h, w, _ = img.shape blob = cv2.dnn.blobFromImage(img, size=self.input_shape) self.net.setInput(blob) # output_names = ['loc', 'conf'] # loc, conf = self.net.forward(output_names) loc, conf = self.net.forward(self.net.getUnconnectedOutLayersNames()) # Decode bboxes and landmarks pb = PriorBox(input_shape=self.input_shape, output_shape=(w, h)) dets = pb.decode(np.squeeze(loc, axis=0), np.squeeze(conf, axis=0)) # Ignore low scores idx = np.where(dets[:, -1] > self.conf_thresh)[0] dets = dets[idx] # NMS if dets.shape[0] > 0: dets = nms(dets, self.nms_thresh) faces = dets[:self.keep_top_k, :] # Draw boudning boxes and landmarks on the original image drawimg = img.copy() for i in range(faces.shape[0]): # score = faces[i,-1] x1, y1, x2, y2 = (faces[i, :4]).astype(np.int32) cv2.rectangle(drawimg, (x1, y1), (x2, y2), (0, 0, 255), thickness=2) face_roi = img[y1:y2, x1:x2] # landmark = faces[i, 4:14].reshape((5, 2)) # landmark = landmark.astype(np.int32) # for j in range(5): # cv2.circle(drawimg, (landmark[j, 0], landmark[j, 1]), 2, (0, 255, 0), thickness=-1) # # cv2.putText(drawimg, str(j), (landmark[j, 0], landmark[j, 1] + 12), cv2.FONT_HERSHEY_DUPLEX, 1, (0, 0, 255)) blob = cv2.dnn.blobFromImage(face_roi, scalefactor=1 / 255.0, size=self.input_size, swapRB=True) self.pfld.setInput(blob) _, landmarks = self.pfld.forward(['output', 'landmarks']) pre_landmark = landmarks[0] pre_landmark = pre_landmark.reshape( -1, 2) * [face_roi.shape[1], face_roi.shape[0]] # np.save('pfld_mtcnn/pfld_dnn_output.npy', pre_landmark) for (x, y) in pre_landmark.astype(np.int32): cv2.circle(drawimg, (x1 + x, y1 + y), 2, (0, 255, 0), thickness=-1) return drawimg else: print('No faces found.') return img
def detect(self, img): h, w, _ = img.shape blob = cv2.dnn.blobFromImage(img, size=self.input_shape) self.net.setInput(blob) # output_names = ['loc', 'conf'] # loc, conf = self.net.forward(output_names) loc, conf = self.net.forward(self.net.getUnconnectedOutLayersNames()) # Decode bboxes and landmarks pb = PriorBox(input_shape=self.input_shape, output_shape=(w, h)) dets = pb.decode(np.squeeze(loc, axis=0), np.squeeze(conf, axis=0)) # Ignore low scores idx = np.where(dets[:, -1] > self.conf_thresh)[0] dets = dets[idx] # NMS if dets.shape[0] > 0: dets = nms(dets, self.nms_thresh) faces = dets[:self.keep_top_k, :] # Draw boudning boxes and landmarks on the original image drawimg, face_rois = img.copy(), [] for i in range(faces.shape[0]): # score = faces[i,-1] x1, y1, x2, y2 = (faces[i, :4]).astype(np.int32) cv2.rectangle(drawimg, (x1, y1), (x2, y2), (0, 0, 255), thickness=2) face_roi = img[y1:y2, x1:x2] landmark = faces[i, 4:14].reshape((5, 2)) if self.align: face_roi = align_process(img, faces[i, :4], landmark, (224, 224)) landmark = landmark.astype(np.int32) for j in range(5): cv2.circle(drawimg, (landmark[j, 0], landmark[j, 1]), 2, (0, 255, 0), thickness=-1) # cv2.putText(drawimg, str(j), (landmark[j, 0], landmark[j, 1] + 12), cv2.FONT_HERSHEY_DUPLEX, 1, (0, 0, 255)) face_rois.append(face_roi) return drawimg, face_rois else: print('No faces found.') return img, []