harryhan618 / SCNN_Pytorch

Pytorch implementation of "Spatial As Deep: Spatial CNN for Traffic Scene Understanding"
MIT License
250 stars 68 forks source link

question about _gen_label_for_json(), using slope to identify lane id #41

Closed jwngo closed 4 years ago

jwngo commented 4 years ago

Hi, thank you for sharing your code.

I am a little confused about this part of the lane label generation.

def _gen_label_for_json(self, image_set):
        H, W = 720, 1280
        SEG_WIDTH = 30
        save_dir = "seg_label"

        os.makedirs(os.path.join(self.data_dir_path, save_dir, "list"), exist_ok=True)
        list_f = open(os.path.join(self.data_dir_path, save_dir, "list", "{}_gt.txt".format(image_set)), "w")

        json_path = os.path.join(self.data_dir_path, save_dir, "{}.json".format(image_set))
        with open(json_path) as f:
            for line in f:
                label = json.loads(line)
 # ---------- clean and sort lanes -------------
                lanes = []
                _lanes = []
                slope = [] # identify 1st, 2nd, 3rd, 4th lane through slope
                for i in range(len(label['lanes'])):
                    l = [(x, y) for x, y in zip(label['lanes'][i], label['h_samples']) if x >= 0]
                    if (len(l)>1):
                        _lanes.append(l)
                        slope.append(np.arctan2(l[-1][1]-l[0][1], l[0][0]-l[-1][0]) / np.pi * 180)
                _lanes = [_lanes[i] for i in np.argsort(slope)]
                slope = [slope[i] for i in np.argsort(slope)]

                idx_1 = None
                idx_2 = None
                idx_3 = None
                idx_4 = None
                for i in range(len(slope)):
                    if slope[i]<=90:
                        idx_2 = i
                        idx_1 = i-1 if i>0 else None
                    else:
                        idx_3 = i
                        idx_4 = i+1 if i+1 < len(slope) else None
                        break
                lanes.append([] if idx_1 is None else _lanes[idx_1])
                lanes.append([] if idx_2 is None else _lanes[idx_2])
                lanes.append([] if idx_3 is None else _lanes[idx_3])
                lanes.append([] if idx_4 is None else _lanes[idx_4])
                # ---------------------------------------------

specifically, identifying the 1st, 2nd 3rd and 4th lane through slope. May I ask about the inner workings of this implementation? Like why slope[i]<=90 would signify that i is idx_2 and so on.

Thank you for your time

harryhan618 commented 4 years ago

The idea is to find four lane lines which are i). left to current left, ii). current left line, iii). current right line, vi). right to current right. 'Current' refers to the lane area where the vehicle is driving. Then sort them in the list as the lines from left to right. An intuitive way to sort as that is based on lines' slope. So that's all.