Beispiel #1
0
    def get_instanced(self, idx):
        """
        Return a separate channel target for each instance in image

        :param idx:
        :type idx:
        :return:
        :rtype:"""
        img = numpy.array(
            Image.open(self._img_path / self.imgs[idx]).convert("RGB"))
        mask = numpy.array(Image.open(self._ped_path / self.masks[idx]))

        img = cv2_resize(img, self.image_size_T)
        mask = cv2_resize(mask, self.image_size_T, InterpolationEnum.nearest)

        obj_ids = numpy.unique(
            mask)  # instances are encoded as different colors
        obj_ids = obj_ids[1:]  # first id is the background, so remove it

        # split the color-encoded mask into a set of binary masks
        masks = mask == obj_ids[:, None, None]
        zero_mask_clone = self.zero_mask.copy()
        zero_mask_clone[:masks.shape[0]] = masks

        return (
            uint_hwc_to_chw_float_tensor(to_tensor(img, dtype=torch.uint8)),
            torch.as_tensor(zero_mask_clone, dtype=torch.uint8),
        )
Beispiel #2
0
def resize(image, width=None, height=None, inter=InterpolationEnum.area):
    # initialize the dimensions of the image to be resized and
    # grab the image size
    dim = None
    (h, w) = image.shape[:2]

    # if both the width and height are None, then return the
    # original image
    if width is None and height is None:
        return image

    # check to see if the width is None
    if width is None:
        # calculate the ratio of the height and construct the
        # dimensions
        r = height / float(h)
        dim = (int(w * r), height)

    # otherwise, the height is None
    else:
        # calculate the ratio of the width and construct the
        # dimensions
        r = width / float(w)
        dim = (width, int(h * r))

    return cv2_resize(image, dim, interpolation=inter)
Beispiel #3
0
def validate_model(model, valid_loader):
    with TorchDeviceSession(global_torch_device("cpu"), model):
        with torch.no_grad():
            with TorchCacheSession():
                with TorchEvalSession(model):
                    valid_masks = []
                    out_data = []
                    a = (256, 256)
                    tr = min(len(valid_loader.dataset) * 4, 2000)
                    probabilities = []
                    for sample_i, (data,
                                   target) in enumerate(tqdm(valid_loader)):
                        data = data.to(global_torch_device())
                        outpu, *_ = model(data)
                        for m, d, p in zip(
                                target.cpu().detach().numpy(),
                                data.cpu().detach().numpy(),
                                torch.sigmoid(outpu).cpu().detach().numpy(),
                        ):
                            out_data.append(cv2_resize(chw_to_hwc(d), a))
                            valid_masks.append(cv2_resize(m[0], a))
                            probabilities.append(cv2_resize(p[0], a))
                            sample_i += 1

                            if sample_i >= tr - 1:
                                break

                        if sample_i >= tr - 1:
                            break

                    min_a = min(3, len(out_data))
                    f, ax = pyplot.subplots(min_a, 3, figsize=(24, 12))

                    # assert len(valid_masks)>2, f'{len(valid_masks), tr}'
                    for i in range(min_a):
                        ax[0, i].imshow(out_data[i], vmin=0, vmax=1)
                        ax[0, i].set_title("Original", fontsize=14)

                        ax[1, i].imshow(valid_masks[i], vmin=0, vmax=1)
                        ax[1, i].set_title("Target", fontsize=14)

                        ax[2, i].imshow(probabilities[i], vmin=0, vmax=1)
                        ax[2, i].set_title("Prediction", fontsize=14)

                    pyplot.show()
Beispiel #4
0
    def get_binary(self, idx):
        """

:param idx:
:type idx:
:return:
:rtype:
"""
        img = numpy.array(Image.open(self._img_path / self.imgs[idx]).convert("RGB"))
        mask = numpy.array(Image.open(self._ped_path / self.masks[idx]))

        mask[mask != 0] = 1.0

        img = cv2_resize(img, self.image_size_T)
        mask = cv2_resize(mask, self.image_size_T)

        return (
            uint_hwc_to_chw_float_tensor(to_tensor(img, dtype=torch.uint8)),
            to_tensor(mask).unsqueeze(0),
        )
Beispiel #5
0
    def get_binary(self, idx):
        """
        Return a single binary channel target for all instances in image

        :param idx:
        :type idx:
        :return:
        :rtype:"""
        img = numpy.array(
            Image.open(self._img_path / self.imgs[idx]).convert("RGB"))
        mask = numpy.array(Image.open(self._ped_path / self.masks[idx]))

        mask[mask != 0] = 1.0

        img = cv2_resize(img, self.image_size_T)
        mask = cv2_resize(mask, self.image_size_T, InterpolationEnum.nearest)

        return (
            uint_hwc_to_chw_float_tensor(to_tensor(img, dtype=torch.uint8)),
            to_tensor(mask).unsqueeze(0),
        )
Beispiel #6
0
 def __getitem__(self, idx):
     image_name = self.img_ids[idx]
     masks = self.fetch_masks(image_name)
     img = cv2.imread(str(self.image_data_path / image_name))
     img = cv2_resize(img, self.image_size_T)
     img_o = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
     if self.transforms:
         augmented = self.transforms(image=img_o, mask=masks)
         img_o = augmented["image"]
         masks = augmented["mask"]
     img_o = uint_hwc_to_chw_float(img_o)
     masks = hwc_to_chw(masks)
     if self.subset == SplitEnum.testing:
         return img_o, masks, self.no_info_mask(img)
     return img_o, masks
Beispiel #7
0
    def fetch_masks(self, image_name: str):
        """
        Create mask based on df, image name and shape."""
        masks = numpy.zeros(self.response_shape, dtype=numpy.float32)
        df = self.data_frame[self.data_frame["im_id"] == image_name]

        for idx, im_name in enumerate(df["im_id"].values):
            for classidx, classid in enumerate(self.categories.values()):
                mpath = str(self.base_image_data / "train_masks_525" /
                            f"{classid}{im_name}")
                mask = cv2.imread(mpath, cv2.IMREAD_GRAYSCALE)
                if mask is None:
                    continue
                mask = cv2_resize(mask, self.image_size_T)
                masks[..., classidx] = mask

        return masks / 255.0
Beispiel #8
0
def resize_children(
    src_path: Union[Path, str],
    size: Union[Tuple[Number, Number], Number],
    dst_path: Union[Path, str] = "resized",
    *,
    from_extensions: Iterable[str] = ("jpg", "png"),
    to_extension: "str" = "jpg",
    resize_method: ResizeMethodEnum = ResizeMethodEnum.scale_crop,
) -> None:
    target_size = (size, size)
    src_path = Path(src_path)
    dst_path = Path(dst_path)
    if not dst_path.root:
        dst_path = src_path.parent / dst_path
    for ext in progress_bar(from_extensions):
        for c in progress_bar(
                src_path.rglob(f'*.{ext.rstrip("*").rstrip(".")}')):
            image = cv2.imread(str(c))
            if resize_method == resize_method.scale:
                resized = cv2_resize(image, target_size,
                                     InterpolationEnum.area)
            elif resize_method == resize_method.crop:
                center = (image.shape[0] / 2, image.shape[1] / 2)
                x = int(center[1] - target_size[0] / 2)
                y = int(center[0] - target_size[1] / 2)
                resized = image[y:y + target_size[1], x:x + target_size[0]]
            elif resize_method == resize_method.scale_crop:
                resized = resize(image, width=target_size[0])
                center = (resized.shape[0] / 2, resized.shape[1] / 2)
                x = int(center[1] - target_size[0] / 2)
                y = int(center[0] - target_size[1] / 2)
                resized = resized[y:y + target_size[1], x:x + target_size[0]]
            else:
                raise NotImplementedError

            target_folder = ensure_existence(
                dst_path.joinpath(*(c.relative_to(src_path).parent.parts)))
            cv2.imwrite(
                str((target_folder / c.name).with_suffix(
                    f'.{to_extension.rstrip("*").rstrip(".")}')),
                resized,
            )
Beispiel #9
0
def aushda():
    """ """
    detector = dlib.get_frontal_face_detector()
    predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
    face_size = (256, 256)
    upsample_num_times = 0

    print(type(detector))

    for frame_i, image in enumerate(AsyncVideoStream()):
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        faces = [cv2_resize(image.copy(), face_size)]

        for rect in detector(gray, upsample_num_times=upsample_num_times):
            faces.append(
                align_face(image, gray, rect, predictor, desired_face_size=face_size)
            )

        cv2.imshow("test", numpy.hstack(faces))

        if cv2.waitKey(1) & 0xFF == ord(
            "q"
        ):  # if the `q` key was pressed, break from the loop
            break