zymk9 / Yet-Another-Anime-Segmenter

Instance segmentation for anime characters based on CondInst and SOLOv2
MIT License
188 stars 13 forks source link

Is there a way to automatically crop the result? #7

Closed kisenera closed 2 years ago

kisenera commented 2 years ago

Like place the detection over a transparent/white background

zymk9 commented 2 years ago

Well, I should have added some of these features to make it really a practical tool but haven't so far. Sorry about that.

I am not aware of any similar utility functions in AdelaiDet or Detectron2, but given the semantic masks and bounding boxes it should be simple to implement it yourself.

Techno-coder commented 2 years ago

I found a demo of this segmenter on HuggingFace: https://huggingface.co/spaces/hysts/Yet-Another-Anime-Segmenter

In the app.py file, the construction of the images is handled in the predict function. You can add an alpha channel to masked like so:

    # Copy image and add alpha channel.
    masked = image.copy()[:, :, ::-1]
    alpha = np.full(masked.shape[:-1], 255)
    masked = np.dstack((masked, alpha))

    # Mark blank areas.
    mask = instances.pred_masks.cpu().numpy().astype(int).max(axis=0)
    masked[mask == 0] = [0, 0, 0, 0]

Note that the colours of the resultant image look a bit weird (more faded than the original; not sure why this happens) (nevermind, it was a mistake with how I turned the numpy array back into an image):

image

but it should be easy to copy the pixels from the original image using the alpha channel as a mask.

kisenera commented 2 years ago

very nice, thanks guys