python代码——labelme标注的车道线结果(点)转为图森车道线格式

import cv2
import json
import numpy as np
import os
from operator import itemgetter

path = '/home/dell/label/'

for json_ in os.listdir(path):
	with open(os.path.join(path+json_),'r') as f:
		file_ = json.load(f)
		coors = file_['shapes']

		lane1_y = []
		lane2_y = []
		lane3_y = []
		lane4_y = []
		lane5_y = []
		lane6_y = []
		lane_y = []
		
		lane1 = []
		lane2 = []
		lane3 = []
		lane4 = []
		lane5 = []
		lane6 = []
		lane = []

		alllane = []
		y = []
		
		alllanes = {}
		for coor_ in coors:

			if coor_['label'] == 'lane1':
				lane1.append([int(coor_['points'][0][0]),int(coor_['points'][0][1])])
				lane1_y.append(int(coor_['points'][0][1]))				
				
			if coor_['label'] == 'lane2':
				lane2.append([int(coor_['points'][0][0]),int(coor_['points'][0][1])])
				lane2_y.append(int(coor_['points'][0][1]))

			if coor_['label'] == 'lane3':
				lane3.append([int(coor_['points'][0][0]),int(coor_['points'][0][1])])
				lane3_y.append(int(coor_['points'][0][1]))
				

			if coor_['label'] == 'lane4':
				lane4.append([int(coor_['points'][0][0]),int(coor_['points'][0][1])])
				lane4_y.append(int(coor_['points'][0][1]))
				
			if coor_['label'] == 'lane':
				lane.append([int(coor_['points'][0][0]),int(coor_['points'][0][1])])
				lane_y.append(int(coor_['points'][0][1]))
				
			if coor_['label'] == 'lane5':
				lane5.append([int(coor_['points'][0][0]),int(coor_['points'][0][1])])
				lane5_y.append(int(coor_['points'][0][1]))
				
			if coor_['label'] == 'lane6':
				lane6.append([int(coor_['points'][0][0]),int(coor_['points'][0][1])])
				lane6_y.append(int(coor_['points'][0][1]))
				
			y = lane1_y + lane2_y +lane3_y + lane4_y + lane5_y + lane6_y + lane_y
			y.sort()
		#print(y)
		
		for y_ in lane2_y +lane3_y + lane4_y + lane5_y + lane6_y + lane_y:
			lane1.append([-2,y_])
		for y_ in lane1_y +lane3_y + lane4_y + lane5_y + lane6_y + lane_y:
			lane2.append([-2,y_])
		for y_ in lane2_y +lane1_y + lane4_y + lane5_y + lane6_y + lane_y:
			lane3.append([-2,y_])
		for y_ in lane2_y +lane3_y + lane1_y + lane5_y + lane6_y + lane_y:
			lane4.append([-2,y_])
		for y_ in lane2_y +lane3_y + lane4_y + lane1_y + lane6_y + lane_y:
			lane5.append([-2,y_])
		for y_ in lane2_y +lane3_y + lane4_y + lane5_y + lane1_y + lane_y:
			lane6.append([-2,y_])
		for y_ in lane2_y +lane3_y + lane4_y + lane5_y + lane6_y + lane1_y:
			lane.append([-2,y_])

		'''
		for y_ in yy:
			if y_ not in lane1_y:			
				lane1.append([-2,y_])
					
			if y_ not in lane2_y:			
				lane2.append([-2,y_])
			if y_ not in lane3_y:			
				lane3.append([-2,y_])
			if y_ not in lane4_y:			
				lane4.append([-2,y_])
			if y_ not in lane5_y:			
				lane5.append([-2,y_])
			if y_ not in lane6_y:			
				lane6.append([-2,y_])
			if y_ not in lane_y:			
				lane.append([-2,y_])
		print(len(list(set([tuple(t) for t in lane1]))),len(yy))
		'''
		lane1 = sorted(lane1, key = itemgetter(1))
		lane2 = sorted(lane2, key = itemgetter(1))
		lane3 = sorted(lane3, key = itemgetter(1))
		lane4 = sorted(lane4, key = itemgetter(1))
		lane5 = sorted(lane5, key = itemgetter(1))
		lane6 = sorted(lane6, key = itemgetter(1))
		lane = sorted(lane, key = itemgetter(1))
					
		lane1_x = [x[0] for x in lane1]
		lane2_x = [x[0] for x in lane2]
		lane3_x = [x[0] for x in lane3]
		lane4_x = [x[0] for x in lane4]
		lane5_x = [x[0] for x in lane5]
		lane6_x = [x[0] for x in lane6]
		lane_x = [x[0] for x in lane]

		alllanes_ = [lane1_x,lane2_x,lane3_x,lane4_x,lane5_x,lane6_x,lane_x]
		for lane_ in alllanes_:
			if list(set(lane_)) != [-2] and len(list(set(lane_))) > 4:
				alllane.append(lane_)
				#print(len(lane_),len(y))
		images = os.listdir('/home/dell/labelimg/')		
		image = file_['imagePath'].split('/')[-1]
		images.sort()

		index = images.index(image)
		if index == 129:
		
			print(image,index)
		alllanes['lanes'] = alllane
		alllanes['h_samples'] = y
		alllanes['raw_file'] = 'clips/8-1/150/'+str(index + 3631)+'.jpg'
		
		fs = open('/home/dell/label.json','a+')
		json.dump(alllanes,fs)
		fs.write('\n')
		fs.close()
		
	
			


		    

		

你可能感兴趣的:(python代码——labelme标注的车道线结果(点)转为图森车道线格式)