Skip to content

Instantly share code, notes, and snippets.

@akTwelve
Last active November 23, 2023 21:01
Show Gist options
  • Save akTwelve/dc79fc8b9ae66828e7c7f648049bc42d to your computer and use it in GitHub Desktop.
Save akTwelve/dc79fc8b9ae66828e7c7f648049bc42d to your computer and use it in GitHub Desktop.
COCO Image Viewer | immersivelimit.com/tutorials/create-coco-annotations-from-scratch
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@madhavajay
Copy link

Hi @akTwelve awesome notebook.
I noticed the local file branch of this doesn't work.

        # Open the image
        if use_url:
            image_path = image['coco_url']
            response = requests.get(image_path)
            image = PILImage.open(BytesIO(response.content))
            
        else:
            # this branch does not work because it needs to base64 encode the image
            image_path = os.path.join(self.image_dir, image['file_name'])
            image = PILImage.open(image_path)

Here is the fix:

import base64

...
        else:
            image_path = os.path.join(self.image_dir, image['file_name'])
            image = PILImage.open(image_path)

            buffer = BytesIO()
            image.save(buffer, format='PNG')
            buffer.seek(0)
            
            data_uri = base64.b64encode(buffer.read()).decode('ascii')
            image_path = "data:image/png;base64,{0}".format(data_uri)

@akTwelve
Copy link
Author

Thanks for sharing this fix! I’ll take a look when I get a chance.

@akTwelve
Copy link
Author

akTwelve commented Mar 8, 2020

@madhavajay, I updated the gist with your suggested fix. Thanks again for the fix!

@zeeshanalipanhwar
Copy link

zeeshanalipanhwar commented Dec 21, 2020

Hi @akTwelve, upon visualizing my own custom dataset I noticed that the masks of some instances did not get displayed while those of some did. This was due to segmentation_points being a numpy array, so sometimes the array when stringified looked like this '241, 5, 242, ..., 244, 5, 245]'. It can be avoided by adding:
segmentation_points = list (segmentation_points) after the following line of the function display_image in the notebook.
segmentation_points = np.multiply(segmentation_points, adjusted_ratio).astype(int).

@FreshlyBrewedCode
Copy link

Thanks for sharing!
I quickly added the option to visualize keypoints. I simply added the parameter in the display_image function:

def display_image(self, image_id, show_polys=True, show_bbox=True, show_crowds=True, show_keypoints=True, use_url=False):

and in the function body below the if show_bbox block I added:

if show_keypoints:
    for i, segm in enumerate(self.segmentations[image_id]):
        keypoints = segm.get('keypoints', None)
        if not keypoints is None:
            for k in range(0, segm.get('num_keypoints', 0)):
                x = keypoints[k * 3 + 0]
                y = keypoints[k * 3 + 1]
                visibility = keypoints[k * 3 + 2]
                html += '<circle cx="{}" cy="{}" r="3" fill="{}" />'.format(x, y, "red")

this will draw all keypoints using red svg circles, regardless of their visibility.

@alexeyev
Copy link

Thank you for sharing!

For photos with non-landscape orientation I had to add this to your code: https://stackoverflow.com/a/63798032/1616037

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment