来自大佬:大佬博客
# -*- coding: utf-8 -*-
import cv2
import json
import numpy as np
import os
base_path = r"/home/jie/TuSimple/train_set/"
file = open(r'/home/jie/TuSimple/train_set/label_data_0601.json', 'r')
image_num = 1#第一张照片的名称
for i in ["gt_image_binary","gt_image_instance", "image"]:
path = os.path.join(base_path, i)
if not os.path.exists(path):
os.mkdir(path)
print("the dir is created!")
else:
print("the dir is exist!")
for line in file.readlines():
data = json.loads(line)
file_path = os.path.join(base_path, data['raw_file'].replace("\\","/"))
image = cv2.imread(file_path)
binaryimage = np.zeros((image.shape[0], image.shape[1], 1), np.uint8)
instanceimage = binaryimage.copy()
arr_width = data['lanes']
arr_height = data['h_samples']
width_num = len(arr_width)
height_num = len(arr_height)
for i in range(height_num):
lane_hist = 0
for j in range(width_num):
if arr_width[j][i - 1] > 0 and arr_width[j][i] > 0:
binaryimage[int(arr_height[i]), int(arr_width[j][i])] = 255
instanceimage[int(arr_height[i]), int(arr_width[j][i])] = lane_hist
if i > 0:
cv2.line(binaryimage, (int(arr_width[j][i - 1]), int(arr_height[i - 1])),
(int(arr_width[j][i]), int(arr_height[i])), 255, 10)
cv2.line(instanceimage, (int(arr_width[j][i - 1]), int(arr_height[i - 1])),
(int(arr_width[j][i]), int(arr_height[i])), lane_hist, 10)
lane_hist += 1
string1 = base_path+"/gt_image_binary/" + str(image_num) + ".png"
string2 = base_path+"/gt_image_instance/" + str(image_num) + ".png"
string3 = base_path+"/image/" + str(image_num) + ".png"
print(string1, string2, string3)
cv2.imwrite(string1, binaryimage)
cv2.imwrite(string2, instanceimage)
cv2.imwrite(string3, image)
image_num = image_num + 1
file.close()