Example #1
0
 def parse_sample(sample):
     video_path, video_metadata = sample
     return fos.Sample(
         filepath=video_path,
         metadata=video_metadata,
         tags=tags,
     )
Example #2
0
 def parse_sample(sample):
     image_path, image_metadata = sample
     return fos.Sample(
         filepath=image_path,
         metadata=image_metadata,
         tags=tags,
     )
Example #3
0
            def parse_sample(sample):
                video_path, video_metadata, frames = sample
                sample = fos.Sample(
                    filepath=video_path, metadata=video_metadata, tags=tags,
                )

                if frames is not None:
                    sample.frames.update(frames)

                return sample
Example #4
0
            def parse_sample(sample):
                image_path, image_metadata, label = sample
                sample = fos.Sample(
                    filepath=image_path, metadata=image_metadata, tags=tags,
                )

                if label is not None:
                    sample[label_field] = label

                return sample
Example #5
0
    def parse_sample(sample):
        sample_parser.with_sample(sample)

        image_path = sample_parser.get_image_path()

        if sample_parser.has_image_metadata:
            metadata = sample_parser.get_image_metadata()
        else:
            metadata = None

        return fos.Sample(filepath=image_path, metadata=metadata, tags=tags)
Example #6
0
            def parse_sample(sample):
                image_path, image_metadata, label = sample
                sample = fos.Sample(
                    filepath=image_path, metadata=image_metadata, tags=tags,
                )

                if isinstance(label, dict):
                    sample.update_fields(label)
                elif label is not None:
                    sample[label_field] = label

                return sample
Example #7
0
    def parse_sample(sample):
        sample_parser.with_sample(sample)

        video_path = sample_parser.get_video_path()

        if sample_parser.has_video_metadata:
            metadata = sample_parser.get_video_metadata()
        else:
            metadata = None

        sample = fos.Sample(filepath=video_path, metadata=metadata, tags=tags)

        frames = sample_parser.get_frame_labels()

        if frames is not None:
            sample.frames.update(frames)

        return sample
Example #8
0
    def parse_sample(sample):
        sample_parser.with_sample(sample)

        image_path = sample_parser.get_image_path()

        if sample_parser.has_image_metadata:
            metadata = sample_parser.get_image_metadata()
        else:
            metadata = None

        label = sample_parser.get_label()

        sample = fos.Sample(filepath=image_path, metadata=metadata, tags=tags)

        if isinstance(label, dict):
            sample.update_fields(label)
        elif label is not None:
            sample[label_field] = label

        return sample
Example #9
0
def load_open_images_dataset(
    dataset_name,
    images_dir,
    bounding_boxes_path=None,
    image_labels_path=None,
    predictions_path=None,
    prediction_field_name="predicted_detections",
    class_descriptions_path=None,
    load_images_with_preds=False,
    max_num_images=-1,
):
    """Loads an Open Images format dataset into FiftyOne.

    **Note** If this takes a long time it is highly recommended to save the
    dataset via:

        dataset.persistent = True

    such that this function only needs to be called once!

    Args:
        dataset_name: the name of the dataset to create in FiftyOne.
        images_dir: directory where images are stored. Images should be in
            <open-images-id>.jpg format
        bounding_boxes_path: path to the expanded-hierarchy annotation bounding
            boxes CSV
        image_labels_path: path to the expanded-hierarchy annotation image
            labels CSV
        predictions_path: path to the predicted bounding boxes CSV
        prediction_field_name: the name of the field to save the predictions
            under. Useful if other predictions may be added later
        class_descriptions_path: optional metadata file. if provided, the
            MID labels are mapped to descriptive labels
        load_images_with_preds: if True, skip any images that do not have
            predictions

    Returns:
        a :class:`fiftyone.core.dataset.Dataset` instance
    """
    # pylint: disable=unsubscriptable-object
    # read data from disk
    all_location_annotations = (pd.read_csv(bounding_boxes_path)
                                if bounding_boxes_path else None)
    all_label_annotations = (pd.read_csv(image_labels_path)
                             if image_labels_path else None)
    if predictions_path:
        all_predictions = pd.read_csv(predictions_path)
        all_predictions.rename(columns={"Score": "Confidence"}, inplace=True)
    else:
        all_predictions = None
    class_descriptions = (pd.read_csv(
        class_descriptions_path, header=None, index_col=0)
                          if class_descriptions_path else None)

    # map label MID to descriptive label
    if class_descriptions is not None:
        for df in [
                all_location_annotations,
                all_label_annotations,
                all_predictions,
        ]:
            if df is None:
                continue

            temp = class_descriptions.loc[df["LabelName"], 1]
            temp.index = df.index
            df["LabelName"] = temp

    if load_images_with_preds:
        img_paths = [
            os.path.join(images_dir, image_id + ".jpg")
            for image_id in set(all_predictions["ImageID"])
        ]
    else:
        img_paths = glob.glob(os.path.join(images_dir, "*.jpg"))

    if max_num_images != -1:
        img_paths = img_paths[:max_num_images]

    print("Parsing CSV labels...")
    _samples = []
    with fou.ProgressBar(img_paths) as pb:
        for image_path in pb(img_paths):
            image_id = os.path.splitext(os.path.basename(image_path))[0]

            kwargs = {"filepath": image_path, OPEN_IMAGES_ID: image_id}

            # parse ground truth image labels
            if all_label_annotations is not None:
                cur_lab_anns = all_label_annotations.query("ImageID == '%s'" %
                                                           image_id)
                if not cur_lab_anns.empty:
                    kwargs[GT_IMAGE_LABELS] = df2classifications(cur_lab_anns)

            # parse ground truth bounding boxes
            if all_location_annotations is not None:
                cur_loc_anns = all_location_annotations.query(
                    "ImageID == '%s'" % image_id)
                if not cur_loc_anns.empty:
                    kwargs[GT_DETECTIONS] = df2detections(cur_loc_anns)

            # parse prediction bounding boxes
            if all_predictions is not None:
                cur_preds = all_predictions.query("ImageID == '%s'" % image_id)
                if not cur_preds.empty:
                    kwargs[prediction_field_name] = df2detections(cur_preds)

            _samples.append(fos.Sample(**kwargs))

    print("Creating FiftyOne Dataset...")
    dataset = fod.Dataset(dataset_name)
    dataset.add_samples(_samples)

    return dataset