Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How demo one picture result ? #6

Closed
delldu opened this issue May 6, 2022 · 3 comments
Closed

How demo one picture result ? #6

delldu opened this issue May 6, 2022 · 3 comments

Comments

@delldu
Copy link

delldu commented May 6, 2022

Dear friend,
Thanks you for your good job.
Now we do not want to download coco datasets, just want to give one picture, segment it and show its result. How to do it ?
Best regards,

@zhiqi-li
Copy link
Owner

zhiqi-li commented May 7, 2022

@delldu
Copy link
Author

delldu commented May 7, 2022

Many thanks

@delldu delldu closed this as completed May 7, 2022
@garriton
Copy link

garriton commented Jul 8, 2022

you can use this code https://github.com/zhiqi-li/Panoptic-SegFormer/blob/master/tools/bricks/infererce.py

Dear friend,
how to visualize the segmentation result of custom images?
I run the infererce.py and didn’t get a good result. I think there are some faults in my code.
Here is my code:

from mmcv.runner import checkpoint
from mmdet.apis.inference import init_detector,LoadImage, inference_detector
import easymd
import cv2
import random
import colorsys
import numpy as np

def random_colors(N, bright=True):
    brightness = 1.0 if bright else 0.7
    hsv = [(i / float(N), 1, brightness) for i in range(N)]
    colors = list(map(lambda c: colorsys.hsv_to_rgb(*c), hsv))
    random.shuffle(colors)
    return colors

def apply_mask(image, mask, color, alpha=0.5):
    for c in range(3):
        image[:, :, c] = np.where(mask == 0,
                                  image[:, :, c],
                                  image[:, :, c] *
                                  (1 - alpha) + alpha * color[c] * 255)
    return image

config = './configs/panformer/panformer_pvtb5_24e_coco_panoptic.py'
#checkpoints = './checkpoints/pseg_r101_r50_latest.pth'
checkpoints = "./checkpoints/panoptic_segformer_pvtv2b5_2x.pth"
img_path = "img_path "
mask_save_path = "save_path"

colors = random_colors(80)

model = init_detector(config,checkpoint=checkpoints)

results = inference_detector(model, [img_path])

img = cv2.imread(img_path)

seg = results['segm'][0]
N = len(seg)

masked_image = img.copy()
for i in range(N):
    color = colors[i]
    masks = np.sum(seg[i], axis=0)
    masked_image = apply_mask(masked_image, masks, color)
    # for mask in seg[i]:
    #     masked_image = apply_mask(masked_image, mask, color)

# cv2.imshow("a", masked_image)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants