def get_face(self, srcimg, threshold=0.5):
        height, width = srcimg.shape[:2]
        self.img_h_new, self.img_w_new, self.scale_h, self.scale_w = self.transform(
            height, width)
        dets, lms = self.inference_opencv(srcimg, threshold)
        boxs, face_rois = [], []
        for i in range(dets.shape[0]):
            # boxes, score = dets[i, :4], dets[i, 4]
            box = [
                int(dets[i, 0]),
                int(dets[i, 1]),
                int(dets[i, 2]),
                int(dets[i, 3])
            ]
            face_roi = srcimg[box[1]:box[3], box[0]:box[2]]

            if len(lms) > 0:
                lm = lms[i, :]
                if self.align:
                    face_roi = align_process(srcimg, np.array(box),
                                             np.array(lm).reshape(-1, 2),
                                             (224, 224))
                box.extend(list(map(int, lm.tolist())))
            boxs.append(tuple(box))
            face_rois.append(face_roi)
        return boxs, face_rois
 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, srcimg, threshold=0.5):
        height, width = srcimg.shape[:2]
        self.img_h_new, self.img_w_new, self.scale_h, self.scale_w = self.transform(
            height, width)
        dets, lms = self.inference_opencv(srcimg, threshold)

        drawimg, face_rois = srcimg.copy(), []
        for i in range(dets.shape[0]):
            boxes, score = dets[i, :4], dets[i, 4]
            cv2.rectangle(drawimg, (int(boxes[0]), int(boxes[1])),
                          (int(boxes[2]), int(boxes[3])), (0, 0, 255),
                          thickness=2)
            face_roi = srcimg[int(boxes[1]):int(boxes[3]),
                              int(boxes[0]):int(boxes[2])]

            if len(lms) > 0:
                lm = lms[
                    i, :]  ###landmarks: numpy array, n x 10 (x1, y1 ... x5,y5)
                for j in range(0, 5):
                    cv2.circle(drawimg, (int(lm[j * 2]), int(lm[j * 2 + 1])),
                               2, (0, 255, 0),
                               thickness=-1)
                    # cv2.putText(drawimg, str(j), (int(lm[j * 2]), int(lm[j * 2 + 1]) + 12), cv2.FONT_HERSHEY_DUPLEX, 1,(0, 0, 255))
                if self.align:
                    face_roi = align_process(srcimg, np.array(boxes),
                                             np.array(lm).reshape(-1, 2),
                                             (224, 224))
            face_rois.append(face_roi)
        return drawimg, face_rois
Exemple #4
0
 def detect(self, srcimg):
     bounding_boxes, landmarks = self.mtcnn.detect(
         srcimg
     )  ###landmarks: numpy array, n x 10 (x1, x2 ... x5, y1, y2 ..y5)
     if len(bounding_boxes) == 0:
         return srcimg, []
     drawimg, face_rois = srcimg.copy(), []
     for i in range(bounding_boxes.shape[0]):
         # score = bounding_boxes[i,4]
         x1, y1, x2, y2 = (bounding_boxes[i, :4]).astype(np.int32)
         cv2.rectangle(drawimg, (x1, y1), (x2, y2), (0, 0, 255),
                       thickness=2)
         face_roi = srcimg[y1:y2, x1:x2]
         landmark = landmarks[i, :].reshape((2, 5)).T
         if self.align:
             face_roi = align_process(srcimg, bounding_boxes[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
Exemple #5
0
    def get_face(self, srcimg):
        ori_h, ori_w, _ = srcimg.shape
        LONG_SIDE = self.long_side
        if self.long_side == -1:
            max_size = max(ori_w, ori_h)
            LONG_SIDE = max(32, max_size - max_size % 32)

        if ori_h > ori_w:
            scale_h = LONG_SIDE / ori_h
            tar_w = int(ori_w * scale_h)
            tar_w = tar_w - tar_w % 32
            tar_w = max(32, tar_w)
            tar_h = LONG_SIDE
        else:
            scale_w = LONG_SIDE / ori_w
            tar_h = int(ori_h * scale_w)
            tar_h = tar_h - tar_h % 32
            tar_h = max(32, tar_h)
            tar_w = LONG_SIDE

        scale_w = tar_w * 1.0 / ori_w
        scale_h = tar_h * 1.0 / ori_h

        image = cv2.resize(srcimg, (tar_w, tar_h))
        image = image[..., ::-1]
        image = image.astype(np.float64)
        # image = (image - hyp['mean']) / hyp['std']
        image /= 255.0
        image = np.transpose(image, [2, 0, 1])
        image = np.expand_dims(image, axis=0)
        with torch.no_grad():
            image = torch.from_numpy(image)
            image = image.to(self.device).float()
            pred = self.net(image)[0]
            pred = non_max_suppression(pred, 0.25, 0.35, multi_label=False, classes=0, agnostic=False, land=True,
                                       point_num=self.point_num)
            try:
                det = pred[0].cpu().detach().numpy()
                srcimg = srcimg.astype(np.uint8)
                det[:, :4] = det[:, :4] / np.array([scale_w, scale_h] * 2)
                det[:, 5:5 + self.point_num * 2] = det[:, 5:5 + self.point_num * 2] / np.array([scale_w, scale_h] * self.point_num)
            except:
                det = []
        boxs, face_rois = [], []
        for b in det:
            b = list(map(int, b))
            del b[4]  ### delte score
            boxs.append(b)

            face_roi = srcimg[b[1]:b[3], b[0]:b[2]]
            if self.align:
                face_roi = align_process(srcimg, np.array(b[:4]), np.array(b[4:14]).reshape(-1, 2), (224, 224))
            face_rois.append(face_roi)
        return boxs, face_rois
Exemple #6
0
    def get_face(self, srcimg, threshold=0.3, nms_iou=0.3):
        image = common.pad(srcimg).astype(np.float32)
        image = ((image / 255.0 - self.mean) / self.std)
        image = image.transpose(2, 0, 1)
        with torch.no_grad():
            torch_image = torch.from_numpy(image)[None]
            torch_image = torch_image.to(self.device)

            hm, box, landmark = self.net(torch_image)
            hm_pool = F.max_pool2d(hm, 3, 1, 1)
            scores, indices = ((hm == hm_pool).float() * hm).view(
                1, -1).cpu().topk(1000)
            hm_height, hm_width = hm.shape[2:]

            scores = scores.squeeze()
            indices = indices.squeeze()
            ys = list((indices / hm_width).int().data.numpy())
            xs = list((indices % hm_width).int().data.numpy())
            scores = list(scores.data.numpy())
            box = box.cpu().squeeze().data.numpy()
            landmark = landmark.cpu().squeeze().data.numpy()

            stride = 4
            objs = []
            for cx, cy, score in zip(xs, ys, scores):
                if score < threshold:
                    break

                x, y, r, b = box[:, cy, cx]
                xyrb = (np.array([cx, cy, cx, cy]) + [-x, -y, r, b]) * stride
                x5y5 = landmark[:, cy, cx]
                x5y5 = (common.exp(x5y5 * 4) + ([cx] * 5 + [cy] * 5)) * stride
                box_landmark = list(zip(x5y5[:5], x5y5[5:]))
                objs.append(
                    common.BBox(0,
                                xyrb=xyrb,
                                score=score,
                                landmark=box_landmark))
            objs = self.nms(objs, iou=nms_iou)
        boxs, face_rois = [], []
        for i, obj in enumerate(objs):
            box, score, landmark = list(map(int,
                                            obj.box)), obj.score, obj.landmark
            landmark = [int(x) for t in obj.landmark for x in t]
            boxs.append(box + landmark)

            face_roi = srcimg[box[1]:box[3], box[0]:box[2]]
            if self.align:
                face_roi = align_process(srcimg, np.array(box),
                                         np.array(landmark).reshape(-1, 2),
                                         (224, 224))
            face_rois.append(face_roi)
        return boxs, face_rois
 def get_face(self, srcimg):
     bounding_boxes, landmarks = self.retinaface.detect_faces(srcimg)
     boxs, face_rois = [], []
     for i in range(bounding_boxes.shape[0]):
         # score = bounding_boxes[i,4]
         box = (bounding_boxes[i, :4]).astype(np.int32).tolist()
         face_roi = srcimg[box[1]:box[3], box[0]:box[2]]
         landmark = landmarks[i, :].reshape((2, 5)).T
         if self.align:
             face_roi = align_process(srcimg, bounding_boxes[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
 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, []
Exemple #9
0
    def detect(self, srcimg):
        ori_h, ori_w, _ = srcimg.shape
        LONG_SIDE = self.long_side
        if self.long_side == -1:
            max_size = max(ori_w, ori_h)
            LONG_SIDE = max(32, max_size - max_size % 32)

        if ori_h > ori_w:
            scale_h = LONG_SIDE / ori_h
            tar_w = int(ori_w * scale_h)
            tar_w = tar_w - tar_w % 32
            tar_w = max(32, tar_w)
            tar_h = LONG_SIDE
        else:
            scale_w = LONG_SIDE / ori_w
            tar_h = int(ori_h * scale_w)
            tar_h = tar_h - tar_h % 32
            tar_h = max(32, tar_h)
            tar_w = LONG_SIDE

        scale_w = tar_w * 1.0 / ori_w
        scale_h = tar_h * 1.0 / ori_h

        image = cv2.resize(srcimg, (tar_w, tar_h))
        image = image[..., ::-1]
        image = image.astype(np.float64)
        # image = (image - hyp['mean']) / hyp['std']
        image /= 255.0
        image = np.transpose(image, [2, 0, 1])
        image = np.expand_dims(image, axis=0)
        with torch.no_grad():
            image = torch.from_numpy(image)
            image = image.to(self.device).float()
            pred = self.net(image)[0]
            pred = non_max_suppression(pred, 0.25, 0.35, multi_label=False, classes=0, agnostic=False, land=True,
                                       point_num=self.point_num)
            try:
                det = pred[0].cpu().detach().numpy()
                srcimg = srcimg.astype(np.uint8)
                det[:, :4] = det[:, :4] / np.array([scale_w, scale_h] * 2)
                det[:, 5:5 + self.point_num * 2] = det[:, 5:5 + self.point_num * 2] / np.array([scale_w, scale_h] * self.point_num)
            except:
                det = []
        drawimg, face_rois = srcimg.copy(), []
        for b in det:
            # text = "{:.4f}".format(b[4])
            b = list(map(int, b))   ###landmarks: numpy array, n x 10 (x1, y1 ... x5,y5)
            cv2.rectangle(drawimg, (b[0], b[1]), (b[2], b[3]), (0, 0, 255), thickness=2)
            cx, cy = b[0], b[1] + 12
            # cv2.putText(drawimg, text, (cx, cy), cv2.FONT_HERSHEY_DUPLEX, 0.5, (255, 255, 255))

            cv2.circle(drawimg, (b[5], b[6]), 1, (0, 0, 255), 4)
            cv2.circle(drawimg, (b[7], b[8]), 1, (0, 255, 255), 4)
            cv2.circle(drawimg, (b[9], b[10]), 1, (255, 0, 255), 4)
            cv2.circle(drawimg, (b[11], b[12]), 1, (0, 255, 0), 4)
            cv2.circle(drawimg, (b[13], b[14]), 1, (255, 0, 0), 4)
            # for i in range(5):
            #     cv2.putText(drawimg, str(i), (b[2*i+5], b[2*i+6]+12), cv2.FONT_HERSHEY_DUPLEX, 1, (0, 0, 255))

            face_roi = srcimg[b[1]:b[3], b[0]:b[2]]
            if self.align:
                face_roi = align_process(srcimg, np.array(b[:4]), np.array(b[5:15]).reshape(-1, 2), (224,224))
            face_rois.append(face_roi)
        return drawimg, face_rois
Exemple #10
0
    def detect(self, srcimg, threshold=0.3, nms_iou=0.3):
        image = common.pad(srcimg).astype(np.float32)
        image = ((image / 255.0 - self.mean) / self.std)
        image = image.transpose(2, 0, 1)
        with torch.no_grad():
            torch_image = torch.from_numpy(image)[None]
            torch_image = torch_image.to(self.device)

            hm, box, landmark = self.net(torch_image)
            hm_pool = F.max_pool2d(hm, 3, 1, 1)
            scores, indices = ((hm == hm_pool).float() * hm).view(
                1, -1).cpu().topk(1000)
            hm_height, hm_width = hm.shape[2:]

            scores = scores.squeeze()
            indices = indices.squeeze()
            ys = list((indices / hm_width).int().data.numpy())
            xs = list((indices % hm_width).int().data.numpy())
            scores = list(scores.data.numpy())
            box = box.cpu().squeeze().data.numpy()
            landmark = landmark.cpu().squeeze().data.numpy()

            stride = 4
            objs = []
            for cx, cy, score in zip(xs, ys, scores):
                if score < threshold:
                    break

                x, y, r, b = box[:, cy, cx]
                xyrb = (np.array([cx, cy, cx, cy]) + [-x, -y, r, b]) * stride
                x5y5 = landmark[:, cy, cx]
                x5y5 = (common.exp(x5y5 * 4) + ([cx] * 5 + [cy] * 5)) * stride
                box_landmark = list(zip(x5y5[:5], x5y5[5:]))
                objs.append(
                    common.BBox(0,
                                xyrb=xyrb,
                                score=score,
                                landmark=box_landmark))
            objs = self.nms(objs, iou=nms_iou)
        drawimg, face_rois = srcimg.copy(), []
        for i, obj in enumerate(objs):
            box, score, landmark = list(map(int,
                                            obj.box)), obj.score, obj.landmark
            landmark = [int(x) for t in obj.landmark for x in t]
            # landmark = sum(obj.landmark, ())
            # landmark = list(itertools.chain.from_iterable(obj.landmark))
            cv2.rectangle(drawimg, (box[0], box[1]), (box[2], box[3]),
                          (0, 0, 255),
                          thickness=2)
            for j in range(0, 5):
                cv2.circle(drawimg, (landmark[j * 2], landmark[j * 2 + 1]),
                           2, (0, 255, 0),
                           thickness=-1)
                # cv2.putText(drawimg, str(j), (landmark[j * 2], landmark[j * 2 + 1] + 12), cv2.FONT_HERSHEY_DUPLEX, 1, (0, 0, 255))
            face_roi = srcimg[box[1]:box[3], box[0]:box[2]]
            # import imutils
            # angle = np.rad2deg(np.arctan2(landmark[3] - landmark[1], landmark[2] - landmark[0]))
            # if angle != 0 and self.align:
            #     face_roi = imutils.rotate(face_roi, angle)
            if self.align:
                face_roi = align_process(srcimg, np.array(box),
                                         np.array(landmark).reshape(-1, 2),
                                         (224, 224))
            face_rois.append(face_roi)
        return drawimg, face_rois