Download vis.py from gatilin/WeDetect-Ref-4B-Base: direct link, hf CLI and curl.
- Browser
- Download file 1.78 kB
-
https://huggingface.co/gatilin/WeDetect-Ref-4B-Base/resolve/main/vis.py
- Command line
-
hf download hf://gatilin/WeDetect-Ref-4B-Base/vis.py
-
curl -L -o vis.py https://huggingface.co/gatilin/WeDetect-Ref-4B-Base/resolve/main/vis.py
1.78 kB
| from PIL import Image, ImageDraw, ImageFont | |
| from PIL import ImageColor | |
| additional_colors = [colorname for (colorname, colorcode) in ImageColor.colormap.items()] | |
| def plot_bounding_boxes(im, bounding_boxes): | |
| """ | |
| Plots bounding boxes on an image with markers for each a name, using PIL, normalized coordinates, and different colors. | |
| Args: | |
| img_path: The path to the image file. | |
| bounding_boxes: A list of bounding boxes containing the name of the object | |
| and their positions in normalized [y1 x1 y2 x2] format. | |
| """ | |
| # Load the image | |
| img = im | |
| width, height = img.size | |
| # Create a drawing object | |
| draw = ImageDraw.Draw(img) | |
| # Define a list of colors | |
| colors = [ | |
| 'red', | |
| 'green', | |
| 'blue', | |
| 'yellow', | |
| 'orange', | |
| 'pink', | |
| 'purple', | |
| 'brown', | |
| 'gray', | |
| 'beige', | |
| 'turquoise', | |
| 'cyan', | |
| 'magenta', | |
| 'lime', | |
| 'navy', | |
| 'maroon', | |
| 'teal', | |
| 'olive', | |
| 'coral', | |
| 'lavender', | |
| 'violet', | |
| 'gold', | |
| 'silver', | |
| ] + additional_colors | |
| # Iterate over the bounding boxes | |
| for i, bounding_box in enumerate(bounding_boxes): | |
| # Select a color from the list | |
| color = colors[i % len(colors)] | |
| # Convert normalized coordinates to absolute coordinates | |
| abs_y1 = int(bounding_box[1]) | |
| abs_x1 = int(bounding_box[0]) | |
| abs_y2 = int(bounding_box[3]) | |
| abs_x2 = int(bounding_box[2]) | |
| if abs_x1 > abs_x2: | |
| abs_x1, abs_x2 = abs_x2, abs_x1 | |
| if abs_y1 > abs_y2: | |
| abs_y1, abs_y2 = abs_y2, abs_y1 | |
| # Draw the bounding box | |
| draw.rectangle( | |
| ((abs_x1, abs_y1), (abs_x2, abs_y2)), outline=color, width=4 | |
| ) | |
| # Display the image | |
| return img | |