question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Generate colors from random color palette in draw_bounding_boxes

See original GitHub issue

🚀 The feature

The current default white color looks bad. Maybe we can generate colors from palette as we do in drawing masks?

Motivation, pitch

It should be simple tweak to _generate_color_palette to pass num_boxes and get a List of RGB values. Instead of List[Tensor]. The fix is simple and that can be passed to get RGB colors.

Alternatives

Users can anyway use color="white" to get white boxes.

Additional context

It’s simple issue and good-first. We can probably wait for some new contributor 😃 to join.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
oke-adityacommented, Oct 19, 2021

@ABD-01 I spent some time and thought a bit more.

Here is a brief pseudocode (highly untested, written on paper), this can be simplified / optimized as per needs 😃


if colors is None:
   if labels is None:
       # We have no choice to create colors equal to number of boxes
       # gen_colors is  List[Tuple[int, int, int]]

        gen_colors = genereate_color_palette(len(img_boxes))
   else:
      # One way is to generate number of colors equal to number of unique labels.
      # Or alternatively, just generate excess colors and map each label just once.
      gen_colors = generate_color_palette(len(labels)) (or probably even len(img_boxes))
      
     # Since dictionary will have only unique keys.  
      label_color_map = {}
      for label, color in zip(label, gen_colors):
          label_color_map[label] = color

# Now we have color palette we can enumerate over boxes.
for i, bbox in enumerate(img_boxes):
    if colors is None:
        if labels is None:
            # No labels so just use the generated box colors
           color = gen_colors[i]
            # We have labels, so simply make use of label_color_map
        else:
           color = label_color_map[labels[i]]     
   else:
          # Main code continues.

1reaction
oke-adityacommented, Oct 19, 2021

Yes and use the colors the generated from the palette in draw_bounding_boxes. Create colors upto num_labels.

Map each label to a color.

Since labels is a list and generated color palette is also a list. Both have the same length. (As we generated colors equal to num_labels)

Use these colors to draw boxes.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I give bounding box color according to ... - GitHub
Look at « colors » In detect.py, its randomly generated using numpy. You can define it there as a list of rgb values...
Read more >
How to create random color bounding boxes with cv2.rectangle()
To generate random BGR values you can use np.random() then insert it into cv2.rectangle() color = list(np.random.random(size=3) * 256) ...
Read more >
Random Color Generator 🎨 - Pick a Color at Random
A virtual color wheel to randomly pick a color. ➤ Random color generator which outputs hex code, RGB and HSL. Supports 16.8 million...
Read more >
How to create random color bounding boxes with cv2.rectangle()
The following line works: COLORS = (np.random.randint(0,255), np.random.randint(0,255), np.random.randint(0,255)) and then pass this to the cv2.rectangle ...
Read more >
How to make random colors in Python - Turtle? - GeeksforGeeks
The random module is used to generate random numbers. ... It ends to fill the circle with color. penup(): It will stop drawing...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found