def test_get_face_rate(self, ):
        # Arrange
        max_process = 4
        batch_data: BatchData = batch_data_loader_service.load_batch(0)

        logger.info("Got batch data.")

        stopwatch = Stopwatch()
        stopwatch.start()
        num_videos = 1  # batch_data.size()
        for i in range(num_videos):
            logger.info(f"Getting {i}th video.")
            vid_path = batch_data.get_candidate_file_path(i)

            fil = video_service.process_all_video_frames(
                vid_path, face_recog_service.get_face_data, max_process)
        stopwatch.stop()

        for fi in fil:
            landmarks = fi['face_info_landmarks']
            frame_index = fi['frame_index']
            file_path = fi['file_path']
            for ndx, land in enumerate(landmarks):
                face_image = land['face_image']
                image_service.show_image(face_image, 'test')

                par_path = os.path.join(config.TRASH_PATH, f"{file_path.stem}")
                os.makedirs(par_path, exist_ok=True)

                image_path = os.path.join(par_path, f"{frame_index}_{ndx}.png")
                logger.info(f"Writing to {image_path}.")
                image_converted = cv2.cvtColor(face_image, cv2.COLOR_BGR2RGB)
                cv2.imwrite(image_path, image_converted)

        logger.info(stopwatch)
Пример #2
0
    def test_get_swatch_pair(self):
        # Arrange
        vid_path = Path(
            'D:\\Kaggle Downloads\\deepfake-detection-challenge\\train\\dfdc_train_part_3\\dnrpknwija.mp4'
        )
        image, _, _ = video_service.get_single_image_from_vid(vid_path, 0)
        image_service.show_image(image, 'Original Fake')
        redis_service = RedisService()
        red = (255, 0, 0)
        green = (0, 255, 0)

        l2 = (397, 812)
        r2 = (560, 976)

        # 397,812; 560,976

        face_finder = FaceFinder.load(redis_service, vid_path)
        # Act
        for i in range(25):
            swatch_fake, swatch_real, x_rnd, y_rnd = find_diff_random.get_swatch_pair(
                face_finder, 0, image, image)

            l1 = (x_rnd, y_rnd)
            r1 = (x_rnd + 244, y_rnd + 244)
            image_corner = cv2.rectangle(image,
                                         pt1=l1,
                                         pt2=r1,
                                         color=red,
                                         thickness=3)
            image_corner = cv2.rectangle(image_corner,
                                         pt1=l2,
                                         pt2=r2,
                                         color=green,
                                         thickness=3)
            image_service.show_image(image_corner, 'Original Fake with swatch')
Пример #3
0
def find_tiny_faces(root_path: Path, output_path: Path):
    expand_frame = False
    detector = MTCNN()

    files = file_service.walk(root_path)
    face_path_list = []
    for ndx, f in enumerate(files):
        if ndx > 10:
            break
        file_path = Path(f)
        filename = file_path.stem
        filename_parts = filename.split("_")
        frame_index = filename_parts[0]
        head_index = filename_parts[1]
        image = cv2.imread(f)
        output_head_path = os.path.join(str(output_path), os.path.dirname(f))

        image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        image_service.show_image(image_rgb, "test")
        # fi = detector.detect_faces(image_rgb)
        #
        # for _, face in enumerate(fi):
        #   face_path = process_mtcnn_face(face, frame_index, head_index, image, output_head_path, expand_frame)
        #   face_path_list.append(face_path)

    return face_path_list
    def test_show_frame(self):
        # Arrange
        batch_data: BatchData = batch_data_loader_service.load_batch(8)
        vid_path: Path = batch_data.get_candidate_file_path(0)
        index = 100

        # Act
        image, _, _ = video_service.get_single_image_from_vid(vid_path, index)

        show_image(image, f"Image {index}: {vid_path.name}")
Пример #5
0
def threshholding(diff_image, image_real, image_fake, max_val, thresh):
  blur = cv2.GaussianBlur(diff_image, (5, 5), 0)
  ret3, thresh = cv2.threshold(blur, thresh, max_val, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
  # thresh = cv2.adaptiveThreshold(diff_image, max_val, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
  # thresh = cv2.threshold(diff_image, thresh, max_val, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
  contours = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  contours = imutils.grab_contours(contours)
  rectangle_diffs = []
  # loop over the contours
  for c in contours:
    # compute the bounding box of the contour and then draw the
    # bounding box on both input images to represent where the two
    # images differ
    tuple_rect = cv2.boundingRect(c)
    rectangle_diffs.append(tuple_rect)
    (x, y, w, h) = tuple_rect
    cv2.rectangle(image_real, (x, y), (x + w, y + h), (0, 0, 255), 2)
    # cv2.rectangle(image_fake, (x, y), (x + w, y + h), (0, 0, 255), 2)

  image_service.show_image(image_real, "Original")
  image_service.show_image(image_fake, "Modified")
  image_service.show_image(diff_image, "Diff")
  image_service.show_image(thresh, "Thresh")

  return image_real, image_fake
Пример #6
0
    def test_blazeface_real_image(self):
        # Arrange
        # img, height, width = image_service.pick_image(2, 3, 1)
        blazeface = BlazeFace()

        batch_path = config.get_train_batch_path(2)
        # vid_filename = "ambabjrwbt.mp4"
        # vid_filename = "aimzesksew.mp4"
        vid_filename = "aejroilouc.mp4"
        vid_path = Path(batch_path, vid_filename)

        logger.info(
            f"Odim: {video_service.get_single_image_from_vid(vid_path, 13)[0].shape}"
        )

        blaze_dataset = BlazeDataSet(vid_path=vid_path, max_process=10)

        for i in range(1, 3):
            sub_frame_img = blaze_dataset.__getitem__(i)
            # image_service.show_image(sub_frame_img)

            # logger.info(f"img.shape: {img.shape}")
            height, width, _ = sub_frame_img.shape
            # return

            # offset = ((height - width) // 2)
            # img = img[:width, :]

            # logger.info(f"img.shape: {img.shape}")
            # image_service.show_image(img)
            sub_frame_image_resized = cv2.resize(
                sub_frame_img, (128, 128), interpolation=cv2.INTER_NEAREST)

            # Act
            subframe_detections = blazeface.predict_on_image(
                sub_frame_image_resized)
            logger.info(subframe_detections)

            face_obj_list = blaze_dataset.get_face_images_in_subframe(
                subframe_detections, i)

            for f in face_obj_list:
                face_image = f['image']
                xmin = f['xmin']
                ymin = f['ymin']
                logger.info(f"xmind: {xmin}; {ymin}")

                image_service.show_image(face_image)
Пример #7
0
def get_face_diffs(batch_data: BatchData, max_diffs: int = 1):
    df: DataFrame = batch_data.df_metadata

    df_fakes = df[df['label'] == 'FAKE']

    small_dir_path = config.SMALL_HEAD_OUTPUT_PATH

    diffs = []
    for ndx, row in df_fakes.iterrows():
        if len(diffs) > max_diffs:
            break
        fake_filename = row['candidate_filename']
        original_filename = row['original_filename']

        orig_dirname = Path(original_filename).stem
        fake_dirname = Path(fake_filename).stem

        orig_dir_path = os.path.join(small_dir_path, orig_dirname)
        fake_dir_path = os.path.join(small_dir_path, fake_dirname)

        if os.path.exists(orig_dir_path) and os.path.exists(fake_dir_path):
            orig_files = file_service.walk(orig_dir_path)
            orig_file_info = get_file_info(orig_files)

            for of in orig_file_info:
                file_path = of['file_path']
                frame_index = of['frame_index']
                head_index = of['head_index']

                file_to_find = os.path.join(fake_dir_path,
                                            f"{frame_index}_{head_index}.png")
                fake_path = Path(file_to_find)
                if fake_path.exists():
                    image_diffs = get_image_differences(file_path, fake_path)
                    ssim = image_diffs['ssim']
                    if ssim > 0.93:
                        image_service.show_image(image_diffs['original_image'],
                                                 f'original {ssim}')
                        image_service.show_image(image_diffs['fake_image'],
                                                 f'fake {ssim}')
                    head_diff = dict(ssim=ssim,
                                     original_path=file_path,
                                     fake_path=fake_path)
                    diffs.append(head_diff)

    return diffs
    def test_get_face(self):
        # Arrange
        # Batch 8, video 0 has fake
        batch_data: BatchData = batch_data_loader_service.load_batch(0)
        vid_path = batch_data.get_candidate_file_path(0)

        # Act
        image, _, _ = video_service.get_single_image_from_vid(vid_path, 150)

        image_service.show_image(image)

        height, width, _ = image.shape

        # Act
        face_infos = face_recog_service.get_face_infos(image, height, width)

        for fi in face_infos:
            title = f"Found {len(face_infos)} face(s)."
            face_image, _, _, _, _ = fi
            image_service.show_image(face_image, title)
            face_lines_image = face_recog_service.add_face_lines(face_image)
            image_service.show_image(face_lines_image)

        # Assert
        assert (len(face_infos) > 0)
Пример #9
0
    def test_get_video_frame_face(self):
        # Arrange
        filename = 'dnrpknwija.mp4'
        files = file_service.walk_to_path(Path(config.TRAIN_PARENT_PATH_D),
                                          filename_endswith=filename)
        assert (len(files) == 1)

        vid_path = files[0]
        logger.info(f'vid: {vid_path}')

        assert (vid_path.exists())

        image, _, _ = video_service.get_single_image_from_vid(vid_path, 0)

        # l1: 408,706; r1: 652:950 - swatch
        # l2: 397,812; r2: 560,976 - face
        red = (255, 0, 0)
        green = (0, 255, 0)

        l1 = (408, 706)
        r1 = (652, 950)

        l2 = (397, 812)
        r2 = (560, 976)

        image_rect_1 = cv2.rectangle(image,
                                     pt1=l1,
                                     pt2=r1,
                                     color=red,
                                     thickness=3)
        image_rect_1 = cv2.rectangle(image,
                                     pt1=l2,
                                     pt2=r2,
                                     color=green,
                                     thickness=3)

        image_service.show_image(image_rect_1, 'Original')
Пример #10
0
  def test_get_subframe_images(self):
    # Arrange
    batch_data = batch_data_loader_service.load_batch(0)
    bdr: BatchDataRow = batch_data.__getitem__(0)

    blazeBright = BlazeBrightSideDataSet(bdr.video_path)

    # Act
    images = blazeBright.get_subframe_images_info(0)

    # Assert
    assert(len(images) == 3)
    image_service.show_image(images[0])
    image_service.show_image(images[1])
    image_service.show_image(images[2])