steering-curve.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. from __future__ import print_function
  2. import cv2
  3. import glob
  4. import os
  5. import sys
  6. import math
  7. import random
  8. import csv
  9. import shutil
  10. import imageio
  11. import time
  12. import tensorflow as tf
  13. import numpy as np
  14. import pandas as pd
  15. import argparse
  16. from data_utils import *
  17. from tensorflow.keras.applications.imagenet_utils import preprocess_input
  18. from keras_preprocessing import image
  19. from tensorflow.keras.layers import Convolution2D, Input, Dense, Flatten, Lambda, MaxPooling2D, Dropout, Activation, \
  20. SpatialDropout2D
  21. # remove merge
  22. from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint
  23. from tensorflow.keras import models, optimizers, backend
  24. from PIL import Image
  25. ##########################################移植模型代码用于生成预测值并得到转角曲线###############################################
  26. def Dave_orig(input_tensor=None, load_weights=False): # original dave
  27. if input_tensor is None:
  28. input_tensor = Input(shape=(100, 100, 3))
  29. x = Convolution2D(24, (5, 5), padding='valid', activation='relu', strides=(2, 2), name='block1_conv1')(input_tensor)
  30. x = Convolution2D(36, (5, 5), padding='valid', activation='relu', strides=(2, 2), name='block1_conv2')(x)
  31. x = Convolution2D(48, (5, 5), padding='valid', activation='relu', strides=(2, 2), name='block1_conv3')(x)
  32. x = Convolution2D(64, (3, 3), padding='valid', activation='relu', strides=(1, 1), name='block1_conv4')(x)
  33. x = Convolution2D(64, (3, 3), padding='valid', activation='relu', strides=(1, 1), name='block1_conv5')(x)
  34. x = Flatten(name='flatten')(x)
  35. x = Dense(1164, activation='relu', name='fc1')(x)
  36. x = Dense(100, activation='relu', name='fc2')(x)
  37. x = Dense(50, activation='relu', name='fc3')(x)
  38. x = Dense(10, activation='relu', name='fc4')(x)
  39. x = Dense(1, name='before_prediction')(x)
  40. x = Lambda(atan_layer, output_shape=atan_layer_shape, name='prediction')(x)
  41. m = Model(input_tensor, x)
  42. if load_weights:
  43. m.load_weights('./trained_models/Model1.h5')
  44. # compiling
  45. m.compile(loss='mse', optimizer='Adam')
  46. # m.compile(loss=[rmse], optimizer='adadelta')
  47. return m
  48. def Dave_norminit(input_tensor=None, load_weights=False): # original dave with normal initialization
  49. if input_tensor is None:
  50. input_tensor = Input(shape=(100, 100, 3))
  51. x = Convolution2D(24, (5, 5), padding='valid', activation='relu', strides=(2, 2),
  52. name='block1_conv1')(input_tensor)
  53. x = Convolution2D(36, (5, 5), padding='valid', activation='relu', strides=(2, 2),
  54. name='block1_conv2')(x)
  55. x = Convolution2D(48, (5, 5), padding='valid', activation='relu', strides=(2, 2),
  56. name='block1_conv3')(x)
  57. x = Convolution2D(64, (3, 3), padding='valid', activation='relu', strides=(1, 1),
  58. name='block1_conv4')(x)
  59. x = Convolution2D(64, (3, 3), padding='valid', activation='relu', strides=(1, 1),
  60. name='block1_conv5')(x)
  61. x = Flatten(name='flatten')(x)
  62. x = Dense(1164, kernel_initializer=normal_init, activation='relu', name='fc1')(x)
  63. x = Dense(100, kernel_initializer=normal_init, activation='relu', name='fc2')(x)
  64. x = Dense(50, kernel_initializer=normal_init, activation='relu', name='fc3')(x)
  65. x = Dense(10, kernel_initializer=normal_init, activation='relu', name='fc4')(x)
  66. x = Dense(1, name='before_prediction')(x)
  67. x = Lambda(atan_layer, output_shape=atan_layer_shape, name='prediction')(x)
  68. m = Model(input_tensor, x)
  69. if load_weights:
  70. m.load_weights('./trained_models/Model2.h5')
  71. # compiling
  72. m.compile(loss='mse', optimizer='Adam')
  73. # m.compile(loss=[rmse], optimizer='adadelta')
  74. return m
  75. def Dave_dropout(input_tensor=None, load_weights=False): # simplified dave
  76. if input_tensor is None:
  77. input_tensor = Input(shape=(100, 100, 3))
  78. x = Convolution2D(16, (3, 3), padding='valid', activation='relu', name='block1_conv1')(input_tensor)
  79. x = MaxPooling2D(pool_size=(2, 2), name='block1_pool1')(x)
  80. x = Convolution2D(32, (3, 3), padding='valid', activation='relu', name='block1_conv2')(x)
  81. x = MaxPooling2D(pool_size=(2, 2), name='block1_pool2')(x)
  82. x = Convolution2D(64, (3, 3), padding='valid', activation='relu', name='block1_conv3')(x)
  83. x = MaxPooling2D(pool_size=(2, 2), name='block1_pool3')(x)
  84. x = Flatten(name='flatten')(x)
  85. x = Dense(500, activation='relu', name='fc1')(x)
  86. x = Dropout(.5)(x)
  87. x = Dense(100, activation='relu', name='fc2')(x)
  88. x = Dropout(.25)(x)
  89. x = Dense(20, activation='relu', name='fc3')(x)
  90. x = Dense(1, name='before_prediction')(x)
  91. x = Lambda(atan_layer, output_shape=atan_layer_shape, name="prediction")(x)
  92. m = Model(input_tensor, x)
  93. if load_weights:
  94. m.load_weights('./trained_models/Model3.h5')
  95. # compiling
  96. m.compile(loss='mse', optimizer=optimizers.Adam(lr=1e-04))
  97. # m.compile(loss=[rmse], optimizer='adadelta')
  98. return m
  99. def Epoch_model(input_tensor=None, load_weights=False):
  100. if input_tensor is None:
  101. input_tensor = Input(shape=(128, 128, 3))
  102. x = Convolution2D(32, (3, 3), activation='relu', padding='same')(input_tensor)
  103. x = MaxPooling2D((2, 2), strides=(2, 2))(x)
  104. x = Dropout(0.25)(x)
  105. x = Convolution2D(64, (3, 3), activation='relu', padding='same')(x)
  106. x = MaxPooling2D((2, 2), strides=(2, 2))(x)
  107. x = Dropout(0.25)(x)
  108. x = Convolution2D(128, (3, 3), activation='relu', padding='same')(x)
  109. x = MaxPooling2D((2, 2), strides=(2, 2))(x)
  110. x = Dropout(0.5)(x)
  111. y = Flatten()(x)
  112. y = Dense(1024, activation='relu')(y)
  113. y = Dropout(.5)(y)
  114. y = Dense(1)(y)
  115. m = Model(input_tensor, y)
  116. if load_weights:
  117. m.load_weights('./trained_models/Model4.h5')
  118. # compliling
  119. m.compile(loss='mse', optimizer=optimizers.Adam(lr=1e-04))
  120. # m.compile(loss=[rmse], optimizer='adadelta')
  121. return m
  122. def calc_mse(yhat, label): # used for loss cal, output float
  123. mse = 0.
  124. count = 0
  125. if len(yhat) != len(label):
  126. print("[" + os.path.basename(__file__) + ", Line " + str(sys._getframe().f_lineno) + ", " + sys._getframe().f_code.co_name + "] ", "yhat and label have different lengths")
  127. return -1
  128. for i in range(len(yhat)):
  129. count += 1
  130. predicted_steering = yhat[i]
  131. steering = label[i]
  132. # print("[" + os.path.basename(__file__) + ", Line " + str(sys._getframe().f_lineno) + ", " + sys._getframe().f_code.co_name + "] ", predicted_steering)
  133. # print("[" + os.path.basename(__file__) + ", Line " + str(sys._getframe().f_lineno) + ", " + sys._getframe().f_code.co_name + "] ", steering)
  134. mse += (float(steering) - float(predicted_steering)) ** 2.
  135. return (mse / count)
  136. def model_predict(model_name):
  137. # Train variables define & Input variables parse
  138. # TODO
  139. if not isinstance(model_name, str):
  140. model_name = str(model_name)
  141. batch_size = 64
  142. nb_epoch = 30
  143. image_shape = (100, 100)
  144. # model_name = sys.argv[1]
  145. test_dataset_path = './scenario_runner-0.9.13/_out/'
  146. with open(test_dataset_path + 'label_test.csv', 'r') as f:
  147. rows = len(f.readlines()) - 1
  148. if rows == 0:
  149. return 0
  150. # --------------------------------------Build Model---------------------------------------- #
  151. # Dave_v1
  152. if model_name == '1':
  153. model = Dave_orig(None, True)
  154. save_model_name = './trained_models/Model1.h5'
  155. # Dave_v2
  156. elif model_name == '2':
  157. # K.set_learning_phase(1)
  158. model = Dave_norminit(None, True)
  159. save_model_name = './trained_models/Model2.h5'
  160. # batch_size = 64 # 1 2 3 4 5 6x
  161. nb_epoch = 30
  162. # Dave_v3
  163. elif model_name == '3':
  164. # K.set_learning_phase(1)
  165. model = Dave_dropout(None, True)
  166. save_model_name = './trained_models/Model3.h5'
  167. # nb_epoch = 30
  168. # Udacity Epoch Model
  169. elif model_name == '4':
  170. model = Epoch_model(None, True)
  171. save_model_name = './trained_models/Model4.h5'
  172. image_shape = (128, 128)
  173. nb_epoch = 30
  174. batch_size = 32
  175. else:
  176. print("[" + os.path.basename(__file__) + ", Line " + str(sys._getframe().f_lineno) + ", " + sys._getframe().f_code.co_name + "] ", bcolors.FAIL + 'invalid model name, must in [1, 2, 3, 4]' + bcolors.ENDC)
  177. print("[" + os.path.basename(__file__) + ", Line " + str(sys._getframe().f_lineno) + ", " + sys._getframe().f_code.co_name + "] ", bcolors.OKGREEN + 'model %s built' % model_name + bcolors.ENDC)
  178. # --------------------------------------Evaluation---------------------------------------- #
  179. # Different evaluation methods for different model
  180. png_name_list = []
  181. if model_name != '4':
  182. K.set_learning_phase(0)
  183. test_generator, samples_per_epoch = load_carla_test_data(path=test_dataset_path, batch_size=batch_size,
  184. shape=image_shape)
  185. print("[" + os.path.basename(__file__) + ", Line " + str(sys._getframe().f_lineno) + ", " + sys._getframe().f_code.co_name + "] ", 'test samples: ', samples_per_epoch)
  186. loss = model.evaluate(test_generator, steps=math.ceil(samples_per_epoch * 1. / batch_size), verbose=1)
  187. print("[" + os.path.basename(__file__) + ", Line " + str(sys._getframe().f_lineno) + ", " + sys._getframe().f_code.co_name + "] ", "model %s evaluate_generator loss: %.8f" % (model_name, loss))
  188. # --------------------------------------Predict Dave---------------------------------------- #
  189. true_angle_list = []
  190. filelist = []
  191. with open(test_dataset_path + 'label_test.csv', 'r') as f:
  192. rows = len(f.readlines()) - 1
  193. f.seek(0)
  194. for i, line in enumerate(f):
  195. if i == 0:
  196. continue
  197. file_name = line.split(',')[0]
  198. # TODO BEGIN
  199. # if i > int(rows * 0.75):
  200. filelist.append(test_dataset_path + 'center/' + file_name)
  201. png_name_list.append(file_name)
  202. true_angle_list.append(float(line.split(',')[2]))
  203. # TODO END
  204. # print("[" + os.path.basename(__file__) + ", Line " + str(sys._getframe().f_lineno) + ", " + sys._getframe().f_code.co_name + "] ", 'filelist length: ', len(filelist))
  205. # print("[" + os.path.basename(__file__) + ", Line " + str(sys._getframe().f_lineno) + ", " + sys._getframe().f_code.co_name + "] ", 'true_angle_list', true_angle_list)
  206. print("[" + os.path.basename(__file__) + ", Line " + str(sys._getframe().f_lineno) + ", " + sys._getframe().f_code.co_name + "] ", "--------IMG READ-------")
  207. predict_angle_list = []
  208. imgs = []
  209. raw_imgs = []
  210. count = 0
  211. ori_image_size = (720, 1280)
  212. for f in filelist:
  213. count += 1
  214. if (count % 100 == 0):
  215. print("[" + os.path.basename(__file__) + ", Line " + str(sys._getframe().f_lineno) + ", " + sys._getframe().f_code.co_name + "] ", str(count) + ' images read')
  216. orig_name = f
  217. gen_img = preprocess_image(orig_name, image_shape)
  218. raw_img = preprocess_image(orig_name, ori_image_size)
  219. imgs.append(gen_img)
  220. raw_imgs.append(raw_img)
  221. print("[" + os.path.basename(__file__) + ", Line " + str(sys._getframe().f_lineno) + ", " + sys._getframe().f_code.co_name + "] ", "--------IMG READ COMPLETE-------")
  222. print("[" + os.path.basename(__file__) + ", Line " + str(sys._getframe().f_lineno) + ", " + sys._getframe().f_code.co_name + "] ", "--------DAVE PREDICT-------")
  223. count = 0
  224. imgs = np.array(imgs)
  225. for i in range(len(imgs)):
  226. predict_angle_list.append(model.predict(imgs[i])[0])
  227. # TODO: Add arrows to raw images and save
  228. # gen_img_deprocessed = draw_arrow3(deprocess_image(raw_imgs[i], (720, 1280, 3)), -true_angle_list[i], -predict_angle_list[-1])
  229. # imsave('./test_output_carla/' + str(i) + 'th_img.png', gen_img_deprocessed)
  230. # count += 1
  231. # if (count % 20 == 0):
  232. # print("[" + os.path.basename(__file__) + ", Line " + str(sys._getframe().f_lineno) + ", " + sys._getframe().f_code.co_name + "] ", str(count) + ' images saved')
  233. print("[" + os.path.basename(__file__) + ", Line " + str(sys._getframe().f_lineno) + ", " + sys._getframe().f_code.co_name + "] ", "--------DAVE PREDICT COMPLETE-------")
  234. yhat = np.array([x.flatten()[0] for x in predict_angle_list])
  235. test_y = true_angle_list
  236. else:
  237. test_steering_log = path.join(test_dataset_path, 'label_test.csv')
  238. test_data = carla_load_steering_data(test_steering_log)
  239. png_name_list = get_png_name_list(test_steering_log)
  240. # dataset divide
  241. time_list_test = carla_load_frame_id(test_data)
  242. print("[" + os.path.basename(__file__) + ", Line " + str(sys._getframe().f_lineno) + ", " + sys._getframe().f_code.co_name + "] ", 'time_list_test: ', len(time_list_test), time_list_test)
  243. test_generator = carla_data_generator(frame_id=time_list_test,
  244. steering_log=test_steering_log,
  245. image_folder=test_dataset_path,
  246. unique_list=time_list_test,
  247. gen_type='test',
  248. batch_size=len(time_list_test),
  249. image_size=image_shape,
  250. shuffle=False,
  251. preprocess_input=normalize_input,
  252. preprocess_output=exact_output)
  253. # --------------------------------------Predict Epoch---------------------------------------- #
  254. print("[" + os.path.basename(__file__) + ", Line " + str(sys._getframe().f_lineno) + ", " + sys._getframe().f_code.co_name + "] ", "--------EPOCH PREDICT-------")
  255. test_x, test_y = next(test_generator)
  256. yhat = model.predict(test_x, verbose=1)
  257. yhat = [item[0] for item in yhat]
  258. print("[" + os.path.basename(__file__) + ", Line " + str(sys._getframe().f_lineno) + ", " + sys._getframe().f_code.co_name + "] ", "--------EPOCH PREDICT COMPLETE-------")
  259. print("[" + os.path.basename(__file__) + ", Line " + str(sys._getframe().f_lineno) + ", " + sys._getframe().f_code.co_name + "] ", 'png_name_list', png_name_list)
  260. print("[" + os.path.basename(__file__) + ", Line " + str(sys._getframe().f_lineno) + ", " + sys._getframe().f_code.co_name + "] ", 'yhat', yhat)
  261. #----------------------存储预测结果用于绘制转角曲线------------------------------
  262. print("[" + os.path.basename(__file__) + ", Line " + str(sys._getframe().f_lineno) + ", " + sys._getframe().f_code.co_name + "] ", 'Write predict result to csv file')
  263. write_rows = zip(png_name_list, yhat)
  264. with open(test_dataset_path + 'predict_result.csv', 'w', encoding='utf-8') as f:
  265. writer = csv.writer(f)
  266. writer.writerow(['png_file', 'predict_angle'])
  267. for row in write_rows:
  268. writer.writerow(row)
  269. def get_png_name_list(steering_log):
  270. png_name_list = []
  271. df_steer = pd.read_csv(steering_log, usecols=['frame_id', 'steering_angle_change'], index_col=False)
  272. for frame in df_steer['frame_id']:
  273. png_name_list.append(frame)
  274. return png_name_list
  275. ##########################################移植模型代码用于生成预测值并得到转角曲线###############################################
  276. err_img_path = sys.path[0] + '/err_img_new/'
  277. def draw_arrow(img, angle, thickness = 1, imgtype=0):
  278. pt1 = (int(img.shape[1] / 2), img.shape[0])
  279. pt2_angle1 = (int(img.shape[1] / 2 + img.shape[0] / 3 * math.sin(angle)),
  280. int(img.shape[0] - img.shape[0] / 3 * math.cos(angle)))
  281. if imgtype == 1:
  282. img = cv2.arrowedLine(img, pt1, pt2_angle1, (255, 0, 0), thickness)
  283. else:
  284. img = cv2.arrowedLine(img, pt1, pt2_angle1, (0, 0, 255), thickness)
  285. return img
  286. def preprocess_image(img_path, target_size=(100, 100)):
  287. img = image.load_img(img_path, target_size=target_size)
  288. input_img_data = image.img_to_array(img)
  289. input_img_data = np.expand_dims(input_img_data, axis=0)
  290. input_img_data = preprocess_input(input_img_data)
  291. return input_img_data
  292. def deprocess_image(x,shape=(100,100,3)):
  293. tmp = x.copy()
  294. tmp = tmp.reshape(shape)
  295. # Remove zero-center by mean pixel
  296. tmp[:, :, 0] += 103.939
  297. tmp[:, :, 1] += 116.779
  298. tmp[:, :, 2] += 123.68
  299. # 'BGR'->'RGB'
  300. tmp = tmp[:, :, ::-1]
  301. tmp = np.clip(tmp, 0, 255).astype('uint8')
  302. return tmp
  303. def process_csv(csv_path):
  304. img_name_dic = {}
  305. with open(csv_path, 'r') as f:
  306. for m, line in enumerate(f):
  307. if m == 0:
  308. continue
  309. line_info = line.rstrip('\n').split(',')
  310. img_name_dic[line_info[0]] = line_info[1:]
  311. print('img_name_dic', img_name_dic)
  312. return img_name_dic
  313. def convertToPDF(path):
  314. # Open the image
  315. # path = os.path.join(sys.path[0], 'selected_diff_imgs/12626571.png')
  316. image = Image.open(path)
  317. image = image.convert("RGB")
  318. # Save the image as a PDF
  319. path = path.replace('selected_diff_imgs', 'selected_diff_pdfs')
  320. image.save(path.replace('png', 'pdf'))
  321. def generateNewCsv():
  322. for model_id in range(1, 5):
  323. for scene_id in range(1, 7):
  324. # 1. 复制一个名为list.csv文件到其所在目录命名为list_new.csv;
  325. cur_list_path = err_img_path + str(model_id) + "/" + str(scene_id) + "/"
  326. shutil.copy(cur_list_path + "list.csv", cur_list_path + "list_new.csv")
  327. # 2. 打开list_new.csv文件并对其最右边添加一列,这一列的值为单元格D1与E1的差的绝对值。将这一计算规则运用于该list_new.csv的所有行数据。
  328. with open(cur_list_path + "list_new.csv", "r") as f:
  329. reader = csv.reader(f)
  330. data = list(reader)
  331. # add the new column
  332. data[0].append("angle_diff")
  333. for i in range(1, len(data)):
  334. data[i].append(abs(float(data[i][3]) - float(data[i][4])))
  335. with open(cur_list_path + "list_new.csv", "w", newline="") as f:
  336. writer = csv.writer(f)
  337. writer.writerows(data)
  338. def mergeFile():
  339. for model_id in range(1, 5):
  340. for scene_id in range(1, 7):
  341. cur_list_path = err_img_path + str(model_id) + "/" + str(scene_id) + "/"
  342. if model_id == 1 and scene_id == 1:
  343. # 读取第一个文件并创建合并后的 DataFrame
  344. df_merged = pd.read_csv(cur_list_path + "list_new.csv", header=0)
  345. else:
  346. df = pd.read_csv(cur_list_path + "list_new.csv", header=0)
  347. df_merged = pd.concat([df_merged, df])
  348. df_merged.to_csv(err_img_path + "merged_csv_new.csv", index=False)
  349. def find_files(path, extension):
  350. file_paths = []
  351. for root, dirs, files in os.walk(path):
  352. for file in files:
  353. if file.endswith(extension):
  354. file_paths.append(os.path.join(root, file))
  355. return file_paths
  356. def files_process(path, extension):
  357. png_files = find_files(path, extension)
  358. i = 1
  359. for png_path in png_files:
  360. print("--Convert Progress: " + str(i) + " / 50")
  361. convertToPDF(png_path)
  362. i += 1
  363. def main():
  364. seed_town_dic = [0, 4, 4, 6, 7, 7, 7]
  365. model_dic = ['', 'dave_v1', 'dave_v2', 'dave_v3', 'epoch']
  366. print("---IMG READ---")
  367. raw_imgs = []
  368. muted_imgs = []
  369. img_name_dic = process_csv(os.path.join(err_img_path, 'merged_csv_new.csv'))
  370. filelist = list(img_name_dic.keys())
  371. for img_name in filelist:
  372. muted_img_path = err_img_path + img_name_dic[img_name][5] + '/' + img_name_dic[img_name][4] + '/' + img_name
  373. ori_img_path = sys.path[0] + '/seed_data_train/seed' + str(img_name_dic[img_name][4]) + '_town0' + str(seed_town_dic[int(img_name_dic[img_name][4])]) + '/' + img_name_dic[img_name][0]
  374. # print(muted_img_path + " " + ori_img_path)
  375. muted_img = preprocess_image(muted_img_path,(720,1280))
  376. muted_imgs.append(muted_img)
  377. raw_img = preprocess_image(ori_img_path,(720,1280))
  378. raw_imgs.append(raw_img)
  379. print("---IMG READ COMPLETE---")
  380. print("---IMG WRITE---")
  381. imsave = imageio.imsave
  382. for i in range(len(raw_imgs)):
  383. print("--Write Progress: " + str(i) + "/" + str(len(raw_imgs)))
  384. # print(image_idx_arr[i], img_name_dic[image_idx_arr[i]][1], img_name_dic[image_idx_arr[i]][2], img_name_dic[image_idx_arr[i]][3])
  385. ## -------------- 用于对变异后的图片划线 ----------------
  386. gen_img_deprocessed = draw_arrow(deprocess_image(muted_imgs[i],(720,1280,3)), float(img_name_dic[filelist[i]][3]) * math.pi / 2, 10, 1)
  387. imsave(os.path.join(sys.path[0], 'diff_imgs_new/seed') + str(img_name_dic[filelist[i]][4]) + '_town0' + str(seed_town_dic[int(img_name_dic[filelist[i]][4])]) +
  388. '/' + model_dic[int(img_name_dic[filelist[i]][5])] + '/' + filelist[i], gen_img_deprocessed)
  389. ## -------------- 用于对原图片划线 ---------------------
  390. gen_img_deprocessed = draw_arrow(deprocess_image(raw_imgs[i],(720,1280,3)), float(img_name_dic[filelist[i]][2]) * math.pi / 2, 10, 0)
  391. imsave(os.path.join(sys.path[0], 'diff_imgs_new/seed') + str(img_name_dic[filelist[i]][4]) + '_town0' + str(seed_town_dic[int(img_name_dic[filelist[i]][4])]) +
  392. '/' + model_dic[int(img_name_dic[filelist[i]][5])] + '/' + filelist[i][:-4] + '_' + img_name_dic[filelist[i]][0], gen_img_deprocessed)
  393. print("---IMG WRITE COMPLETE---")
  394. return
  395. def generate_arrowed_pics(input_path, dir_name, color):
  396. img_path = input_path + 'center/'
  397. csv_path = input_path + dir_name + '.csv'
  398. output_path = input_path + 'arrowed_out/'
  399. if not isinstance(color, int):
  400. color = int(color)
  401. print("---IMG READ---")
  402. img_data = []
  403. img_name_dic = process_csv(csv_path)
  404. filelist = list(img_name_dic.keys())
  405. for img_name in filelist:
  406. raw_img = preprocess_image(img_path + img_name,(720, 1280))
  407. img_data.append(raw_img)
  408. print("---IMG READ COMPLETE---")
  409. print("---CLEAR OUTPUT FOLDER---")
  410. for filename in os.listdir(output_path):
  411. file_path = os.path.join(output_path, filename)
  412. os.remove(file_path)
  413. print("---IMG WRITE---")
  414. imsave = imageio.imsave
  415. for i in range(len(img_data)):
  416. print("--Write Progress: " + str(i + 1) + "/" + str(len(img_data)))
  417. ## -------------- 用于对图片划线 ---------------------
  418. gen_img_deprocessed = draw_arrow(deprocess_image(img_data[i],(720, 1280, 3)), float(img_name_dic[filelist[i]][0]) * math.pi / 2, 10, color)
  419. imsave(output_path + filelist[i], gen_img_deprocessed)
  420. print("---IMG WRITE COMPLETE---")
  421. return
  422. def convertToGif(input_path):
  423. images = []
  424. png_files = find_files(input_path, '.png')
  425. png_files.sort()
  426. i = 0
  427. # 按顺序打开一系列图片
  428. for filename in png_files:
  429. img = Image.open(filename)
  430. images.append(img)
  431. # i += 1
  432. # if i >= 115:
  433. # break
  434. # 将图片转换为GIF动图并保存
  435. images[0].save(input_path + "result.gif", save_all=True, append_images=images[1:], duration=100, loop=0)
  436. # CUDNN 11.2 CUDA 8.1
  437. if __name__ == '__main__':
  438. description = ("Steering-curve script.")
  439. parser = argparse.ArgumentParser(description=description)
  440. parser.add_argument('--model', type=str, default='')
  441. parser.add_argument('--type', type=str, default='ori_gif')
  442. # parser.add_argument('--name', type=str, default='')
  443. args = parser.parse_args()
  444. if args.type == 'ori_gif':
  445. convertToGif('/home/vangogh/software/FuzzScene/code/scenario_runner-0.9.13/_out/center/')
  446. else:
  447. model_predict(args.model)
  448. generate_arrowed_pics('./scenario_runner-0.9.13/_out/', 'predict_result', args.type)
  449. convertToGif('./scenario_runner-0.9.13/_out/arrowed_out/')
  450. print("[" + os.path.basename(__file__) + ", Line " + str(sys._getframe().f_lineno) + ", " + sys._getframe().f_code.co_name + "] ", "# Generate Arrowed GIF Over")
  451. # main()
  452. # convertToPDF()
  453. # mergeFile()
  454. # files_process("d:/BaiduNetdiskDownload/selected_diff_imgs", ".png")
  455. # generate_arrowed_pics('./scenario_runner-0.9.13/', 'predict_result', 1)
  456. # convertToGif(sys.path[0] + '/videos/seed1')
  457. # convertToGif(sys.path[0] + '/videos/seed2')