Huawei Street View Semantic Segmentation Data Set json file is converted to label tags

Encountered a complicated problem today. Huawei's semantic segmentation street view dataset is a json file, not a label image that has been generated. At first, I didn't know how to do it. I wanted to make it through the cityscapesScripts-master tool of the cityscapes dataset, but I didn't get any results for a long time.

So I want to make my own without wheels. . . . . . Uh uh uh, uncomfortable

If you look at the picture below without further ado, you will find that in fact, the json file is a dictionary. The most important thing in the customization is the thing in the red box, which contains the vertex coordinates of the circumscribed closed polygon of each object . If you don’t understand Read this sentence several times. Our goal is to generate an image so that these enclosed areas are filled with corresponding target values

In fact, each vertex coordinate of each object here is connected to form an outline, then the problem is transformed into the problem of coloring within the outline. So this is simple. It can be easily achieved by using opencv's fillPoly function. How to use Baidu for cv2 .fillPoly, I won't talk about it here.

We first need to parse the json file, and then parse the value that should be filled for each object. Then generate a corresponding 0 array, and finally fill the corresponding value in the corresponding outline of the 0 array. code show as below:

import numpy as np
import matplotlib.pyplot as plt
import cv2
import json
import glob

_json = glob.glob('./gtFine/*/*/*.json')   # 所有json文件

# 不同类别的物体对应的值
name_dict = {'flat': 0,
             'human': 1,
             'vehicle': 2,
             'construction': 3,
             'object': 4,
             'nature': 5,
             'sky': 6}
for j in _json:
    with open(j, 'r') as f:
        r = f.read()
    r = json.loads(r)
    obj = r['objects']

    # 0数组 ,这里的3通道是我按照cityscapes的方式保存的
    # 其实三个通道两两相等

    img = np.zeros((1024,2048,3))
    
    # 填充
    for i in obj:
        name = i['label']
        po = i['polygon']
        n = name_dict[name]
        cv2 .fillPoly(img,pts = [np.array(po)],color =(n,n,n))

    # 保存
    cv2.imwrite(j[:-4]+'png', img)

The renderings are as follows:

   

Guess you like

Origin blog.csdn.net/qq_39197555/article/details/112155822
Recommended