Esempio n. 1
0
    def iterate(self):
        records = self._get_records()
        for image_id, annotations in records.items():
            if self._stop_iteration():
                return

            if self._should_skip(image_id):
                continue

            image_path = os.path.join(self._images_dir, image_id)
            try:
                image = read_image(image_path)
            except tf.errors.NotFoundError:
                tf.logging.warning(
                    'Image `{}` at `{}` couldn\'t be opened.'.format(
                        image_id, image_path
                    )
                )
                self.errors += 1
                continue

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

            gt_boxes = []
            for annotation in annotations:
                try:
                    label_id = self.classes.index(annotation['label'])
                except ValueError:
                    tf.logging.warning(
                        'Error finding id for image `{}`, label `{}`.'.format(
                            image_id, annotation['label']
                        )
                    )
                    continue

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

            if len(gt_boxes) == 0:
                continue

            record = {
                'width': width,
                'height': height,
                'depth': 3,
                'filename': image_id,
                'image_raw': image,
                'gt_boxes': gt_boxes,
            }
            self._will_add_record(record)
            self.yielded_records += 1

            yield record
Esempio n. 2
0
    def iterate(self):
        for image_id, image_details in self._image_to_details.items():
            filename = image_details['file_name']
            width = image_details['width']
            height = image_details['height']

            try:
                image_path = self._get_image_path(filename)
                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 = self._image_to_bboxes.get(image_id, [])
            if len(gt_boxes) == 0:
                continue

            self.yielded_records += 1

            yield {
                'width': width,
                'height': height,
                'depth': 3,
                'filename': filename,
                'image_raw': image,
                'gt_boxes': gt_boxes,
            }
    def _complete_records(self, input_queue, output_queue):
        while True:
            partial_record = input_queue.get()

            if partial_record is None:
                break

            try:
                image_id = partial_record['filename']
                image_raw = read_image(self._get_image_path(image_id))
                image = Image.open(six.BytesIO(image_raw))

                for gt_box in partial_record['gt_boxes']:
                    gt_box['xmin'] *= image.width
                    gt_box['ymin'] *= image.height
                    gt_box['xmax'] *= image.width
                    gt_box['ymax'] *= image.height

                partial_record['width'] = image.width
                partial_record['height'] = image.height
                partial_record['depth'] = 3 if image.mode == 'RGB' else 1
                partial_record['image_raw'] = image_raw

                input_queue.task_done()
                output_queue.put(partial_record)
            except Exception as e:
                tf.logging.warning(
                    'Error processing record: {}'.format(partial_record))
                tf.logging.error(e)
                self.errors += 1

        # Notify it finished
        output_queue.put(None)
Esempio n. 4
0
    def iterate(self):
        for annotation in self.annotations:
            if self._stop_iteration():
                return

            image_id = annotation['image_id']

            if self._should_skip(image_id):
                continue

            try:
                image_path = self._get_image_path(image_id)
                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

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

            gt_boxes = []
            for b in annotation[self._objects_key]:
                try:
                    label_id = self.classes.index(
                        b.get('label', self._default_class)
                    )
                except ValueError:
                    continue

                gt_boxes.append({
                    'label': label_id,
                    'xmin': b[self._x_min_key],
                    'ymin': b[self._y_min_key],
                    'xmax': b[self._x_max_key],
                    'ymax': b[self._y_max_key],
                })

            if len(gt_boxes) == 0:
                tf.logging.debug('Image "{}" has zero valid gt_boxes.'.format(
                    image_id))
                self.errors += 1
                continue

            record = {
                'width': width,
                'height': height,
                'depth': 3,
                'filename': image_id,
                'image_raw': image,
                'gt_boxes': gt_boxes,
            }
            self._will_add_record(record)
            self.yielded_records += 1

            yield record
Esempio n. 5
0
    def iterate(self):
        records = self._get_records()
        for image_id, image_data in records.items():
            if self._stop_iteration():
                return

            if not self._is_valid(image_id):
                continue

            image_path = self._get_image_path(image_id)
            if image_path is None:
                tf.logging.debug(
                    'Could not find image_path for image "{}".'.format(
                        image_id))
                self.errors += 1
                continue

            try:
                image = read_image(image_path)
            except tf.errors.NotFoundError:
                tf.logging.debug('Could not find image "{}" in "{}".'.format(
                    image_id, image_path))
                self.errors += 1
                continue

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

            gt_boxes = []
            for b in image_data:
                try:
                    label_id = self.classes.index(b['label'])
                except ValueError:
                    tf.logging.debug('Error finding id for label "{}".'.format(
                        b['label']))
                    continue

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

            if len(gt_boxes) == 0:
                continue

            self.yielded_records += 1

            yield {
                'width': width,
                'height': height,
                'depth': 3,
                'filename': image_id,
                'image_raw': image,
                'gt_boxes': gt_boxes,
            }
Esempio n. 6
0
    def iterate(self):
        for annotation in self.annotations:
            if self._stop_iteration():
                return

            image_id = annotation["image_id"]

            if self._should_skip(image_id):
                continue

            try:
                image_path = self._get_image_path(image_id)
                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

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

            gt_boxes = []
            for b in annotation[self._objects_key]:
                try:
                    label_id = self.classes.index(
                        b.get("label", self._default_class))
                except ValueError:
                    continue

                gt_boxes.append({
                    "label": label_id,
                    "xmin": b[self._x_min_key],
                    "ymin": b[self._y_min_key],
                    "xmax": b[self._x_max_key],
                    "ymax": b[self._y_max_key],
                })

            if len(gt_boxes) == 0:
                tf.logging.debug(
                    'Image "{}" has zero valid gt_boxes.'.format(image_id))
                self.errors += 1
                continue

            record = {
                "width": width,
                "height": height,
                "depth": 3,
                "filename": image_id,
                "image_raw": image,
                "gt_boxes": gt_boxes,
            }
            self._will_add_record(record)
            self.yielded_records += 1

            yield record
Esempio n. 7
0
    def iterate(self):
        records = self._get_records()
        for image_id, annotations in records.items():
            if self._stop_iteration():
                return

            if self._should_skip(image_id):
                continue

            image_path = os.path.join(self._images_dir, image_id)
            try:
                image = read_image(image_path)
            except tf.errors.NotFoundError:
                tf.logging.warning(
                    "Image `{}` at `{}` couldn't be opened.".format(
                        image_id, image_path))
                self.errors += 1
                continue

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

            gt_boxes = []
            for annotation in annotations:
                try:
                    label_id = self.classes.index(annotation["label"])
                except ValueError:
                    tf.logging.warning(
                        "Error finding id for image `{}`, label `{}`.".format(
                            image_id, annotation["label"]))
                    continue

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

            if len(gt_boxes) == 0:
                continue

            record = {
                "width": width,
                "height": height,
                "depth": 3,
                "filename": image_id,
                "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
Esempio n. 9
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.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,
            }
Esempio n. 11
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,
            }
Esempio n. 13
0
    def iterate(self):
        for image_id, image_details in self._image_to_details.items():

            if self._stop_iteration():
                return

            filename = image_details['file_name']
            width = image_details['width']
            height = image_details['height']

            gt_boxes = self._image_to_bboxes.get(image_id, [])
            if len(gt_boxes) == 0:
                continue

            if self._should_skip(image_id):
                continue

            # Read the image *after* checking whether any ground truth box is
            # present.
            try:
                image_path = self._get_image_path(filename)
                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

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

            yield record
Esempio n. 14
0
    def _complete_records(self, input_queue, output_queue):
        while not self._stop_iteration():
            partial_record = input_queue.get()

            if partial_record is None:
                input_queue.task_done()
                break

            try:
                image_id = partial_record['filename']
                image_raw = read_image(
                    self._get_image_path(image_id)
                )
                image = Image.open(six.BytesIO(image_raw))

                for gt_box in partial_record['gt_boxes']:
                    gt_box['xmin'] *= image.width
                    gt_box['ymin'] *= image.height
                    gt_box['xmax'] *= image.width
                    gt_box['ymax'] *= image.height

                partial_record['width'] = image.width
                partial_record['height'] = image.height
                partial_record['depth'] = 3 if image.mode == 'RGB' else 1
                partial_record['image_raw'] = image_raw

                output_queue.put(partial_record)
            except Exception as e:
                tf.logging.error(
                    'Error processing record: {}'.format(partial_record))
                tf.logging.error(e)
                self.errors += 1
            finally:
                input_queue.task_done()

        # Notify it finished
        output_queue.put(None)
Esempio n. 15
0
    def iterate(self):
        for image_id, image_details in self._image_to_details.items():

            if self._stop_iteration():
                return

            if not self._is_valid(image_id):
                continue

            filename = image_details['file_name']
            width = image_details['width']
            height = image_details['height']

            try:
                image_path = self._get_image_path(filename)
                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 = self._image_to_bboxes.get(image_id, [])
            if len(gt_boxes) == 0:
                continue

            self.yielded_records += 1

            yield {
                'width': width,
                'height': height,
                'depth': 3,
                'filename': filename,
                'image_raw': image,
                'gt_boxes': gt_boxes,
            }
Esempio n. 16
0
    def _complete_records(self, input_queue, output_queue):
        """
        Daemon thread that will complete queued records from `input_queue` and
        put them in `output_queue`, where they will be read and yielded by the
        main thread.

        This is the thread that will actually download the images of the
        dataset.
        """
        while True:
            try:
                partial_record = input_queue.get()

                image_id = partial_record['filename']
                image_raw = read_image(self._get_image_path(image_id))
                image = Image.open(six.BytesIO(image_raw))

                for gt_box in partial_record['gt_boxes']:
                    gt_box['xmin'] *= image.width
                    gt_box['ymin'] *= image.height
                    gt_box['xmax'] *= image.width
                    gt_box['ymax'] *= image.height

                partial_record['width'] = image.width
                partial_record['height'] = image.height
                partial_record['depth'] = 3 if image.mode == 'RGB' else 1
                partial_record['image_raw'] = image_raw

                output_queue.put(partial_record)
            except Exception as e:
                tf.logging.error(
                    'Error processing record: {}'.format(partial_record))
                tf.logging.error(e)
                self.errors += 1
            finally:
                input_queue.task_done()
Esempio n. 17
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
Esempio n. 18
0
    def iterate(self):
        records = self._get_records()
        for image_id, image_data in records.items():
            if self._stop_iteration():
                return

            if not self._is_valid(image_id):
                continue

            image_path = self._get_image_path(image_id)
            if image_path is None:
                tf.logging.debug(
                    'Could not find image_path for image "{}".'.format(
                        image_id
                    ))
                self.errors += 1
                continue

            try:
                image = read_image(image_path)
            except tf.errors.NotFoundError:
                tf.logging.debug('Could not find image "{}" in "{}".'.format(
                    image_id, image_path
                ))
                self.errors += 1
                continue

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

            gt_boxes = []
            for b in image_data:
                try:
                    label_id = self.classes.index(b['label'])
                except ValueError:
                    tf.logging.debug('Error finding id for label "{}".'.format(
                        b['label']
                    ))
                    continue

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

            if len(gt_boxes) == 0:
                continue

            self.yielded_records += 1

            yield {
                'width': width,
                'height': height,
                'depth': 3,
                'filename': image_id,
                'image_raw': image,
                'gt_boxes': gt_boxes,
            }
Esempio n. 19
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,
            }
Esempio n. 20
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
Esempio n. 21
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
Esempio n. 22
0
    def iterate(self):
        for annotation in self.annotations:
            # Checks that we don't yield more records than necessary.
            if self._stop_iteration():
                return

            image_id = annotation['image_id']

            if self._should_skip(image_id):
                continue

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

            # Parse image bytes with PIL to get width and height.
            image_pil = Image.open(six.BytesIO(image))
            img_width = image_pil.width
            img_height = image_pil.height

            gt_boxes = []
            for b in annotation['gt_boxes']:
                try:
                    label_id = self.classes.index(
                        b.get('label', self._default_class)
                    )
                except ValueError:
                    continue

                if 'height' in b and 'width' in b and 'x' in b and 'y' in b:
                    gt_boxes.append({
                        'label': label_id,
                        'xmin': b['x'] * img_width,
                        'ymin': b['y'] * img_height,
                        'xmax': b['x'] * img_width + b['width'] * img_width,
                        'ymax': b['y'] * img_height + b['height'] * img_height,
                    })
                else:
                    gt_boxes.append({
                        'label': label_id,
                        'xmin': b['x_min'] * img_width,
                        'ymin': b['y_min'] * img_height,
                        'xmax': b['x_max'] * img_width,
                        'ymax': b['y_max'] * img_height,
                    })

            if len(gt_boxes) == 0:
                tf.logging.debug('Image "{}" has zero valid gt_boxes.'.format(
                    image_id))
                self.errors += 1
                continue

            record = {
                'width': img_width,
                'height': img_height,
                'depth': 3,
                'filename': image_id,
                'image_raw': image,
                'gt_boxes': gt_boxes,
            }

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

            yield record
Esempio n. 23
0
    def iterate(self):
        for annotation in self.annotations:
            # Checks that we don't yield more records than necessary.
            if self._stop_iteration():
                return

            image_id = annotation["image_id"]

            if self._should_skip(image_id):
                continue

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

            # Parse image bytes with PIL to get width and height.
            image_pil = Image.open(six.BytesIO(image))
            img_width = image_pil.width
            img_height = image_pil.height

            gt_boxes = []
            for b in annotation["gt_boxes"]:
                try:
                    label_id = self.classes.index(
                        b.get("label", self._default_class))
                except ValueError:
                    continue

                if "height" in b and "width" in b and "x" in b and "y" in b:
                    gt_boxes.append({
                        "label":
                        label_id,
                        "xmin":
                        b["x"] * img_width,
                        "ymin":
                        b["y"] * img_height,
                        "xmax":
                        b["x"] * img_width + b["width"] * img_width,
                        "ymax":
                        b["y"] * img_height + b["height"] * img_height,
                    })
                else:
                    gt_boxes.append({
                        "label": label_id,
                        "xmin": b["x_min"] * img_width,
                        "ymin": b["y_min"] * img_height,
                        "xmax": b["x_max"] * img_width,
                        "ymax": b["y_max"] * img_height,
                    })

            if len(gt_boxes) == 0:
                tf.logging.debug(
                    'Image "{}" has zero valid gt_boxes.'.format(image_id))
                self.errors += 1
                continue

            record = {
                "width": img_width,
                "height": img_height,
                "depth": 3,
                "filename": image_id,
                "image_raw": image,
                "gt_boxes": gt_boxes,
            }

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

            yield record