txt to json

flag = -1
frame = 0
point_id = 0
rc_file = './suzhou_0508.txt'

with open(rc_file,'r') as f:
     for line in f.readlines():
         content = line.strip('\n')
         a = content.split(";")
         #print(a)
         if 'Start' in a and '  message: "LaneMarkings' in a:
             frame = frame + 1
             #print(frame)
             json_file = './' + str(frame) + '.json'
             print(json_file)
             with open(json_file, 'w') as jf:
                  jf.write('{\n')
                  jf.write(' "version": "3.14.1",\n')
                  jf.write(' "flags": {},\n')
                  jf.write(' "lineColor": [0,255,0,128],\n')
                  jf.write(' "fillColor": [255,0,0,128],\n')
                  jf.write(' "imageHeight": 1208,\n')
                  jf.write(' "imageWidth": 1920,\n')
                  jf.write(' "imagePath": "')
                  jf.write(json_file.replace("json","png"))
                  jf.write('",\n')
                  jf.write(' "shapes": [')
         if 'Start' not in a and 'End' not in a and '  message: "LaneMarkings' in a:
             print("a is, {}".format(a))
             #print("the len is, {}".format(len(a)))
             #print("a is, {}".format(a))
             lane_id = int(a[1].split(',')[0])        # 0 is 0th line, 1 is 1th line
             lane_position = int(a[1].split(',')[1])  # -2 is left lane, -1 is left of ego, 1 is right of ego, 2 is right lane, 999 is others
             lane_type = int(a[1].split(',')[2])    # 0 is undefined, 1 is solid, 2 is dashed, 3 is roadboundary, 4 is count
             if lane_type == 0:
                lane_type = "undefined"
             if lane_type == 1:
                lane_type = "white solid"
             if lane_type == 2:
                lane_type = "white dashed"
             if lane_type == 3:
                lane_type = "white roadboundary"

             lane_points = int(a[1].split(',')[3])  # points inside the lane
             #print(lane_id)
             #print(lane_points)

             points = []
             point_id = point_id + 1

             points.append('[')
             points.append(a[1].split(',')[4])
             points.append(',')
             points.append(a[1].split(',')[5])
             points.append(']')
             points.append(',')

             points.append('[')
             points.append(str(float(a[1].split(',')[4]) + 3))
             points.append(',')
             points.append(str(float(a[1].split(',')[5]) + 3))
             points.append(']')

             if point_id != lane_points:
                print(point_id)
                print(lane_points)
                points.append(',')
             if point_id == lane_points:
                point_id = 0    # reset point_id
                points.append('\n]   ')


             if lane_id == flag:
                with open(json_file, 'a') as jf:
                     jf.write(''.join(points))

             else:
                with open(json_file, 'a') as jf:
                     if lane_id != 0:
                        jf.write('\n   },\n')
                        jf.write('\n   {\n')
                     if lane_id == 0:
                        jf.write('\n   {\n')
                     jf.write('     "label": ')
                     jf.write('"')
                     jf.write(lane_type)
                     jf.write('"')
                     jf.write(',\n')
                     jf.write('     "line_color": null,\n')
                     jf.write('     "fill_color": null,\n')
                     jf.write('     "shape_type": "polygon",\n')
                     jf.write('     "points": [\n')
                     jf.write(''.join(points))

             flag = lane_id



         if 'End' in a and '  message: "LaneMarkings' in a:
                with open(json_file, 'a') as jf:
                     jf.write('\n   }\n')
                     jf.write('\n      ]')
                     jf.write('\n}\n')

         if frame > 10:
            break

  

posted on 2019-05-10 13:02  cdekelon  阅读(145)  评论(0)    收藏  举报

导航