pcd.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import json
  2. import math
  3. import shutil
  4. import os
  5. import subprocess
  6. import pandas as pd
  7. import cv2
  8. import numpy as np
  9. import requests
  10. geocode_base = 'http://restapi.amap.com/v3/geocode/regeo'
  11. def geocode(location):
  12. parameters = {'output': 'json', 'key': 'cdf24f471cc579ba6d5dd1f9b856ee31', 'location': location,
  13. 'extensions': 'base'}
  14. response = requests.get(geocode_base, parameters)
  15. # print('HTTP 请求的状态: %s' % response.status_code)
  16. return response.json()
  17. def convert(locations):
  18. parameters = {'output': 'json', 'key': 'cdf24f471cc579ba6d5dd1f9b856ee31', 'locations': locations,
  19. 'coordsys': 'gps'}
  20. base = 'https://restapi.amap.com/v3/assistant/coordinate/convert'
  21. response_cor = requests.get(base, parameters).json()
  22. result_location = response_cor['locations']
  23. para_location = result_location.replace(';', '|')
  24. response_location = geocode(para_location)
  25. status = response_location['status']
  26. if status == '1':
  27. try:
  28. address = geocode(para_location)['regeocode']['formatted_address']
  29. return status, (result_location, address)
  30. except:
  31. return '0', None
  32. else:
  33. return status, None
  34. def get_position_single(base_df, inset_df):
  35. inset_df['Longitude'] = inset_df['Longitude'].astype('str')
  36. inset_df['Latitude'] = inset_df['Latitude'].astype('str')
  37. inset_df = inset_df[['Longitude', 'Latitude']]
  38. for index, twenty_df in inset_df.groupby(lambda x: math.floor(x / 20)):
  39. twenty_df = twenty_df.T
  40. cor_list = twenty_df.values.tolist()
  41. cor_total = ''
  42. for i, v in enumerate(cor_list[0]):
  43. cor_total += f'{v},{cor_list[1][i]}|'
  44. cor_total = cor_total[:-1]
  45. status, convert_result = convert(cor_total)
  46. if status == '1':
  47. result_cor = convert_result[0]
  48. result_cor_list = result_cor.split(';')
  49. result_cor_list = list(map(lambda x: x.split(','), result_cor_list))
  50. twenty_df = pd.DataFrame(result_cor_list, columns=['Longitude', 'Latitude'])
  51. base_df = pd.concat([base_df, twenty_df]).reset_index(drop=True)
  52. return base_df[1:].reset_index(drop=True)
  53. def draw_points(pic, filename):
  54. f = open(filename, "r")
  55. ff = f.readline()
  56. for i in range(0, len(ff)):
  57. point = ff[i].split()[:4]
  58. point_x = float(point[0])
  59. point_y = -float(point[1])
  60. if math.isnan(point_x):
  61. pass
  62. else:
  63. cv2.circle(pic, (int(800 - point_x * 10), int(400 - point_y * 10)), 0, (250, 250, 250), 0)
  64. def pcd_image(pcd_path):
  65. for root, dirs, files in os.walk(pcd_path):
  66. # create pcd_ascii and pcd_jpg folders
  67. if "pcd" in dirs:
  68. if "jpg" not in dirs:
  69. jpg_path = os.path.join(root, 'jpg')
  70. os.mkdir(jpg_path)
  71. # find .pcd files and do the transfer twice
  72. for file in files:
  73. if "pcd" == root[-3:] and ".pcd" == file[-4:]:
  74. inputFilePath = root + "/" + file
  75. outputFilePath = root[:-3] + "jpg/" + file[:-3] + "jpg"
  76. pic = np.zeros([800, 1600, 3])
  77. draw_points(pic, inputFilePath)
  78. cv2.imwrite(outputFilePath, pic)
  79. else:
  80. break
  81. return jpg_path
  82. def generate_video(img_path, video_name):
  83. if video_name == 'CAMERA0.mp4':
  84. img_name = 'image'
  85. else:
  86. img_name = 'jpg'
  87. image_list = get_file(img_path, img_name)
  88. for item in image_list:
  89. res_path = os.path.join(item, video_name)
  90. if os.path.exists(res_path):
  91. return
  92. all_image = list(map(int, list(map(lambda x: x.split('.')[0], os.listdir(os.path.join(img_path, img_name))))))
  93. all_image.sort(reverse=False)
  94. frame = round(len(all_image) / ((all_image[-1] - all_image[0]) / 1000), 2)
  95. command = f"ffmpeg -f image2 -r {frame} -pattern_type glob -i '" + item + f"/{img_name}/*.jpg" + "' -y '" + res_path + "'"
  96. process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  97. process.wait()
  98. def get_file(path, key_file_name):
  99. files = os.listdir(path)
  100. file_list = []
  101. if key_file_name not in files:
  102. for name in files:
  103. next_path = os.path.join(path, name)
  104. if os.path.isdir(next_path):
  105. file_list.extend(get_file(next_path, key_file_name))
  106. else:
  107. file_list.append(path)
  108. file_list = list(set(file_list))
  109. return file_list
  110. def copy_files(source, target, dir_name):
  111. files_source_path = os.path.join(source, dir_name)
  112. if dir_name == 'simulation':
  113. dir_name = dir_name.upper()
  114. files_target_path = os.path.join(target, dir_name)
  115. if os.path.isdir(files_source_path):
  116. shutil.copytree(files_source_path, files_target_path)
  117. else:
  118. shutil.copy(files_source_path, files_target_path)
  119. return files_target_path
  120. if __name__ == '__main__':
  121. root_path = '/media/hancheng/Simulation4/1013/here_bizhang2_2023-10-13-14-32-23'
  122. output_path = '/media/hancheng/Simulation4/1013/here_bizhang2_2023-10-13-14-32-23/output'
  123. # xodr_list = ['/home/hancheng/simulation_dynamic/od/jiuzhonglu.xodr', '/home/hancheng/simulation_dynamic/od/gaosu.xodr']
  124. # osgb_list = ['/home/hancheng/simulation_dynamic/od/GJBM_20km_0822.opt.osgb', '/home/hancheng/simulation_dynamic/od/gaosu.osgb']
  125. source_files_list = get_file(root_path, 'pos.csv')
  126. if not os.path.exists(output_path):
  127. os.mkdir(output_path)
  128. for single_path in source_files_list:
  129. # if not os.path.exists(os.path.join(single_path, 'simulation')):
  130. # continue
  131. # json_path = os.path.join(single_path, 'autoSliceRes.json')
  132. # with open(json_path) as f:
  133. # sc_json = json.load(f)
  134. # road_section = sc_json['road_section']
  135. #target_path = os.path.join(output_path, os.path.basename(single_path))
  136. # if not os.path.exists(target_path):
  137. # os.mkdir(target_path)
  138. # else:
  139. # shutil.rmtree(target_path)
  140. #
  141. # # 原始照片/视频
  142. # copy_files(single_path, target_path, 'CAMERA0')
  143. # 点云图片/视频
  144. #target_lidar_path = copy_files(single_path, target_path, 'LIDAR0')
  145. #jpg_path = pcd_image(target_lidar_path)
  146. generate_video('/media/hancheng/Simulation4/1013/here_bizhang2_2023-10-13-14-32-23/output/here_bizhang2_2023-10-13-14-32-23/LIDAR0', 'LIDAR0.mp4')
  147. #shutil.rmtree(jpg_path)
  148. # 场景以及道路文件
  149. # target_sc_path = copy_files(single_path, target_path, 'simulation')
  150. # simulation_video = (os.path.join(target_sc_path, 'simulation_Camera_1.mp4'))
  151. # os.rename(simulation_video, simulation_video.replace('_Camera_1', ''))
  152. # xodr_path = os.path.join(target_sc_path, "SIMULATION.xodr")
  153. # osgb_path = os.path.join(target_sc_path, "SIMULATION.osgb")
  154. # shutil.copy(xodr_list[road_section-1], xodr_path)
  155. # shutil.copy(osgb_list[road_section-1], osgb_path)
  156. # xosc_path = os.path.join(target_sc_path, 'simulation.xosc')
  157. # xosc_upper = os.path.join(target_sc_path, 'SIMULATION.xosc')
  158. # os.rename(xosc_path, xosc_upper)
  159. # 数据以及说明文件
  160. #copy_files(single_path, target_path, 'label.json')
  161. # data_path = os.path.join(target_path, 'data.csv')
  162. # autocan = pd.read_csv(os.path.join(os.path.join(single_path, 'autocan.csv')))
  163. # gps_origin = pd.read_csv(os.path.join(os.path.join(single_path, 'gps.csv')))
  164. # origin_data = pd.DataFrame(columns=['date', 'time', 'location', 'lon', 'lat'], index=[0])
  165. # gps = get_position_single(origin_data, gps_origin)
  166. # data_df = pd.DataFrame(
  167. # columns=['Time', 'FrameID', 'Velocity', 'AcceleratorPedalPos', 'BrakePedalSignal',
  168. # 'SteeringAngle',
  169. # 'GPSStatus', 'gdlng', 'gdlat'])
  170. # data_df['Time'] = autocan['Time']
  171. # data_df['FrameID'] = autocan['FrameID']
  172. # data_df['Velocity'] = autocan['Velocity']
  173. # data_df['AcceleratorPedalPos'] = autocan['AcceleratorPedalPos']
  174. # data_df['BrakePedalSignal'] = autocan['BrakePedalSignal']
  175. # data_df['SteeringAngle'] = autocan['SteeringAngle']
  176. # data_df['GPSStatus'] = gps_origin['GPSStatus']
  177. # data_df['gdlng'] = gps['Longitude']
  178. # data_df['gdlat'] = gps['Latitude']
  179. # data_df.reset_index(drop=False)
  180. # data_df.to_csv(data_path)
  181. print(f'{single_path} done!')