Example #1
0
    def draw_one(self, frame, box, prob, landmark, count):
        """
        Draw landmarks and boxes for only one face detected
        """

        im_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

        # cv2 imwrite no need for RGB conversion
        cv2.imwrite(
            configs['frames_folder'] + "original" + str(count) + ".png", frame)

        cropped_img = extract_face(im_rgb,
                                   box,
                                   image_size=224,
                                   save_path=configs['frames_folder'] +
                                   str(count) + ".png")

        # Draw rectangle on frame
        cv2.rectangle(frame, (box[0], box[1]), (box[2], box[3]), (0, 0, 255),
                      thickness=2)

        transform = get_test_augmentations()

        # (C, H, W) -> (H, W, C)
        transformed_img = transform(
            image=np.array(cropped_img).transpose((1, 2, 0)))['image']
        # add batch dim
        transformed_img = transformed_img.unsqueeze(0)

        # transformed_img = transformed_img.to(device)
        cue = self.model.infer(transformed_img)

        # save cues
        save_image(cue,
                   configs['frames_folder'] + "cues/" + str(count) + ".png")

        score = cue.mean().cpu().item()

        # Show probability
        cv2.putText(frame, "FDet: " + "{:.3f}".format(prob),
                    (box[0], int(box[3])), cv2.FONT_HERSHEY_SIMPLEX, 1,
                    (0, 0, 255), 2, cv2.LINE_AA)
        cv2.putText(frame, "Spoof Score: " + "{:.6f}".format(score),
                    (box[0], int(box[3] + 30.0)), cv2.FONT_HERSHEY_SIMPLEX, 1,
                    (0, 0, 255), 2, cv2.LINE_AA)

        # Draw landmarks
        # cv2.circle(frame, tuple(ld[0]), 5, (0, 0, 255), -1)
        # cv2.circle(frame, tuple(ld[1]), 5, (0, 0, 255), -1)
        # cv2.circle(frame, tuple(ld[2]), 5, (0, 0, 255), -1)
        # cv2.circle(frame, tuple(ld[3]), 5, (0, 0, 255), -1)
        # cv2.circle(frame, tuple(ld[4]), 5, (0, 0, 255), -1)

        return frame
Example #2
0
    def _draw(self, frame, boxes, probs, landmarks, count):
        """
        Draw landmarks and boxes for each face detected
        """

        im_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

        # cv2 imwrite no need for RGB conversion
        cv2.imwrite(
            configs['frames_folder'] + "original" + str(count) + ".png", frame)
        for box, prob, ld in zip(boxes, probs, landmarks):
            cropped_img = extract_face(im_rgb,
                                       box,
                                       image_size=224,
                                       save_path=configs['frames_folder'] +
                                       str(count) + ".png")

            # Draw rectangle on frame
            cv2.rectangle(frame, (box[0], box[1]), (box[2], box[3]),
                          (0, 0, 255),
                          thickness=2)

            transform = get_test_augmentations()

            # (C, H, W) -> (H, W, C)
            transformed_img = transform(
                image=np.array(cropped_img).transpose((1, 2, 0)))['image']
            # add batch dim
            transformed_img = transformed_img.unsqueeze(0)

            output = self.model.classify(transformed_img)
            prediction = torch.argmax(output, dim=1).cpu().numpy()

            # Show probability
            cv2.putText(frame, "FDet: " + str(prob),
                        (box[2], int(box[3] - 30.0)), cv2.FONT_HERSHEY_SIMPLEX,
                        1, (0, 0, 255), 2, cv2.LINE_AA)
            cv2.putText(frame, str(labels_map.get(prediction[0])),
                        (box[2], box[3]), cv2.FONT_HERSHEY_SIMPLEX, 1,
                        (0, 0, 255), 2, cv2.LINE_AA)

            # Draw landmarks
            # cv2.circle(frame, tuple(ld[0]), 5, (0, 0, 255), -1)
            # cv2.circle(frame, tuple(ld[1]), 5, (0, 0, 255), -1)
            # cv2.circle(frame, tuple(ld[2]), 5, (0, 0, 255), -1)
            # cv2.circle(frame, tuple(ld[3]), 5, (0, 0, 255), -1)
            # cv2.circle(frame, tuple(ld[4]), 5, (0, 0, 255), -1)

        return frame