mmdeploy icon indicating copy to clipboard operation
mmdeploy copied to clipboard

There is a problem with own painting mask

Open monkeycc opened this issue 3 years ago • 1 comments

from mmdeploy_python import Detector
import cv2

# 读取图片
img = cv2.imread('mmdetection/demo/demo.jpg')

# 创建检测器
detector = Detector(model_path='mmdeploy_models/mask-rcnn', device_name='cuda', device_id=0)
# 执行推理
bboxes, labels, _ = detector(img)

# 使用阈值过滤推理结果,并绘制到原图中
indices = [i for i in range(len(bboxes))]
for index, bbox, label_id in zip(indices, bboxes, labels):
    [left, top, right, bottom], score = bbox[0:4].astype(int), bbox[4]

    
    if score < 0.3:
        continue

    color =  (0,0,255)
    # draw bbox
    image = cv2.rectangle(image, (left, top), (right, bottom), color,4)
    # draw mask
    if masks[index].size:

        mask = masks[index] * 255
        image = image.astype('float32')
        alpha = .7
        w_ratio = .4
        color_mask = np.asarray(color, dtype=int)
        for c in range(3):
            color_mask[c] = color_mask[c] * (1 - w_ratio) + w_ratio * 255
        idx = np.nonzero(mask)
        image[idx[0], idx[1], :] *= 1.0 - alpha
        image[idx[0], idx[1], :] += alpha * color_mask
        image = image.astype("uint8")
        contours = cv2.findContours(
            mask.astype("uint8"), cv2.RETR_CCOMP,
            cv2.CHAIN_APPROX_NONE)[-2]
        image = cv2.drawContours(
            image,
            contours,
            contourIdx=-1,
            color=color,
            thickness=1,
            lineType=cv2.LINE_AA)
cv2.imwrite("F:/MM/" + "demo.jpg", image)

I try to make my own mask

But all of them are at coordinates 0, 0

I don't know how to cope

I need to calculate the length and width of each mask

微信图片_20220904130551

monkeycc avatar Sep 04 '22 05:09 monkeycc

@lvhan028

tpoisonooo avatar Sep 04 '22 05:09 tpoisonooo