kinglong_simulation.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import time
  4. import oss2
  5. import xml.etree.ElementTree as ET
  6. from rosbag import Bag, Compression
  7. import shutil
  8. import docker
  9. import bagtocsv_orig
  10. import bagtocsv_hmi
  11. import get_drive
  12. import parse_jinlong_image
  13. import pcdtovideo_jilong_overlook
  14. import pcdtovideo_jilong_forwardlook
  15. key1 = 'kinglong/'
  16. path1 = '/scenarios/'
  17. sleep_time = 5 # 每多少秒扫描一次
  18. def move_xosc_before_simulation(root_path1, root_path2):
  19. try:
  20. xodr1 = 'thq.xodr'
  21. xodr2 = 'OpenDRIVE.xodr'
  22. xodr3 = 'gelan.xodr'
  23. osgb1 = 'thq.opt.osgb'
  24. osgb2 = 'GJBM_20km_0822.opt.osgb'
  25. osgb3 = 'gelan.opt.osgb'
  26. tree1 = ET.parse(root_path1 + 'scenario_orig.xosc')
  27. root1 = tree1.getroot()
  28. for node0 in root1:
  29. if node0.tag == 'RoadNetwork':
  30. for node1 in node0:
  31. # 打印根元素的标签名
  32. src_file_prefix = '/root/rosmerge/resource/'
  33. if 'LogicFile' == node1.tag:
  34. if xodr1 in node1.get('filepath'):
  35. dst_file = root_path1 + xodr1
  36. shutil.copy(src_file_prefix + xodr1, dst_file)
  37. node1.set('filepath', dst_file)
  38. if xodr2 in node1.get('filepath'):
  39. dst_file = root_path1 + xodr2
  40. shutil.copy(src_file_prefix + xodr2, dst_file)
  41. node1.set('filepath', dst_file)
  42. if xodr3 in node1.get('filepath'):
  43. dst_file = root_path1 + xodr3
  44. shutil.copy(src_file_prefix + xodr3, dst_file)
  45. node1.set('filepath', dst_file)
  46. if 'SceneGraphFile' == node1.tag:
  47. if osgb1 in node1.get('filepath'):
  48. dst_file = root_path1 + osgb1
  49. shutil.copy(src_file_prefix + osgb1, dst_file)
  50. node1.set('filepath', dst_file)
  51. if osgb2 in node1.get('filepath'):
  52. dst_file = root_path1 + osgb2
  53. shutil.copy(src_file_prefix + osgb2, dst_file)
  54. node1.set('filepath', dst_file)
  55. if osgb3 in node1.get('filepath'):
  56. dst_file = root_path1 + osgb3
  57. shutil.copy(src_file_prefix + osgb3, dst_file)
  58. node1.set('filepath', dst_file)
  59. if node0.tag == 'Entities':
  60. for node1 in node0:
  61. if node1.get("name") == 'Ego':
  62. for node2 in node1:
  63. if node2.tag == 'Vehicle':
  64. node2.set('name', 'KingLong_snow white')
  65. tree1.write(root_path1 + 'openx0.xosc')
  66. tree2 = ET.parse(root_path2 + 'scenario_hmi.xosc')
  67. root2 = tree2.getroot()
  68. for node0 in root2:
  69. if node0.tag == 'RoadNetwork':
  70. for node1 in node0:
  71. # 打印根元素的标签名
  72. src_file_prefix = '/root/rosmerge/resource/'
  73. if 'LogicFile' == node1.tag:
  74. if xodr1 in node1.get('filepath'):
  75. dst_file = root_path2 + xodr1
  76. shutil.copy(src_file_prefix + xodr1, dst_file)
  77. node1.set('filepath', dst_file)
  78. if xodr2 in node1.get('filepath'):
  79. dst_file = root_path2 + xodr2
  80. shutil.copy(src_file_prefix + xodr2, dst_file)
  81. node1.set('filepath', dst_file)
  82. if xodr3 in node1.get('filepath'):
  83. dst_file = root_path2 + xodr3
  84. shutil.copy(src_file_prefix + xodr3, dst_file)
  85. node1.set('filepath', dst_file)
  86. if 'SceneGraphFile' == node1.tag:
  87. if osgb1 in node1.get('filepath'):
  88. dst_file = root_path2 + osgb1
  89. shutil.copy(src_file_prefix + osgb1, dst_file)
  90. node1.set('filepath', dst_file)
  91. if osgb2 in node1.get('filepath'):
  92. dst_file = root_path2 + osgb2
  93. shutil.copy(src_file_prefix + osgb2, dst_file)
  94. node1.set('filepath', dst_file)
  95. if osgb3 in node1.get('filepath'):
  96. dst_file = root_path2 + osgb3
  97. shutil.copy(src_file_prefix + osgb3, dst_file)
  98. node1.set('filepath', dst_file)
  99. if node0.tag == 'Entities':
  100. for node1 in node0:
  101. if node1.get("name") == 'Ego':
  102. for node2 in node1:
  103. if node2.tag == 'Vehicle':
  104. node2.set('name', 'KingLong_snow white')
  105. tree2.write(root_path2 + 'openx0.xosc')
  106. except Exception as e:
  107. print "修改xosc报错: %s" % str(e)
  108. # print(f'修改xosc报错:{e}')
  109. def delete_files_in_dir(dir_path):
  110. # 遍历目录下的所有文件和子目录
  111. for root, dirs, files in os.walk(dir_path):
  112. for file in files:
  113. file_path = os.path.join(root, file)
  114. try:
  115. os.remove(file_path)
  116. except OSError as e:
  117. print("删除文件时出错:", file_path, "-", e.strerror)
  118. # 扫描目录上传mp4
  119. def upload_simulation(parse_prefix, mp41, mp42):
  120. try:
  121. bucket.put_object_from_file(parse_prefix + 'scenario_orig.mp4', mp41)
  122. print '上传仿真视频到 %s' % parse_prefix + 'scenario_orig.mp4'
  123. bucket.put_object_from_file(parse_prefix + 'scenario_hmi.mp4', mp42)
  124. print '上传仿真视频到 %s' % parse_prefix + 'scenario_hmi.mp4'
  125. shutil.rmtree(path1)
  126. except Exception as e:
  127. print "上传视频报错: %s" % str(e)
  128. # print(f'上传视频报错{e}')
  129. def simulation(parse_prefix, mp41, mp42):
  130. try:
  131. os.system("docker start vtd")
  132. # 实例化Docker客户端
  133. client = docker.from_env()
  134. while True:
  135. time.sleep(5)
  136. # 获取容器列表
  137. containers = client.containers.list()
  138. run = False
  139. # 打印容器列表
  140. for container in containers:
  141. if 'vtd' == container.name:
  142. run = True
  143. break
  144. if not run:
  145. break
  146. time.sleep(5)
  147. upload_simulation(parse_prefix, mp41, mp42)
  148. except Exception as e:
  149. print "生成仿真视频报错: %s" % str(e)
  150. # print(f'生成仿真视频报错{e}')
  151. def is_upload_completed(bucket, prefix):
  152. target_number = str(prefix).split('_')[-1][:-1]
  153. count = 0
  154. for obj in oss2.ObjectIterator(bucket, prefix=prefix):
  155. if obj.key != prefix:
  156. count += 1
  157. return int(count) == int(target_number)
  158. '''
  159. cname:http://open-bucket.oss.icvdc.com
  160. 内网endpoint: oss-cn-beijing-gqzl-d01-a.ops.gqzl-cloud.com
  161. oss桶名: open-bucket
  162. keyid:n8glvFGS25MrLY7j
  163. secret:xZ2Fozoarpfw0z28FUhtg8cu0yDc5d
  164. '''
  165. # ------- 获取合并之后的bag包,解析出csv -------
  166. if __name__ == '__main__':
  167. # 1 创建阿里云对象
  168. auth = oss2.Auth('n8glvFGS25MrLY7j', 'xZ2Fozoarpfw0z28FUhtg8cu0yDc5d')
  169. # cname = 'http://open-bucket.oss.icvdc.com'
  170. # bucket = oss2.Bucket(auth, cname, 'open-bucket', is_cname=True)
  171. endpoint = 'oss-cn-beijing-gqzl-d01-a.ops.gqzl-cloud.com'
  172. bucket = oss2.Bucket(auth, endpoint, 'open-bucket')
  173. while True:
  174. local_delete_list = []
  175. oss_delete_list = []
  176. prefix_list = []
  177. # 2 获取已经上传完成的所有目录并分组
  178. all_prefix_set = set()
  179. for obj1 in oss2.ObjectIterator(bucket, prefix=key1):
  180. # 获取csv
  181. if 'data_parse' in str(obj1.key) and str(obj1.key).count('/') == 4:
  182. all_prefix_set.add('/'.join(str(obj1.key).split('/')[:-1]) + '/')
  183. for obj2_key in all_prefix_set:
  184. xosc1_done = False
  185. xosc2_done = False
  186. mp41_done = False
  187. mp42_done = False
  188. for obj3 in oss2.ObjectIterator(bucket, prefix=obj2_key):
  189. if '/scenario_orig.xosc' in str(obj3.key):
  190. xosc1_done = True
  191. if '/scenario_hmi.xosc' in str(obj3.key):
  192. xosc2_done = True
  193. if '/scenario_orig.mp4' in str(obj3.key):
  194. mp41_done = True
  195. if '/scenario_hmi.mp4' in str(obj3.key):
  196. mp42_done = True
  197. if not xosc1_done or not xosc2_done:
  198. continue
  199. if mp41_done and mp42_done:
  200. continue
  201. # print(f'需要生成xosc:{obj2_key}')
  202. print "需要生成仿真视频: %s" % obj2_key
  203. parse_prefix_full = obj2_key
  204. local_dir_full = path1 + parse_prefix_full
  205. if not os.path.exists(local_dir_full):
  206. os.makedirs(local_dir_full)
  207. # 下载两个csv
  208. root_path1 = local_dir_full + 'orig/'
  209. root_path2 = local_dir_full + 'hmi/'
  210. if not os.path.exists(root_path1):
  211. os.makedirs(root_path1)
  212. if not os.path.exists(root_path2):
  213. os.makedirs(root_path2)
  214. bucket.get_object_to_file(parse_prefix_full + 'scenario_orig.xosc', root_path1 + 'scenario_orig.xosc')
  215. bucket.get_object_to_file(parse_prefix_full + 'scenario_hmi.xosc', root_path2 + 'scenario_hmi.xosc')
  216. local_delete_list.append(root_path1 + 'scenario_orig.xosc')
  217. local_delete_list.append(root_path2 + 'scenario_hmi.xosc')
  218. move_xosc_before_simulation(root_path1, root_path2)
  219. simulation(parse_prefix_full, root_path1 + 'simulation.mp4', root_path2 + 'simulation.mp4')
  220. # 删除本地临时文件
  221. if len(local_delete_list) > 0:
  222. for local_delete in local_delete_list:
  223. try:
  224. os.remove(local_delete)
  225. except Exception as e:
  226. print "删除本地临时文件报错: %s" % str(e)
  227. # print(f'删除本地临时文件报错{e}')
  228. time.sleep(sleep_time)