sulc / tfrecord-viewer

TFRecord Viewer for browsing contents of TFRecords with object detection/classification annotations.
Other
183 stars 45 forks source link

Draw label over detection boxed #14

Open smedegaard opened 3 years ago

smedegaard commented 3 years ago

First, thanks for making this. It's super useful!

The Problem

For objects that are very small compared to the total image size, the label might block the object.

I'm currently converting this dataset to TF Records: http://bird.nae-lab.org/dataset/

Suggestion

consider drawing the label over the bounding box.

A Solution

I would have sent a PR but my editor changed the formatting of the file.

Where's what I did:

# file: overlays/detection_overlay.py

...

    def draw_bboxes(self, image_bytes, bboxes):
        """Draw bounding boxes onto image.

        Args:
          image_bytes: JPEG image.
          bboxes (list of tuples): [ (label, xmin, xmax, ymin, ymax), (label, xmin, xmax, ymin, ymax) , .. ]

        Returns:
          image_bytes: JPEG image including bounding boxes.
        """
        img = Image.open(io.BytesIO(image_bytes))

        draw = ImageDraw.Draw(img)

        width, height = img.size

        for bbox in bboxes:
            label, xmin, xmax, ymin, ymax = self.bboxes_to_pixels(bbox, width, height)
            draw.rectangle([xmin, ymin, xmax, ymax], outline=self.bbox_color(label))
            # draw label over bounding box
            w, h = self.font.getsize(label)
            draw.rectangle((xmin, ymin - 2, xmin + w + 4, ymin - h - 4), fill="white")

            draw.text(
                (xmin + 2, ymin - h - 4),
                label,
                fill=self.bbox_color(label),
                font=self.font,
            )

        with io.BytesIO() as output:
            if img.mode in ("RGBA", "P"):
                img = img.convert("RGB")
            img.save(output, format="JPEG")
            output_image = output.getvalue()
        return output_image
smedegaard commented 2 years ago

made a pull request: https://github.com/sulc/tfrecord-viewer/pull/18