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 to visualize demo image? #10

Closed
garriton opened this issue Jul 14, 2022 · 2 comments
Closed

how to visualize demo image? #10

garriton opened this issue Jul 14, 2022 · 2 comments

Comments

@garriton
Copy link

Dear friend,
how to visualize the segmentation result of custom images?
I run the infererce.py and didn’t get a good result. Like this:
000000

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)
@hzf-hhh
Copy link

hzf-hhh commented Jun 8, 2023

Dear friend, how to visualize the segmentation result of custom images? I run the infererce.py and didn’t get a good result. Like this: 000000

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)

您好,我想问下您解决了吗,我使用源代码提供的infererce.py,往inference_detector传入img一直报错,但是用[img]却运行正常,但是还是无法用model.show_result(img, result, out_file='result.jpg')将结果映射到图片上,想问下您是如何解决的

@garriton
Copy link
Author

Dear friend, how to visualize the segmentation result of custom images? I run the infererce.py and didn’t get a good result. Like this: 000000
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)

您好,我想问下您解决了吗,我使用源代码提供的infererce.py,往inference_detector传入img一直报错,但是用[img]却运行正常,但是还是无法用model.show_result(img, result, out_file='result.jpg')将结果映射到图片上,想问下您是如何解决的

I’m sorry, I didn’t get the correct output in the end, so I switched to other repository.

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

2 participants