Пример #1
0
    def iterate(self):
        for image_id in self._get_record_names():
            if self._stop_iteration():
                # Finish iteration.
                return

            if self._should_skip(image_id):
                continue

            try:
                annotation_path = self._get_image_annotation(image_id)
                image_path = self._get_image_path(image_id)

                # Read both the image and the annotation into memory.
                annotation = read_xml(annotation_path)
                image = read_image(image_path)
            except tf.errors.NotFoundError:
                tf.logging.debug(
                    'Error reading image or annotation for "{}".'.format(
                        image_id))
                self.errors += 1
                continue

            gt_boxes = []

            for b in annotation["object"]:
                try:
                    label_id = self.classes.index(b["name"])
                except ValueError:
                    continue

                gt_boxes.append({
                    "label": label_id,
                    "xmin": b["bndbox"]["xmin"],
                    "ymin": b["bndbox"]["ymin"],
                    "xmax": b["bndbox"]["xmax"],
                    "ymax": b["bndbox"]["ymax"],
                })

            if len(gt_boxes) == 0:
                continue

            record = {
                "width": annotation["size"]["width"],
                "height": annotation["size"]["height"],
                "depth": annotation["size"]["depth"],
                "filename": annotation["filename"],
                "image_raw": image,
                "gt_boxes": gt_boxes,
            }
            self._will_add_record(record)
            self.yielded_records += 1

            yield record
    def iterate(self):
        for image_id in self._get_record_names():
            if self._stop_iteration():
                # Finish iteration.
                return

            if self._should_skip(image_id):
                continue

            try:
                annotation_path = self._get_image_annotation(image_id)
                image_path = self._get_image_path(image_id)

                # Read both the image and the annotation into memory.
                annotation = read_xml(annotation_path)
                image = read_image(image_path)
            except tf.errors.NotFoundError:
                tf.logging.debug(
                    'Error reading image or annotation for "{}".'.format(
                        image_id))
                self.errors += 1
                continue

            gt_boxes = []

            for b in annotation['object']:
                try:
                    label_id = self.classes.index(b['name'])
                except ValueError:
                    continue

                gt_boxes.append({
                    'label': label_id,
                    'xmin': b['bndbox']['xmin'],
                    'ymin': b['bndbox']['ymin'],
                    'xmax': b['bndbox']['xmax'],
                    'ymax': b['bndbox']['ymax'],
                })

            if len(gt_boxes) == 0:
                continue

            record = {
                'width': annotation['size']['width'],
                'height': annotation['size']['height'],
                'depth': annotation['size']['depth'],
                'filename': annotation['filename'],
                'image_raw': image,
                'gt_boxes': gt_boxes,
            }
            self._will_add_record(record)
            self.yielded_records += 1

            yield record
    def iterate(self):
        for image_id in self._get_record_names():
            if self.yielded_records == self.total:
                # Finish iteration based on predefined total.
                return

            if self._only_filename and image_id != self._only_filename:
                # Ignore image when using `only_filename` and it doesn't match
                continue

            try:
                annotation_path = self._get_image_annotation(image_id)
                image_path = self._get_image_path(image_id)

                # Read both the image and the annotation into memory.
                annotation = read_xml(annotation_path)
                image = read_image(image_path)
            except tf.errors.NotFoundError:
                tf.logging.debug(
                    'Error reading image or annotation for "{}".'.format(
                        image_id))
                self.errors += 1
                continue

            gt_boxes = []

            for b in annotation['object']:
                try:
                    label_id = self.classes.index(b['name'])
                except ValueError:
                    continue

                gt_boxes.append({
                    'label': label_id,
                    'xmin': b['bndbox']['xmin'],
                    'ymin': b['bndbox']['ymin'],
                    'xmax': b['bndbox']['xmax'],
                    'ymax': b['bndbox']['ymax'],
                })

            if len(gt_boxes) == 0:
                continue

            self.yielded_records += 1

            yield {
                'width': annotation['size']['width'],
                'height': annotation['size']['height'],
                'depth': annotation['size']['depth'],
                'filename': annotation['filename'],
                'image_raw': image,
                'gt_boxes': gt_boxes,
            }
Пример #4
0
    def iterate(self):
        for image_id in self._get_record_names():
            if self._stop_iteration():
                # Finish iteration.
                return

            if not self._is_valid(image_id):
                # Ignore image when using image_id is not valid.
                continue

            try:
                annotation_path = self._get_image_annotation(image_id)
                image_path = self._get_image_path(image_id)

                # Read both the image and the annotation into memory.
                annotation = read_xml(annotation_path)
                image = read_image(image_path)
            except tf.errors.NotFoundError:
                tf.logging.debug(
                    'Error reading image or annotation for "{}".'.format(
                        image_id))
                self.errors += 1
                continue

            gt_boxes = []

            for b in annotation['object']:
                try:
                    label_id = self.classes.index(b['name'])
                except ValueError:
                    continue

                gt_boxes.append({
                    'label': label_id,
                    'xmin': b['bndbox']['xmin'],
                    'ymin': b['bndbox']['ymin'],
                    'xmax': b['bndbox']['xmax'],
                    'ymax': b['bndbox']['ymax'],
                })

            if len(gt_boxes) == 0:
                continue

            self.yielded_records += 1

            yield {
                'width': annotation['size']['width'],
                'height': annotation['size']['height'],
                'depth': annotation['size']['depth'],
                'filename': annotation['filename'],
                'image_raw': image,
                'gt_boxes': gt_boxes,
            }
    def iterate(self):
        for image_id in self._get_record_names():
            if self._stop_iteration():
                return

            try:
                annotation_path = f"datasets/annotations/{image_id}.xml"
                annotation = read_xml(annotation_path)

                image = read_image(annotation["path"])
            except tf.errors.NotFoundError:
                tf.logging.debug(f"Error reading image or annotation for '{image_id}'.")
                self.errors += 1

                continue

            gt_boxes = list()

            for obj in annotation["object"]:
                try:
                    label_id = self.classes.index(obj["name"])
                except ValueError:
                    continue

                gt_boxes.append({
                    "label": label_id,
                    "xmin": obj["bndbox"]["xmin"],
                    "ymin": obj["bndbox"]["ymin"],
                    "xmax": obj["bndbox"]["xmax"],
                    "ymax": obj["bndbox"]["ymax"],
                })

            if len(gt_boxes) == 0:
                continue

            self.yielded_records += 1

            yield {
                "width": annotation["size"]["width"],
                "height": annotation["size"]["height"],
                "depth": annotation["size"]["depth"],
                "filename": annotation["filename"],
                "image_raw": image,
                "gt_boxes": gt_boxes,
            }
Пример #6
0
    def iterate(self):
        for image_id in self._get_record_names():
            if self._stop_iteration():
                return

            if self._should_skip(image_id):
                continue

            try:
                annotation_path = self._get_image_annotation(image_id)
                image_path = self._get_image_path(image_id)
                # Read both the image and the annotation into memory.
                annotation = read_xml(annotation_path)
                image = read_image(image_path)
            except tf.errors.NotFoundError:
                tf.logging.debug(
                    'Error reading image or annotation for "{}".'.format(
                        image_id))
                self.errors += 1
                continue

            objects = annotation.get("object")
            if objects is None:
                # If there's no bounding boxes, we don't want it.
                continue

            image_pil = Image.open(six.BytesIO(image))
            width = image_pil.width
            height = image_pil.height

            gt_boxes = []
            for b in annotation["object"]:
                try:
                    label_id = self.classes.index(self._wnids[b["name"]])
                except ValueError:
                    continue

                (xmin, ymin, xmax, ymax) = self._adjust_bbox(
                    xmin=int(b["bndbox"]["xmin"]),
                    ymin=int(b["bndbox"]["ymin"]),
                    xmax=int(b["bndbox"]["xmax"]),
                    ymax=int(b["bndbox"]["ymax"]),
                    old_width=int(annotation["size"]["width"]),
                    old_height=int(annotation["size"]["height"]),
                    new_width=width,
                    new_height=height,
                )

                gt_boxes.append({
                    "label": label_id,
                    "xmin": xmin,
                    "ymin": ymin,
                    "xmax": xmax,
                    "ymax": ymax,
                })

            if len(gt_boxes) == 0:
                continue

            record = {
                "width": width,
                "height": height,
                "depth": 3,
                "filename": annotation["filename"],
                "image_raw": image,
                "gt_boxes": gt_boxes,
            }

            self._will_add_record(record)
            self.yielded_records += 1

            yield record
Пример #7
0
    def image_to_example(self, classes, image_id):
        annotation_path = self.get_image_annotation(image_id)
        image_path = self.get_image_path(image_id)

        # Read both the image and the annotation into memory.
        annotation = read_xml(annotation_path)
        image = read_image(image_path)

        # TODO: consider alternatives to using Pillow here.
        image_pil = Image.open(image_path)
        width = image_pil.width
        height = image_pil.height
        image_pil.close()

        obj_vals = {
            'label': [],
            'xmin': [],
            'ymin': [],
            'xmax': [],
            'ymax': [],
        }

        objects = annotation.get('object')
        if objects is None:
            # If there's no bounding boxes, we don't want it
            return
        for b in annotation['object']:
            try:
                label_id = classes.index(self._wnids[b['name']])
            except ValueError:
                continue

            (xmin, ymin, xmax, ymax) = adjust_bbox(
                xmin=int(b['bndbox']['xmin']), ymin=int(b['bndbox']['ymin']),
                xmax=int(b['bndbox']['xmax']), ymax=int(b['bndbox']['ymax']),
                old_width=int(annotation['size']['width']),
                old_height=int(annotation['size']['height']),
                new_width=width, new_height=height
            )
            obj_vals['label'].append(to_int64(label_id))
            obj_vals['xmin'].append(to_int64(xmin))
            obj_vals['ymin'].append(to_int64(ymin))
            obj_vals['xmax'].append(to_int64(xmax))
            obj_vals['ymax'].append(to_int64(ymax))

        if len(obj_vals['label']) == 0:
            # No bounding box matches the available classes.
            return

        object_feature_lists = {
            'label': tf.train.FeatureList(feature=obj_vals['label']),
            'xmin': tf.train.FeatureList(feature=obj_vals['xmin']),
            'ymin': tf.train.FeatureList(feature=obj_vals['ymin']),
            'xmax': tf.train.FeatureList(feature=obj_vals['xmax']),
            'ymax': tf.train.FeatureList(feature=obj_vals['ymax']),
        }

        object_features = tf.train.FeatureLists(
            feature_list=object_feature_lists
        )

        sample = {
            'width': to_int64(width),
            'height': to_int64(height),
            'depth': to_int64(3),
            'filename': to_string(annotation['filename']),
            'image_raw': to_bytes(image),
        }

        # Now build an `Example` protobuf object and save with the writer.
        context = tf.train.Features(feature=sample)
        example = tf.train.SequenceExample(
            feature_lists=object_features, context=context
        )

        return example
Пример #8
0
    def iterate(self):
        for image_id in self._get_record_names():
            if self._stop_iteration():
                return

            if not self._is_valid(image_id):
                continue

            try:
                annotation_path = self._get_image_annotation(image_id)
                image_path = self._get_image_path(image_id)
                # Read both the image and the annotation into memory.
                annotation = read_xml(annotation_path)
                image = read_image(image_path)
            except tf.errors.NotFoundError:
                tf.logging.debug(
                    'Error reading image or annotation for "{}".'.format(
                        image_id))
                self.errors += 1
                continue

            objects = annotation.get('object')
            if objects is None:
                # If there's no bounding boxes, we don't want it.
                continue

            image_pil = Image.open(six.BytesIO(image))
            width = image_pil.width
            height = image_pil.height

            gt_boxes = []
            for b in annotation['object']:
                try:
                    label_id = self.classes.index(self._wnids[b['name']])
                except ValueError:
                    continue

                (xmin, ymin, xmax, ymax) = self._adjust_bbox(
                    xmin=int(b['bndbox']['xmin']),
                    ymin=int(b['bndbox']['ymin']),
                    xmax=int(b['bndbox']['xmax']),
                    ymax=int(b['bndbox']['ymax']),
                    old_width=int(annotation['size']['width']),
                    old_height=int(annotation['size']['height']),
                    new_width=width, new_height=height
                )

                gt_boxes.append({
                    'label': label_id,
                    'xmin': xmin,
                    'ymin': ymin,
                    'xmax': xmax,
                    'ymax': ymax,
                })

            if len(gt_boxes) == 0:
                continue

            self.yielded_records += 1

            yield {
                'width': width,
                'height': height,
                'depth': 3,
                'filename': annotation['filename'],
                'image_raw': image,
                'gt_boxes': gt_boxes,
            }
Пример #9
0
    def image_to_example(self, classes, image_id):
        annotation_path = self.get_image_annotation(image_id)
        image_path = self.get_image_path(image_id)

        # Read both the image and the annotation into memory.
        annotation = read_xml(annotation_path)
        image = read_image(image_path)

        object_features_values = {
            'label': [],
            'xmin': [],
            'ymin': [],
            'xmax': [],
            'ymax': [],
        }

        for b in annotation['object']:
            try:
                label_id = classes.index(b['name'])
            except ValueError:
                continue

            object_features_values['label'].append(to_int64(label_id))
            object_features_values['xmin'].append(to_int64(
                b['bndbox']['xmin']))
            object_features_values['ymin'].append(to_int64(
                b['bndbox']['ymin']))
            object_features_values['xmax'].append(to_int64(
                b['bndbox']['xmax']))
            object_features_values['ymax'].append(to_int64(
                b['bndbox']['ymax']))

        if len(object_features_values['label']) == 0:
            # No bounding box matches the available classes.
            return

        object_feature_lists = {
            'label':
            tf.train.FeatureList(feature=object_features_values['label']),
            'xmin':
            tf.train.FeatureList(feature=object_features_values['xmin']),
            'ymin':
            tf.train.FeatureList(feature=object_features_values['ymin']),
            'xmax':
            tf.train.FeatureList(feature=object_features_values['xmax']),
            'ymax':
            tf.train.FeatureList(feature=object_features_values['ymax']),
        }

        object_features = tf.train.FeatureLists(
            feature_list=object_feature_lists)

        sample = {
            'width': to_int64(int(annotation['size']['width'])),
            'height': to_int64(int(annotation['size']['height'])),
            'depth': to_int64(int(annotation['size']['depth'])),
            'filename': to_string(annotation['filename']),
            'image_raw': to_bytes(image),
        }

        # Now build an `Example` protobuf object and save with the writer.
        context = tf.train.Features(feature=sample)
        example = tf.train.SequenceExample(feature_lists=object_features,
                                           context=context)

        return example