zhiqi-li / Panoptic-SegFormer

This is the official repo of Panoptic SegFormer [CVPR'22]
Apache License 2.0
211 stars 29 forks source link

How demo one picture result ? #6

Closed delldu closed 2 years ago

delldu commented 2 years ago

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 commented 2 years ago

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

delldu commented 2 years ago

Many thanks

garriton commented 2 years ago

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)