123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- import os
- import sys
- from datetime import datetime
- import traceback
- import json
- import math
- import pandas as pd
- import subprocess
- import time
- import multiprocessing
- from functools import partial
- import warnings
- warnings.filterwarnings("ignore")
- from openx import Scenario, formatThree, formatTwo, change_CDATA
- import osgbGenerator
- from utils import smooth_1, get_coordinate_cow, get_map_name, reverse_gyb_gps
- class Batchrun:
- def __init__(self, path, keyFileName):
- """"初始化方法"""
- self.path = path
- self.keyFileName = keyFileName
- def getFile(self, path, keyFileName):
- '''
- 获得可以进行轨迹提取的所有片段文件夹路径
-
- Parameters
- ----------
- path : TYPE
- DESCRIPTION.
-
- Returns
- -------
- FileList : TYPE
- 可以进行轨迹提取的所有片段文件夹路径.
-
- '''
- files = os.listdir(path)
-
- FileList = []
- if keyFileName not in files:
- for name in files:
- if os.path.isdir(path +'/'+name):
- FileList.extend(self.getFile(path +'/'+name, keyFileName))
- else:
- FileList.append(path)
- FileList = list(set(FileList))
-
- return FileList
-
-
- def generateScenarios_raw(self, absPath, param):
- '''
- 原始自然驾驶场景还原
- '''
-
-
- posPath = os.path.join(absPath, 'pos.csv')
- posdata = pd.read_csv(posPath)
-
- offset_x_gyb = -446403.548
- offset_y_gyb = -4403799.286
- offset_x_thq = -456256.260152
- offset_y_thq = -4397809.886833
- offset_x_glyt = -457000
- offset_y_glyt = -4400000
-
- offset_h = 0
- thqPath = '/root/rosmerge/resource/thq_path.csv'
-
- thqdata = pd.read_csv(thqPath)
- thqdata['posX'] -= offset_x_thq
- thqdata['posY'] -= offset_y_thq
- gybPath = '/root/rosmerge/resource/gybhl_path.csv'
-
- gybdata = pd.read_csv(gybPath)
- gybdata['posX'] -= offset_x_gyb
- gybdata['posY'] -= offset_y_gyb
- reverse_east, reverse_north = reverse_gyb_gps(gybdata)
- gybdata['East'] = reverse_east
- gybdata['North'] = reverse_north
- glytPath = '/root/rosmerge/resource/glyt_path.csv'
-
- glytdata = pd.read_csv(glytPath)
- glytdata['posX'] -= offset_x_glyt
- glytdata['posY'] -= offset_y_glyt
- init_x = posdata.iloc[0]['East']
- init_y = posdata.iloc[0]['North']
- map_id = get_map_name(init_x, init_y, thqdata, gybdata, glytdata)
-
-
- if map_id == 0:
- offset_x = offset_x_thq
- offset_y = offset_y_thq
- elif map_id == 1:
- new_east, new_north = get_coordinate_cow(posdata)
- posdata['East'] = new_east
- posdata['North'] = new_north
- offset_x = offset_x_gyb
- offset_y = offset_y_gyb
- else:
- offset_x = offset_x_glyt
- offset_y = offset_y_glyt
-
- posdata['Time'] = posdata['Time'].apply(lambda x: (x // 100) * 100)
-
- posdata['altitude'] = posdata['altitude'] - 9.1
- pos_ego = posdata.loc[posdata['ID'] == -1, ['Time', 'East', 'North', 'HeadingAngle', 'altitude', 'Type']]
-
-
- pos_ego = pos_ego.reset_index(drop=True)
-
-
-
- start_time = pos_ego.at[0, 'Time']
- ego_points, gps_time, ego_type = Scenario.getXoscPosition(pos_ego, 'Time', 'East', 'North', 'HeadingAngle', 'altitude','Type', offset_x, offset_y, offset_h,start_time)
-
-
-
- pos_obs = posdata[posdata['Type'] != 10]
-
- pos_obs = pos_obs.loc[pos_obs['ID'] != -1, ['Time', 'ID', 'East', 'North', 'HeadingAngle', 'AbsVel', 'altitude', 'Type']]
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- pos_obs = pos_obs.reset_index(drop=True)
-
- groups = pos_obs.groupby('ID')
- object_points = []
- for key, value in groups:
- if len(value) < 5:
- continue
-
- object_points.append(Scenario.getXoscPosition(value, 'Time', 'East', 'North', 'HeadingAngle', 'altitude', 'Type', offset_x, offset_y, offset_h,start_time))
-
-
- ego_speed = 5
- period = math.ceil(gps_time[-1] - gps_time[0])
- work_mode = 0
- hour = int(absPath.split('/')[-1].split('-')[3])
- if hour + 8 >= 24:
- hour = hour - 16
- else:
- hour = hour + 8
- time_of_day = hour * 3600
-
- if time_of_day >= 64800:
- time_of_day = 64800
- s = Scenario(ego_points, object_points, gps_time, ego_speed, work_mode, period, absPath, time_of_day, map_id)
- filename = absPath + '/simulation'
- files = s.generate(filename)
- change_CDATA(files[0][0])
- print(files)
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- def multiRun(self, path, param):
- files = self.getFile(path, self.keyFileName)
-
- print('程序开始,%s 个数据包' % len(files))
-
- t1 = time.time()
-
- pool = multiprocessing.Pool(processes = 10)
- pfunc = partial(self.generateScenarios_raw, param)
- pool.map(pfunc, files)
-
- pool.close()
- pool.join()
- t2 = time.time()
- print ("程序结束,并行执行时间:%s s" % int(t2 - t1))
-
- def batchRun(self, path, param):
- files = self.getFile(path, self.keyFileName)
-
- print('程序开始,%s 个数据包' % len(files))
-
- for di, absPath in enumerate(sorted(files)):
- print(absPath)
-
- self.generateScenarios_raw(absPath, param)
-
-
-
-
-
-
-
- return None
- def GenerateVideo(self, root_path):
-
- imagelist = self.getFile(root_path, 'image')
- for item in imagelist:
- strs = item.split('/')
- respath = os.path.join(item,"video.mp4")
-
- print('---------------------')
-
- command = "ffmpeg -f image2 -r 10 -pattern_type glob -i '" + item + "/image/*.jpg" + "' -y '" + respath + "'"
- print(command)
- process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
- process.wait()
- if __name__ == "__main__":
- rootPath = sys.argv[1]
-
- a = Batchrun(rootPath, "pos.csv")
- a.batchRun(rootPath, 0)
-
-
-
|