Open lihuxiao1 opened 1 year ago
`def Jsontopng(Yuan_path, num): count = os.listdir(os.path.join(Yuan_path,'annosjson'))
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
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