switchablenorms / DeepFashion2

DeepFashion2 Dataset https://arxiv.org/pdf/1901.07973.pdf
2.26k stars 354 forks source link

How to convert anno files to mask images #86

Open lihuxiao1 opened 11 months ago

lihuxiao1 commented 11 months ago

Anyone can tell me? When I generate a mask, I found that the different item ['segmentation'] is overlapping, so I can use cv2.fillPoly to get the mask. The result is very bad

lihuxiao1 commented 11 months ago

`def Jsontopng(Yuan_path, num): count = os.listdir(os.path.join(Yuan_path,'annosjson'))

print(count)

tf = open('my_color.json','r')
color_code = json.load(tf)

for i in range(0, 3215):
    path = os.path.join(Yuan_path, 'annosjson',count[i])
    img_info = count[i].replace('json','jpg')
    img_path = os.path.join(Yuan_path,'image',img_info)
    img_png = count[i].replace('json','png')
    img_save = os.path.join(Yuan_path,'annos',img_png)

    print(path)
    print(img_path)

    if os.path.isfile(path) and path.endswith('json'):
        data = json.load(open(path))
        image = cv2.imread(img_path)
        mask = np.zeros_like(image[:,:,0])
        for item in reversed(data):
            if 'item' in item:
                seg = data[item]['segmentation']
                color_id = color_code[str(data[item]['category_id']-1)]
                for i in seg:
                    segmentation = np.array(i).reshape((-1, 2)).astype(np.int32)
                    cv2.fillPoly(mask,[segmentation],color_id)

        mask_visualize = mask.copy()

        cv2.imwrite(img_save,mask)
        # mask_visualize = cv2.cvtColor(mask_visualize,cv2.COLOR_GRAY2BGR)

        # image_with_mask = cv2.addWeighted(image, 1, mask_visualize, 0.5, 0)

        # cv2.imshow('Image with Segmentation Mask', mask)
        # cv2.waitKey(0)

if name == 'main': num = 0 Yuan_path = "/home/lhx/DLlhx/mmsegmentation-main/data/deepfashion2_lite/validation" Jsontopng(Yuan_path, num)`

this is my code