simulation-pjisuv.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import time
  4. import oss2
  5. import xml.etree.ElementTree as ET
  6. import shutil
  7. import docker
  8. import logging
  9. key1 = 'pjisuv/'
  10. path1 = '/scenarios2/'
  11. path2 = '/mnt/disk001/dcl_data_process/src/python2/pjisuv/'
  12. path3 = '/mnt/disk001/simulation_pjisuv/'
  13. logging.basicConfig(filename=path2 + 'log/simulation.log', level=logging.INFO,
  14. format='%(asctime)s - %(levelname)s - %(message)s')
  15. sleep_time = 60 # 每多少秒扫描一次
  16. def move_xosc_before_simulation(root_path):
  17. try:
  18. xodr = 'OpenDRIVE2_add_stop_line_marking_20231229.xodr'
  19. osgb = 'jinlong_all_addtrafficlight_20240226_5_guazai_2.opt.osgb'
  20. tree1 = ET.parse(root_path1 + 'scenario_orig.xosc')
  21. root1 = tree1.getroot()
  22. for node0 in root1:
  23. if node0.tag == 'RoadNetwork':
  24. for node1 in node0:
  25. # 打印根元素的标签名
  26. src_file_prefix = path3
  27. if 'LogicFile' == node1.tag:
  28. if xodr in node1.get('filepath'):
  29. dst_file = root_path1 + xodr
  30. shutil.copy(src_file_prefix + xodr, dst_file)
  31. node1.set('filepath', dst_file)
  32. if xodr in node1.get('filepath'):
  33. dst_file = root_path1 + xodr
  34. shutil.copy(src_file_prefix + xodr, dst_file)
  35. node1.set('filepath', dst_file)
  36. if xodr in node1.get('filepath'):
  37. dst_file = root_path1 + xodr
  38. shutil.copy(src_file_prefix + xodr, dst_file)
  39. node1.set('filepath', dst_file)
  40. if 'SceneGraphFile' == node1.tag:
  41. if osgb in node1.get('filepath'):
  42. dst_file = root_path1 + osgb
  43. shutil.copy(src_file_prefix + osgb, dst_file)
  44. node1.set('filepath', dst_file)
  45. if osgb in node1.get('filepath'):
  46. dst_file = root_path1 + osgb
  47. shutil.copy(src_file_prefix + osgb, dst_file)
  48. node1.set('filepath', dst_file)
  49. if osgb in node1.get('filepath'):
  50. dst_file = root_path1 + osgb
  51. shutil.copy(src_file_prefix + osgb, dst_file)
  52. node1.set('filepath', dst_file)
  53. if node0.tag == 'Entities':
  54. for node1 in node0:
  55. if node1.get("name") == 'Ego':
  56. for node2 in node1:
  57. if node2.tag == 'Vehicle':
  58. node2.set('name', 'Pji_Utility_Vehicle')
  59. tree1.write(root_path1 + 'openx0.xosc')
  60. except Exception as e:
  61. logging.exception("修改xosc报错: %s" % str(e))
  62. def upload_simulation(parse_prefix, mp41):
  63. try:
  64. bucket.put_object_from_file(parse_prefix + 'scenario_orig.mp4', mp41)
  65. logging.info('上传仿真视频到 %s' % parse_prefix + 'scenario_orig.mp4')
  66. # bucket.put_object_from_file(parse_prefix + 'scenario_hmi.mp4', mp42)
  67. # logging.info('上传仿真视频到 %s' % parse_prefix + 'scenario_hmi.mp4')
  68. shutil.rmtree(path1) # 仿真完就删除
  69. except Exception as e:
  70. logging.exception("上传视频报错 %s" % str(e))
  71. def simulation(parse_prefix, mp41):
  72. try:
  73. os.system("docker start vtd2")
  74. # 实例化Docker客户端
  75. client = docker.from_env()
  76. while True:
  77. time.sleep(5)
  78. # 获取容器列表
  79. containers = client.containers.list()
  80. run = False
  81. # 打印容器列表
  82. for container in containers:
  83. if 'vtd2' == container.name:
  84. run = True
  85. break
  86. if not run:
  87. break
  88. time.sleep(5)
  89. upload_simulation(parse_prefix, mp41)
  90. except Exception as e1:
  91. logging.exception("生成仿真视频报错 %s" % str(e1))
  92. def is_upload_completed(bucket, prefix):
  93. target_number = str(prefix).split('_')[-1][:-1]
  94. count = 0
  95. for obj in oss2.ObjectIterator(bucket, prefix=prefix):
  96. if obj.key != prefix:
  97. count += 1
  98. return int(count) == int(target_number)
  99. '''
  100. cname:http://open-bucket.oss.icvdc.com
  101. keyid:n8glvFGS25MrLY7j
  102. secret:xZ2Fozoarpfw0z28FUhtg8cu0yDc5d
  103. oss桶名: oss://open-bucket
  104. oss桶名: open-bucket
  105. 内网endpoint: oss-cn-beijing-gqzl-d01-a.ops.gqzl-cloud.com
  106. '''
  107. # ------- 获取合并之后的bag包,解析出csv -------
  108. if __name__ == '__main__':
  109. # 1 创建阿里云对象
  110. auth = oss2.Auth('n8glvFGS25MrLY7j', 'xZ2Fozoarpfw0z28FUhtg8cu0yDc5d')
  111. # cname = 'http://open-bucket.oss.icvdc.com'
  112. # bucket = oss2.Bucket(auth, cname, 'open-bucket', is_cname=True)
  113. endpoint = 'oss-cn-beijing-gqzl-d01-a.ops.gqzl-cloud.com'
  114. bucket = oss2.Bucket(auth, endpoint, 'open-bucket')
  115. while True:
  116. logging.info("开始新一轮扫描")
  117. local_delete_list = []
  118. oss_delete_list = []
  119. prefix_list = []
  120. # 2 获取已经上传完成的所有目录并分组
  121. all_prefix_set = set()
  122. for obj1 in oss2.ObjectIterator(bucket, prefix=key1):
  123. # 获取csv
  124. if 'data_parse' in str(obj1.key) and str(obj1.key).count('/') == 4:
  125. all_prefix_set.add('/'.join(str(obj1.key).split('/')[:-1]) + '/')
  126. for obj2_key in all_prefix_set:
  127. xosc1_done = False
  128. mp41_done = False
  129. for obj3 in oss2.ObjectIterator(bucket, prefix=obj2_key):
  130. if '/scenario_orig.xosc' in str(obj3.key):
  131. xosc1_done = True
  132. if '/scenario_orig.mp4' in str(obj3.key):
  133. mp41_done = True
  134. if not xosc1_done or mp41_done:
  135. continue
  136. logging.info("需要生成仿真视频: %s" % obj2_key)
  137. parse_prefix_full = obj2_key
  138. local_dir_full = path1 + parse_prefix_full
  139. if not os.path.exists(local_dir_full):
  140. os.makedirs(local_dir_full)
  141. # 下载两个csv
  142. root_path1 = local_dir_full + 'orig/'
  143. if not os.path.exists(root_path1):
  144. os.makedirs(root_path1)
  145. bucket.get_object_to_file(parse_prefix_full + 'scenario_orig.xosc', root_path1 + 'scenario_orig.xosc')
  146. local_delete_list.append(root_path1 + 'scenario_orig.xosc')
  147. move_xosc_before_simulation(root_path1)
  148. simulation(parse_prefix_full, root_path1 + 'simulation.mp4')
  149. # 删除本地临时文件
  150. if len(local_delete_list) > 0:
  151. for local_delete in local_delete_list:
  152. try:
  153. os.remove(local_delete)
  154. except Exception as e:
  155. logging.exception("删除本地临时文件报错: %s" % str(e))
  156. time.sleep(sleep_time)