Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draw label over detection boxed #14

Open
smedegaard opened this issue May 20, 2021 · 1 comment
Open

Draw label over detection boxed #14

smedegaard opened this issue May 20, 2021 · 1 comment

Comments

@smedegaard
Copy link

smedegaard commented May 20, 2021

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 smedegaard changed the title Draw detection boxed on top Draw label over detection boxed May 20, 2021
@smedegaard
Copy link
Author

made a pull request:
#18

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant