1
$\begingroup$

I am using Torchvision in a Python script to draw bounding boxes and then crop images based on the bounding boxes drawn. The script works well, except that the bounding boxes obscure the subjects in the cropped images if more than one subject were close to one another, so I would like bounding boxes to be transparent. I have checked the reference to Torchvision's draw_bounding_boxes function, but have not been able to figure out how to accomplish this.

This is currently how I am calling the function:

box = draw_bounding_boxes(img, boxes=prediction["boxes"],
                                  labels=labels,
                                  colors=(255, 0, 0, 0),  # Red with 100% transparency
                                  width=4, font_size=30)
$\endgroup$

1 Answer 1

0
$\begingroup$

in line 245 of torchvision.utils.py

    if fill:
        draw = ImageDraw.Draw(img_to_draw, "RGBA")
    else:
        draw = ImageDraw.Draw(img_to_draw)

since fill is by default False, so the function just call pillow ImageDraw in rgb color mode instead of RGBA. But even if you set fill to be True, the function just add an 100 to the end of your color tuple so ...

Instead of monkey hacking the draw_bounding_boxes function, I think you can just use photoshop to stack original image and image with bounding boxes together, then set the opacity of image with bounding boxes to something like 60.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.