Python中动画显示与gif生成

news2024/12/25 14:30:32

1. 动画生成

主要使用的是 matplotlib.animation ,具体示例如下:

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
fig, ax = plt.subplots()
t = np.linspace(0, 3, 40)
g = -9.81
v0 = 12
z = g * t**2 / 2 + v0 * t

v02 = 5
z2 = g * t**2 / 2 + v02 * t

scat = ax.scatter(t[0], z[0], c="b", s=5, label=f'v0 = {v0} m/s')
line2 = ax.plot(t[0], z2[0], label=f'v0 = {v02} m/s')[0]
ax.set(xlim=[0, 3], ylim=[-4, 10], xlabel='Time [s]', ylabel='Z [m]')
ax.legend()


def update(frame):
    # for each frame, update the data stored on each artist.
    x = t[:frame]
    y = z[:frame]
    # update the scatter plot:
    data = np.stack([x, y]).T
    scat.set_offsets(data)
    # update the line plot:
    line2.set_xdata(t[:frame])
    line2.set_ydata(z2[:frame])
    return (scat, line2)


anim = animation.FuncAnimation(fig=fig, func=update, frames=40, interval=30)
anim.save("test.gif", writer='pillow') # save a gif
plt.show()

 2. merge_asof 用法

merge_asof()是pandas库中的一个函数,可以根据两个dataframes中的时间戳列,基于最接近的时间戳将两个dataframes进行合并。
这个函数的作用类似于SQL中加入的操作,但是可以处理时间戳列,因此非常适用于金融数据等时间序列数据的合并。
此方法用于执行asof合并,这类似于left-join,除了我们匹配最近的键而不是相等的键。两个DataFrame都必须按键排序。

具体用法:

pandas.merge_asof

pandas.merge_asof(leftrighton=Noneleft_on=Noneright_on=Noneleft_index=Falseright_index=Falseby=Noneleft_by=Noneright_by=Nonesuffixes=('_x', '_y')tolerance=Noneallow_exact_matches=Truedirection='backward')

Perform a merge by key distance.

This is similar to a left-join except that we match on nearest key rather than equal keys. Both DataFrames must be sorted by the key.

For each row in the left DataFrame:

  • A “backward” search selects the last row in the right DataFrame whose ‘on’ key is less than or equal to the left’s key.【key:右侧dataframe对齐左侧数据最后看齐】

  • A “forward” search selects the first row in the right DataFrame whose ‘on’ key is greater than or equal to the left’s key.key:右侧dataframe对齐左侧数据最前看齐】

  • A “nearest” search selects the row in the right DataFrame whose ‘on’ key is closest in absolute distance to the left’s key.key:右侧dataframe对齐左侧数据朝最近看齐】

  • 参数:

    left, right: DataFrame
    on:标签,要加入的字段名称。必须在两个DataFrame中都找到。
    left_on:标签,要在左侧DataFrame中加入的字段名称。
    right_on:标签,要在右侧DataFrame中加入的字段名称。
    left_index:布尔值,使用左侧DataFrame的索引作为连接键。
    right_index:布尔值,使用正确的DataFrame的索引作为连接键。

下面是上述方法的实现和一些示例

# importing package 
import pandas 
  
# creating data 
left = pandas.DataFrame({'a':[1, 5, 10],  
                         'left_val':['a', 'b', 'c']}) 
  
right = pandas.DataFrame({'a':[1, 2, 3, 6, 7], 
                          'right_val':[1, 2, 3, 6, 7]}) 
  
# view data 
print(left) 
print(right) 
  
# applying merge_asof on data 
print(pandas.merge_asof(left, right, on='a',  
                        direction='forward')) 
print(pandas.merge_asof(left, right, on='a', 
                        direction='nearest'))

3. 具体应用

import imageio
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import pandas as pd
import os
from os import listdir
import numpy as np
import scipy.sparse as ss
import random
import datetime
import json
LA = np.linalg
rng = np.random

uss_unit = 100
angle_unit = 4096.0

def gen_fig():
    fig = plt.figure()
    ax = plt.axes(xlim=(10, 80), ylim=(-80, 20))
    line, = ax.plot([], [], 'r-')
    return fig, line

def update(line, data):
    # 更新点的位置
    line.set_data(data[0], data[1])
    return line
 
def gen_data(input_data,xx, yy):
    # 生成动态数据
    for i in range(len(input_data)):
        yield xx[:i], yy[:i]
 
def Q4ToMatrix2(x,y,z,w,xc,yc,xv,yv):
    
    R = np.array([ [1-2*y*y-2*z*z, 2*x*y-2*w*z, 2*x*z+2*w*y],
                  [2*x*y+2*w*z, 1-2*x*x-2*z*z, 2*y*z-2*w*x],
                  [2*x*z-2*w*y, 2*y*z+2*w*x,1-2*x*x-2*y*y]])
    Rv = np.linalg.inv(R)
    pt = np.array([xc,yc,0]).reshape(-1,1) # uss corner point
    pt_v = np.array([xv,yv,0]).reshape(-1,1) # vichle center point
    pt_ = np.dot(Rv,pt)
    pt_new = pt_ + pt_v
    
    # t = Rv[2,0] * xc + Rv[2,1] * yc + Rv[2][2]
    
    # pt_new_x = (Rv[0,0] * xc + Rv[0,1] * yc + Rv[0,2]) / t
    # pt_new_y = (Rv[1,0] * xc + Rv[1,1] * yc + Rv[1,2]) / t
    
    pt_new_x = pt_new[0,0]
    pt_new_y = pt_new[1,0]
    
    return [pt_new_x, pt_new_y]


def Q4ToMatrix(x, y, z, w, xc, yc, xv, yv):
    # 根据四元数构建旋转矩阵
    R = np.array([
        [1 - 2 * y * y - 2 * z * z, 2 * x * y - 2 * w * z, 2 * x * z + 2 * w * y],
        [2 * x * y + 2 * w * z, 1 - 2 * x * x - 2 * z * z, 2 * y * z - 2 * w * x],
        [2 * x * z - 2 * w * y, 2 * y * z + 2 * w * x, 1 - 2 * x * x - 2 * y * y]
    ])

    # xc, yc 是某个点在原坐标系中的坐标,xv, yv 是该坐标系相对于目标坐标系的平移量
    pt = np.array([xc, yc, 0])  # 原始点坐标
    pt_v = np.array([xv, yv, 0])  # 平移向量

    # 应用旋转矩阵和平移向量
    pt_transformed = R.dot(pt) + pt_v

    return [pt_transformed[0], pt_transformed[1]]
    

def process_json(file_name):
    # 读取并解析JSON文件
    with open(file_name, 'r') as file:
        data = json.load(file)
    data_vcs = []
    data_uss = []
    data_ipm = []
    for k,v in data.items():
        if "loc_vcs" in v.keys():
            data_vcs.append((k,v))
        elif "ipm_pt" in v.keys():
            dict_list = v["ipm_pt"]
            if len(dict_list) > 0:
                data_ipm.append((k,v))       
        else:
            data_uss.append((k,v))
            
    # 进一步解析IPM 车位信息, 生成dataframe       
    ts_ipm_list = []        
    idx_slot_list = []
    idx_corner_list = []
    pt_x_list = []
    pt_y_list = []
    pt_z_list = []
    pt_score_list = []
    pt_vis_list = []
    pt_ort_x_list = []
    pt_ort_y_list = []
    for ipm in data_ipm:
        ts = int(ipm[0])
        for idx_slot,ipm_dcts in enumerate(ipm[1]["ipm_pt"]): # 10 or more or less
            ipm_list = ipm_dcts["ipm_pt_slot_points"]
            for idx_corner,ipm_pts in enumerate(ipm_list):    # total 4 corner points
                pt_x  = ipm_pts["point"]["x"]
                pt_y  = ipm_pts["point"]["y"]
                pt_z  = ipm_pts["point"]["z"]
                pt_score = ipm_pts["score"]
                pt_vis   = ipm_pts["visibility"]
                pt_ort_x = ipm_pts["point_orientation"]["direct_x"]
                pt_ort_y = ipm_pts["point_orientation"]["direct_y"]
            
                ts_ipm_list.append(ts)
                idx_slot_list.append(idx_slot)
                idx_corner_list.append(idx_corner)
                pt_x_list.append(pt_x)
                pt_y_list.append(pt_y)
                pt_z_list.append(pt_z)
                pt_score_list.append(pt_score)
                pt_vis_list.append(pt_vis)
                pt_ort_x_list.append(pt_ort_x)
                pt_ort_y_list.append(pt_ort_y)
                
    df_dict_ipm = dict()            
    df_dict_ipm["timestamp"]  = ts_ipm_list
    df_dict_ipm["idx_slot"]   = idx_slot_list
    df_dict_ipm["idx_corner"] = idx_corner_list
    df_dict_ipm["ipm_vcs_x"]  = pt_x_list
    df_dict_ipm["ipm_vcs_y"]  = pt_y_list
    df_dict_ipm["ipm_vcs_z"]  = pt_z_list
    df_dict_ipm["ipm_score"]  = pt_score_list
    
    df_dict_ipm["ipm_vis"]   = pt_vis_list
    df_dict_ipm["ipm_ort_x"] = pt_ort_x_list
    df_dict_ipm["ipm_ort_y"] = pt_ort_y_list
    
    df_ipm_ps = pd.DataFrame(df_dict_ipm)
    df_ipm_ps = df_ipm_ps.sort_values(['timestamp',"idx_slot","idx_corner"])
    df_ipm_ps.reset_index(drop=True, inplace=True)            
                
    # 进一步解析vcs信息, 生成dataframe    
    ts_list = []
    vcs_wld_x_list = []
    vcs_wld_y_list = []
    vcs_wld_z_list = []
    q4_x_list = []
    q4_y_list = []
    q4_z_list = []
    q4_w_list = []
    for vcs in data_vcs:
        ts = int(int(vcs[0]))
        vcs_wld_x = vcs[1]["loc_vcs"]["x"]
        vcs_wld_y = vcs[1]["loc_vcs"]["y"]
        vcs_wld_z = vcs[1]["loc_vcs"]["z"]
        
        loc_q4_x = vcs[1]["loc_q4"]["x"]
        loc_q4_y = vcs[1]["loc_q4"]["y"]
        loc_q4_z = vcs[1]["loc_q4"]["z"]
        loc_q4_w = vcs[1]["loc_q4"]["w"]
        
        ts_list.append(ts)
        vcs_wld_x_list.append(vcs_wld_x)
        vcs_wld_y_list.append(vcs_wld_y)
        vcs_wld_z_list.append(vcs_wld_z)
        
        q4_x_list.append(loc_q4_x)
        q4_y_list.append(loc_q4_y)
        q4_z_list.append(loc_q4_z)
        q4_w_list.append(loc_q4_w)
        
    df_dict_vcs = dict()    
    df_dict_vcs["timestamp"] = ts_list
    df_dict_vcs["vwld_x"] = vcs_wld_x_list
    df_dict_vcs["vwld_y"] = vcs_wld_y_list
    df_dict_vcs["vwld_z"] = vcs_wld_z_list
    
    df_dict_vcs["q4_x"] = q4_x_list
    df_dict_vcs["q4_y"] = q4_y_list
    df_dict_vcs["q4_z"] = q4_z_list
    df_dict_vcs["q4_w"] = q4_w_list
    
    df_ndm_vcs = pd.DataFrame(df_dict_vcs)
    df_ndm_vcs = df_ndm_vcs.sort_values('timestamp')
    df_ndm_vcs.reset_index(drop=True, inplace=True)
    
    # 进一步解析 uss obj & uss ps信息, 生成dataframe  
    ts_list_uss = []
    uss_obj_x_id_list = []
    uss_obj_x_cfd_list = []
    uss_obj_x_type_list = []
    uss_obj_x_height_list = []
    uss_obj_x_pos_1_x_list = []
    uss_obj_x_pos_1_y_list = []
    uss_obj_x_pos_2_x_list = []
    uss_obj_x_pos_2_y_list = []

    ts_list_ps  = []
    uss_ps_id_list = []
    uss_ps_length_list = []
    uss_ps_depth_list  = []
    uss_ps_angle_list  = []
    uss_ps_status_list = []
    uss_ps_type_list  = []
    uss_ps_1st_corner_x_list = []
    uss_ps_1st_corner_y_list = []
    uss_ps_2nd_corner_x_list = []
    uss_ps_2nd_corner_y_list = []

    for uss in data_uss:
        ts = int(uss[0])
        for obj_list in uss[1]["uss_obj"]:
            ts_list_uss.append(ts)
            uss_obj_x_id = obj_list["uss_obj_x_id_ui8"]
            uss_obj_x_cfd = obj_list["uss_obj_x_cfd_ui8"]  #obj confidence
            uss_obj_x_type = obj_list["uss_obj_x_type_en"] #obj type
            uss_obj_x_height = obj_list["uss_obj_x_height_en"]/uss_unit
            uss_obj_x_pos_1_x = obj_list["uss_obj_x_pos_1"]["x_si16"]/uss_unit
            uss_obj_x_pos_1_y = obj_list["uss_obj_x_pos_1"]["y_si16"]/uss_unit
            uss_obj_x_pos_2_x = obj_list["uss_obj_x_pos_2"]["x_si16"]/uss_unit
            uss_obj_x_pos_2_y = obj_list["uss_obj_x_pos_2"]["y_si16"]/uss_unit
            
            uss_obj_x_id_list.append(uss_obj_x_id)
            uss_obj_x_cfd_list.append(uss_obj_x_cfd)
            uss_obj_x_type_list.append(uss_obj_x_type)
            uss_obj_x_height_list.append(uss_obj_x_height)
            uss_obj_x_pos_1_x_list.append(uss_obj_x_pos_1_x)
            uss_obj_x_pos_1_y_list.append(uss_obj_x_pos_1_y)
            uss_obj_x_pos_2_x_list.append(uss_obj_x_pos_2_x)
            uss_obj_x_pos_2_y_list.append(uss_obj_x_pos_2_y)
            
        for ps_list in uss[1]["uss_ps"]:
            ts_list_ps.append(ts)
            uss_ps_id = ps_list["uss_ps_id"] 
            uss_ps_length = ps_list["uss_ps_length_ui16"]/uss_unit
            uss_ps_depth  = ps_list["uss_ps_depth_ui16"]/uss_unit
            uss_ps_angle  = ps_list["uss_ps_angle_ui16"] # /angle_unit + np.pi/2
            uss_ps_status  = ps_list["uss_ps_status_en"] # NONE=0,SEARCH =1, CORRECT=2
            uss_ps_type    = ps_list["uss_ps_type_en"]   # NONE=0,PARALLEL=1,CROSS=2,DIAGONAL=3
            uss_ps_1st_corner_x = ps_list["uss_ps_1st_corner"]["x_si16"]/uss_unit
            uss_ps_1st_corner_y = ps_list["uss_ps_1st_corner"]["y_si16"]/uss_unit
            uss_ps_2nd_corner_x = ps_list["uss_ps_2nd_corner"]["x_si16"]/uss_unit
            uss_ps_2nd_corner_y = ps_list["uss_ps_2nd_corner"]["y_si16"]/uss_unit
            
            uss_ps_id_list.append(uss_ps_id)
            uss_ps_length_list.append(uss_ps_length)
            uss_ps_depth_list.append(uss_ps_depth)
            uss_ps_angle_list.append(uss_ps_angle)
            uss_ps_status_list.append(uss_ps_status)
            uss_ps_type_list.append(uss_ps_type)
            uss_ps_1st_corner_x_list.append(uss_ps_1st_corner_x)
            uss_ps_1st_corner_y_list.append(uss_ps_1st_corner_y)
            uss_ps_2nd_corner_x_list.append(uss_ps_2nd_corner_x)
            uss_ps_2nd_corner_y_list.append(uss_ps_2nd_corner_y)
            
    df_dict_uss_obj = dict()
    df_dict_uss_ps  = dict()        
    df_dict_uss_obj["timestamp"] = ts_list_uss
    df_dict_uss_obj["uss_obj_x_id"] = uss_obj_x_id_list
    df_dict_uss_obj["uss_obj_x_confidence"] = uss_obj_x_cfd_list
    df_dict_uss_obj["uss_obj_x_type"]    = uss_obj_x_type_list
    df_dict_uss_obj["uss_obj_x_height"]  = uss_obj_x_height_list
    df_dict_uss_obj["uss_obj_x_pos_1_x"] = uss_obj_x_pos_1_x_list
    df_dict_uss_obj["uss_obj_x_pos_1_y"] = uss_obj_x_pos_1_y_list
    df_dict_uss_obj["uss_obj_x_pos_2_x"] = uss_obj_x_pos_2_x_list
    df_dict_uss_obj["uss_obj_x_pos_2_y"] = uss_obj_x_pos_2_y_list
    
    df_uss_obj = pd.DataFrame(df_dict_uss_obj)
    df_uss_obj = df_uss_obj.sort_values(["timestamp","uss_obj_x_id"])
    df_uss_obj.reset_index(drop=True, inplace=True)
    
    df_dict_uss_ps["timestamp"] = ts_list_ps
    df_dict_uss_ps["uss_ps_id"] = uss_ps_id_list
    df_dict_uss_ps["uss_ps_length"] = uss_ps_length_list
    df_dict_uss_ps["uss_ps_depth"]  = uss_ps_depth_list
    
    df_dict_uss_ps["uss_ps_angle"] = uss_ps_angle_list
    df_dict_uss_ps["uss_ps_status"]  = uss_ps_status_list
    df_dict_uss_ps["uss_ps_type"] = uss_ps_type_list
    
    df_dict_uss_ps["uss_ps_1st_corner_x"] = uss_ps_1st_corner_x_list
    df_dict_uss_ps["uss_ps_1st_corner_y"] = uss_ps_1st_corner_y_list
    df_dict_uss_ps["uss_ps_2nd_corner_x"] = uss_ps_2nd_corner_x_list
    df_dict_uss_ps["uss_ps_2nd_corner_y"] = uss_ps_2nd_corner_y_list
    
    df_uss_ps = pd.DataFrame(df_dict_uss_ps)
    df_uss_ps = df_uss_ps.sort_values(['timestamp',"uss_ps_id"])
    df_uss_ps.reset_index(drop=True, inplace=True)
    
    return df_ndm_vcs, df_uss_obj, df_uss_ps, df_ipm_ps


def data_merge(df_vcs, df_obj, df_uss, df_ipm,  idx_list, dt = 6000):
    # 分析第一个case,车位id 164,166
    df_uss_list = []
    for idx in idx_list: 
        df_uss_slice = df_uss[(df_uss["uss_ps_id"]==idx)] # uss id filter
        df_uss_list.append(df_uss_slice)
    df_uss_filter = pd.concat(df_uss_list, ignore_index=True)
    
    # 使用merge实现就近join
    # 假设df1和df2的keys是升序的,这里使用了'left','right'来指定merge的方向
    # 使用'backward'来找到就近的key进行合并
    df_ps_merged = pd.merge_asof(df_uss_filter.sort_values('timestamp'), df_vcs.sort_values('timestamp'),
                              on='timestamp', direction='backward')
    
    result11 = df_ps_merged.apply(lambda row: Q4ToMatrix(row['q4_x'], row['q4_y'],row['q4_z'],row['q4_w'],
                                row['uss_ps_1st_corner_x'], row['uss_ps_1st_corner_y'],
                                row['vwld_x'], row['vwld_y']), axis=1) # get corner points wcs x & y
    
    result12 = df_ps_merged.apply(lambda row: Q4ToMatrix(row['q4_x'], row['q4_y'],row['q4_z'],row['q4_w'],
                                 row['uss_ps_2nd_corner_x'], row['uss_ps_2nd_corner_y'],
                                 row['vwld_x'], row['vwld_y']), axis=1) # get corner points wcs x & y
    
    
    x_wld_1 = [res[0] for res in result11] # corner 1 x
    y_wld_1 = [res[1] for res in result11] # corner 1 y
    
    x_wld_2 = [res[0] for res in result12] # corner 2 x
    y_wld_2 = [res[1] for res in result12] # corner 2 y
    
    df_ps_merged["uss_ps_1st_corner_x_wld"] = x_wld_1
    df_ps_merged["uss_ps_1st_corner_y_wld"] = y_wld_1
    
    df_ps_merged["uss_ps_2nd_corner_x_wld"] = x_wld_2
    df_ps_merged["uss_ps_2nd_corner_y_wld"] = y_wld_2
    df_ps_merged = df_ps_merged.sort_values(['timestamp',"uss_ps_id"])
    

    dt_min = df_ps_merged["timestamp"].min() - dt # dt:ms
    # dt_max = df_ps_merged["timestamp"].max() + dt*1.2
    
    # df_obj_slice = df_obj[(df_obj["timestamp"]>=dt_min) & (df_obj["timestamp"]<=dt_max)] 
    
    df_obj_slice = df_obj[(df_obj["timestamp"]>=dt_min)] 
    
    
    df_obj_merged = pd.merge_asof(df_obj_slice.sort_values('timestamp'), df_vcs.sort_values('timestamp'),
                              on='timestamp', direction='backward')
    df_obj_merged = df_obj_merged.sort_values(['timestamp',"uss_obj_x_id"])
    
    
    result21 = df_obj_merged.apply(lambda row: Q4ToMatrix(row['q4_x'], row['q4_y'],row['q4_z'],row['q4_w'],
                                row['uss_obj_x_pos_1_x'], row['uss_obj_x_pos_1_y'],
                                row['vwld_x'], row['vwld_y']), axis=1) # get object points wcs x & y
    
    result22 = df_obj_merged.apply(lambda row: Q4ToMatrix(row['q4_x'], row['q4_y'],row['q4_z'],row['q4_w'],
                                 row['uss_obj_x_pos_2_x'], row['uss_obj_x_pos_2_y'],
                                 row['vwld_x'], row['vwld_y']), axis=1) # get object points wcs x & y
    
    x_obj_1 = [res[0] for res in result21]
    y_obj_1 = [res[1] for res in result21]
    
    x_obj_2 = [res[0] for res in result22]
    y_obj_2 = [res[1] for res in result22]
    
    df_obj_merged["uss_obj_x_pos_1_x_wld"] = x_obj_1
    df_obj_merged["uss_obj_x_pos_1_y_wld"] = y_obj_1
    
    df_obj_merged["uss_obj_x_pos_2_x_wld"] = x_obj_2
    df_obj_merged["uss_obj_x_pos_2_y_wld"] = y_obj_2
    df_obj_merged = df_obj_merged[(df_obj_merged["uss_obj_x_confidence"] > 0)] # confidence means effective obj
    df_obj_merged = df_obj_merged.sort_values(["timestamp","uss_obj_x_id"])
    df_obj_merged.reset_index(drop=True, inplace=True)
    
    
    # merge ipm slot
    df_ipm_merged = pd.merge_asof(df_ipm.sort_values('timestamp'), df_vcs.sort_values('timestamp'),
                              on='timestamp', direction='backward')
    
    res_ipm = df_ipm_merged.apply(lambda row: Q4ToMatrix(row['q4_x'], row['q4_y'],row['q4_z'],row['q4_w'],
                                row['ipm_vcs_x'], row['ipm_vcs_y'],
                                row['vwld_x'], row['vwld_y']), axis=1) # get corner points wcs x & y
    
    x_ipm = [res[0] for res in res_ipm]
    y_ipm = [res[1] for res in res_ipm]
    
    df_ipm_merged["ipm_wld_x"] = x_ipm
    df_ipm_merged["ipm_wld_y"] = y_ipm
    df_ipm_merged = df_ipm_merged.sort_values(['timestamp',"idx_slot","idx_corner"])
    df_ipm_merged.reset_index(drop=True, inplace=True)
    
    return df_ps_merged, df_obj_merged, df_ipm_merged
    
def get_data(df_ps, df_obj, df_ipm, dfv, idx_list, dt = 6*1000):
    
    # dt: ms
    dt_min = df_ps["timestamp"].min() - dt # uss corner首次出现时刻往前推dt
    dt_max = df_ps["timestamp"].max() + dt*1.2 # uss corner首次出现时刻往后推dt
    df_vcs_out = dfv[(dfv["timestamp"]>=dt_min) & (dfv["timestamp"]<=dt_max)]
    
    pt_list = []
    
    pt0_t = df_vcs_out["timestamp"].tolist() #
    pt0_x = df_vcs_out["vwld_x"].tolist()    #
    pt0_y = df_vcs_out["vwld_y"].tolist()    #
    pt_vcs = (pt0_t, pt0_x, pt0_y)
    
    pt_list.append(pt_vcs) # vcs vehicle trajectory point

    dt_min_list = []
    dt_max_list = []
    pt_corner_list = []
    
    for idx in idx_list:
        df_ps_idx = df_ps[df_ps["uss_ps_id"] == idx]

        dt_min_list.append(dt_min)
        dt_max_list.append(dt_max)
        
        pt_c1_t = df_ps_idx["timestamp"].tolist()
        pt_c1_x = df_ps_idx["uss_ps_1st_corner_x_wld"].tolist()
        pt_c1_y = df_ps_idx["uss_ps_1st_corner_y_wld"].tolist()
        pt_c2_x = df_ps_idx["uss_ps_2nd_corner_x_wld"].tolist()
        pt_c2_y = df_ps_idx["uss_ps_2nd_corner_y_wld"].tolist()
        pt_corner = (pt_c1_t, pt_c1_x, pt_c1_y, pt_c2_x, pt_c2_y )
        pt_corner_list.append(pt_corner) # each corner, 2 points
    
    pt_list.append(pt_corner_list) 
    
    # for idx in range(1,13):
    #     df_obj_idx = df_obj[df_obj["uss_obj_x_id"]==idx]
    #     if df_obj_idx.shape[0] > 0:
    #         # obj_x_pos_1 与 obj_x_pos_2 坐标相同对应点状obs, 坐标不相同对应杆状obs
    #         pt1_x = df_obj_idx["uss_obj_x_pos_1_x_wld"].tolist()
    #         pt1_y = df_obj_idx["uss_obj_x_pos_1_y_wld"].tolist()
    #         pt_t = df_obj_idx["timestamp"].tolist()
    #         pt2_x = df_obj_idx["uss_obj_x_pos_2_x_wld"].tolist()
    #         pt2_y = df_obj_idx["uss_obj_x_pos_2_y_wld"].tolist()
    #         pts = (pt_t, pt1_x, pt1_y, pt2_x, pt2_y)
    #         pt_list.append(pts)
            
    # obj_x_pos_1 与 obj_x_pos_2 坐标相同对应点状obs, 坐标不相同对应杆状obs
    pt_t  = df_obj["timestamp"].tolist()
    pt1_x = df_obj["uss_obj_x_pos_1_x_wld"].tolist()
    pt1_y = df_obj["uss_obj_x_pos_1_y_wld"].tolist()
    pt2_x = df_obj["uss_obj_x_pos_2_x_wld"].tolist()
    pt2_y = df_obj["uss_obj_x_pos_2_y_wld"].tolist()
    pt_obj_1 = (pt_t, pt1_x, pt1_y)
    pt_obj_2 = (pt_t, pt2_x, pt2_y)
    pt_list.append(pt_obj_1)
    pt_list.append(pt_obj_2)
    
    # ipm corner
    df0 = df_ipm[df_ipm["idx_corner"]==0]
    df1 = df_ipm[df_ipm["idx_corner"]==1]
    df2 = df_ipm[df_ipm["idx_corner"]==2]
    df3 = df_ipm[df_ipm["idx_corner"]==3]
    
    df0 = df0.sort_values(['timestamp',"idx_slot"])
    df1 = df1.sort_values(['timestamp',"idx_slot"])
    df2 = df2.sort_values(['timestamp',"idx_slot"])
    df3 = df3.sort_values(['timestamp',"idx_slot"])

    pt_ipm_t0 = df0["timestamp"].tolist()
    pt0_ipm_x = df0["ipm_wld_x"].tolist()
    pt0_ipm_y = df0["ipm_wld_y"].tolist()
    
    pt_ipm_t1 = df1["timestamp"].tolist()
    pt1_ipm_x = df1["ipm_wld_x"].tolist()
    pt1_ipm_y = df1["ipm_wld_y"].tolist()
    
    pt_ipm_t2 = df2["timestamp"].tolist()
    pt2_ipm_x = df2["ipm_wld_x"].tolist()
    pt2_ipm_y = df2["ipm_wld_y"].tolist()
    
    pt_ipm_t3 = df3["timestamp"].tolist()
    pt3_ipm_x = df3["ipm_wld_x"].tolist()
    pt3_ipm_y = df3["ipm_wld_y"].tolist()
    
    pt_ipm_0 = (pt_ipm_t0, pt0_ipm_x, pt0_ipm_y)
    pt_ipm_1 = (pt_ipm_t1, pt1_ipm_x, pt1_ipm_y)
    pt_ipm_2 = (pt_ipm_t2, pt2_ipm_x, pt2_ipm_y)
    pt_ipm_3 = (pt_ipm_t3, pt3_ipm_x, pt3_ipm_y)
    
    pt_list.append(pt_ipm_0)
    pt_list.append(pt_ipm_1) 
    pt_list.append(pt_ipm_2) 
    pt_list.append(pt_ipm_3) 

    return pt_list
            

def find_first_index_greater_than(st, elem):
    nums = len(st)
    for index, value in enumerate(st):
        if value > elem:
            return index
    if index == nums -1:
        return nums
    return 0  # 如果没有找到,返回-1或者None, 视具体情况而定
    
def update_line(frame, args, plots):
    print("current frame is: ", frame)
    pt_vcs       = args[0]
    pt_ps_list   = args[1]
    pt_obj_pos_1 = args[2]
    pt_obj_pos_2 = args[3]
    pt_ipm_0 = args[4]
    pt_ipm_1 = args[5]
    pt_ipm_2 = args[6]
    pt_ipm_3 = args[7]
    
    line_vcs = plots[0]
    scat_obj_pos_1 = plots[1]
    scat_obj_pos_2 = plots[2]
    scat_ipm_0 = plots[3]
    scat_ipm_1 = plots[4]
    scat_ipm_2 = plots[5]
    scat_ipm_3 = plots[6]
    
    line_corner_list = []
    if frame == 0:
        line_vcs.set_data([], [])
        data = np.stack([[], []]).T
        scat_obj_pos_1.set_offsets(data)
        scat_obj_pos_2.set_offsets(data)
        scat_ipm_0.set_offsets(data)
        scat_ipm_1.set_offsets(data)
        scat_ipm_2.set_offsets(data)
        scat_ipm_3.set_offsets(data)
        
        for idx in range(7,len(plots)-1,2):
            line_corner_1 = plots[idx]
            line_corner_2 = plots[idx + 1]
            line_corner_1.set_data([], [])
            line_corner_2.set_data([], [])
            line_corner_list.append(line_corner_1)
            line_corner_list.append(line_corner_2)

    else:
        pt0_t = pt_vcs[0]
        pt0_x = pt_vcs[1]
        pt0_y = pt_vcs[2]
        
        t0 = max([pt for pt in pt0_t[:frame]])
        print("current vcs time is: ", t0)
        x0 = [pt for pt in pt0_x[:frame]]
        y0 = [pt for pt in pt0_y[:frame]]
        """ update vehicle trajectory"""
        line_vcs.set_data(x0, y0) 
        # text_pt.set_position(x0[:frame],y0[:frame])
        # axt.set_title(f'Current Time: {t0}',fontsize=16)
        
        pt_t_pos   = pt_obj_pos_1[0]
        pt_x_pos_1 = pt_obj_pos_1[1]
        pt_y_pos_1 = pt_obj_pos_1[2]
        pt_x_pos_2 = pt_obj_pos_2[1]
        pt_y_pos_2 = pt_obj_pos_2[2]
        
        """ update each obj,2 position"""
        fm_pos = find_first_index_greater_than(pt_t_pos, t0)
        x_pos_1 = [pt for pt in pt_x_pos_1[:fm_pos]]
        y_pos_1 = [pt for pt in pt_y_pos_1[:fm_pos]]
        pos_1 = np.stack([x_pos_1, y_pos_1]).T
        scat_obj_pos_1.set_offsets(pos_1)
    
        x_pos_2 = [pt for pt in pt_x_pos_2[:fm_pos]]
        y_pos_2 = [pt for pt in pt_y_pos_2[:fm_pos]]
        pos_2 = np.stack([x_pos_2, y_pos_2]).T
        scat_obj_pos_2.set_offsets(pos_2)
        
        """ update ipm 4 corner"""
        fm_ipm_0 = find_first_index_greater_than(pt_ipm_0[0], t0)
        # if frame > 5 and fm_ipm_0 == 0:
        #     fm_ipm_0 = -1
        x_ipm_0 = [pt for pt in pt_ipm_0[1][:fm_ipm_0]]
        y_ipm_0 = [pt for pt in pt_ipm_0[2][:fm_ipm_0]]
        ipm_0 = np.stack([x_ipm_0, y_ipm_0]).T
        scat_ipm_0.set_offsets(ipm_0)
        
        fm_ipm_1 = find_first_index_greater_than(pt_ipm_1[0], t0)
        # if frame > 5 and fm_ipm_1 == 0:
        #     fm_ipm_1 = -1
        x_ipm_1 = [pt for pt in pt_ipm_1[1][:fm_ipm_1]]
        y_ipm_1 = [pt for pt in pt_ipm_1[2][:fm_ipm_1]]
        ipm_1 = np.stack([x_ipm_1, y_ipm_1]).T
        scat_ipm_1.set_offsets(ipm_1)
        
        fm_ipm_2 = find_first_index_greater_than(pt_ipm_2[0], t0)
        # if frame > 5 and fm_ipm_2 == 0:
        #     fm_ipm_2 = -1
        x_ipm_2 = [pt for pt in pt_ipm_2[1][:fm_ipm_2]]
        y_ipm_2 = [pt for pt in pt_ipm_2[2][:fm_ipm_2]]
        ipm_2 = np.stack([x_ipm_2, y_ipm_2]).T
        scat_ipm_2.set_offsets(ipm_2)
        
        fm_ipm_3 = find_first_index_greater_than(pt_ipm_3[0], t0)
        # if frame > 5 and fm_ipm_3 == 0:
        #     fm_ipm_3 = -1
        x_ipm_3 = [pt for pt in pt_ipm_3[1][:fm_ipm_3]]
        y_ipm_3 = [pt for pt in pt_ipm_3[2][:fm_ipm_3]]
        ipm_3 = np.stack([x_ipm_3, y_ipm_3]).T
        scat_ipm_3.set_offsets(ipm_3)
        
        """ update each corner,2 position"""
        
        idxx = range(7,len(plots)-1,2)
        # """ update each corner,2 position"""
        
        for num, idx in enumerate(idxx):
            line_corner_1 = plots[idx]
            line_corner_2 = plots[idx + 1]
            
            pt_t  = pt_ps_list[num][0]
            pt1_x = pt_ps_list[num][1]
            pt1_y = pt_ps_list[num][2]
            pt2_x = pt_ps_list[num][3]
            pt2_y = pt_ps_list[num][4]
            
            fm12 = find_first_index_greater_than(pt_t, t0)   
            # print("fm12 is: ", fm12)
            # if fm12 > 0:
            #     fm_old[num] = fm12
            # if frame > 0 and fm12 == 0:
            #     fm12 = fm_old[num]
            #     fm1 = fm1_old
            x1 = [pt for pt in pt1_x[:fm12]]
            y1 = [pt for pt in pt1_y[:fm12]]
            line_corner_1.set_data(x1, y1)

            x2 = [pt for pt in pt2_x[:fm12]]
            y2 = [pt for pt in pt2_y[:fm12]]
            line_corner_2.set_data(x2, y2)
            
            line_corner_list.append(line_corner_1)
            line_corner_list.append(line_corner_2)
 
    # plt.xlabel('x(wcs-m)',fontsize=16)
    # plt.ylabel("y(wcs-m)",fontsize=16)
    
    return [line_vcs, scat_obj_pos_1, scat_obj_pos_2, scat_ipm_0, scat_ipm_1, scat_ipm_2, scat_ipm_3] + line_corner_list

if __name__ == '__main__':
    fname = "D:\\work\\data\\data105130.json"
    df_vcs, df_uss_obj, df_uss_ps, df_ipm_ps = process_json(fname)
    idx_uss = [180]
    dt = 8 * 1000 #ms
    df_uss_mg, df_obj_mg, df_ipm_mg = data_merge(df_vcs, df_uss_obj, df_uss_ps, df_ipm_ps, idx_uss,dt)
    
    data_args = get_data(df_uss_mg, df_obj_mg, df_ipm_mg, df_vcs, idx_uss, dt)
    len_frames = len(data_args[0][0])
    x_min = min(data_args[0][1]) - 10
    x_max = max(data_args[0][1]) + 10
    y_min = min(data_args[0][2]) - 10
    y_max = max(data_args[0][2]) + 10
    
    fig, ax = plt.subplots(figsize=(21,10))
    ax.set(xlim=[x_min, x_max], ylim=[y_min, y_max])
    # ax.set(xlim=[-80, 10], ylim=[-30, 0])
    ax.set_xlabel(r"wcs_x [m]",fontsize=16)
    ax.set_ylabel(r"wcs_y [m]",fontsize=16)
    title_ani = ax.set_title("UssObj & USSParkingSlots & IPMParkingSlots",fontsize=16) 
    ax.grid(True)
    xticks_labels = range(int(x_min)-1, int(x_max) + 1, 1)  # x轴刻度间隔设置为5
    yticks_labels = range(int(y_min)-1, int(y_max) + 1, 1)  # y轴刻度间隔设置为50
 
    # 设置x轴和y轴的刻度
    ax.set_xticks(xticks_labels)
    ax.set_yticks(yticks_labels)

    line_vcs, = ax.plot([], [], 'ko', label='ego trajectory')
    scat_obj_pos_1  = ax.scatter([], [], c='r', s=15, label='obj pos 1')
    scat_obj_pos_2  = ax.scatter([], [], c='r', s=15, label='obj pos 2')
    
    scat_ipm_0  = ax.scatter([], [], c='g', marker="o", s=15, label='ipm corner 0')
    scat_ipm_1  = ax.scatter([], [], c='g', marker="d", s=15, label='ipm corner 1')
    scat_ipm_2  = ax.scatter([], [], c='g', marker="s", s=15, label='ipm corner 2')
    scat_ipm_3  = ax.scatter([], [], c='g', marker="*", s=15, label='ipm corner 3')
    
    #text_pt = plt.text(3.5,0.8,"",fontsize=16)
    plot_list = []
    plot_list.append(line_vcs)
    
    plot_list.append(scat_obj_pos_1)
    plot_list.append(scat_obj_pos_2)
    
    plot_list.append(scat_ipm_0)
    plot_list.append(scat_ipm_1)
    plot_list.append(scat_ipm_2)
    plot_list.append(scat_ipm_3) 
    
    for idx in idx_uss:
        line_corner_1, = ax.plot([], [], 'b-', label="id=" + str(idx) + " conner 1")
        line_corner_2, = ax.plot([], [], 'c-', label="id=" + str(idx) + " conner 2")
        plot_list.append(line_corner_1)
        plot_list.append(line_corner_2)
        
    ax.legend()
    
    anim = animation.FuncAnimation(fig, update_line, frames=len_frames,
        fargs=(data_args, plot_list),interval=100,repeat=False)
    anim.save("113151_6.gif", writer='pillow')

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1638748.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【Python函数和类6/6】类与对象

目录 目标 类与对象 类的定义 栗子 实例化对象 属性和方法的调用 特殊的self参数 类方法的其它参数 函数与方法的区别 总结 目标 在前面的博客当中&#xff0c;我们已经接触了一部分封装。比如&#xff1a;将数据扔进列表中&#xff0c;这就是一个简单…

短视频素材去哪里搬运?短视频素材有哪些类型?

在这个数字化和视觉传达至关重要的时代&#xff0c;选择合适的视频素材对于提升视频内容的吸引力和观众参与度至关重要。无论您是一名广告制片人、社交媒体经理还是独立视频制作者&#xff0c;以下这些精选的视频素材网站将为您提供从高清视频到特效资源的全面支持&#xff0c;…

工厂模式和策略模式区别

工厂模式和策略模式都是面向对象设计模式&#xff0c;但它们的目的和应用场景有所不同。 工厂模式是一种创建型设计模式&#xff0c;旨在通过使用一个工厂类来创建对象&#xff0c;而不是直接使用new关键字来创建对象。这样做可以使系统更容易扩展和维护&#xff0c;因为新的对…

reactjs后台管理系统搭建

1 通过yarn 模板创建reactjs项目 yarn create vite reactjs-antdesign-admin --template react-ts 2 基础路由测试 定义一个router/index.tsx&#xff0c;里面定义路由组件 const Router: React.FC () > {return (<HashRouter><Switch><Route path"…

Edge浏览器使用心得与深度探索

&#x1f90d; 前端开发工程师、技术日更博主、已过CET6 &#x1f368; 阿珊和她的猫_CSDN博客专家、23年度博客之星前端领域TOP1 &#x1f560; 牛客高级专题作者、打造专栏《前端面试必备》 、《2024面试高频手撕题》 &#x1f35a; 蓝桥云课签约作者、上架课程《Vue.js 和 E…

信息时代的智慧导航:高效搜索、信息筛选与信任构建的全面指南!

文章目录 一、高效搜索&#xff1a;快速定位目标信息的秘诀二、信息筛选&#xff1a;去伪存真&#xff0c;找到有价值的信息三、信任构建&#xff1a;深入了解与直接沟通《搜索之道&#xff1a;信息素养与终身学习的新引擎》亮点内容简介目录获取方式 随着科技的飞速发展&#…

前端基础学习html(2)

目录 表格标签&#xff1a; 列表标签&#xff1a; 表格标签&#xff1a; <!-- 表格基本架构 --><!-- tr表示一行&#xff0c;td表示一行内单元格 --><!--th为第一行表头加粗居中显示 --><table border"1"><thead><tr><th&g…

用Stream流方式合并两个list集合(部分对象属性重合)

一、合并出共有部分 package com.xu.demo.test;import java.util.Arrays; import java.util.List; import java.util.stream.Collectors;public class ListMergeTest1 {public static void main(String[] args) {List<User> list1 Arrays.asList(new User(1, "Alic…

【linux学习指南】linux指令与实践文件编写

文章目录 &#x1f4dd;前言&#x1f320; linux目录结构&#x1f309;linux命令介绍 &#x1f320;pwd命令&#x1f309;mkdir指令&#xff08;重要&#xff09; &#x1f320;cd 指令&#x1f309;touch指令 &#x1f320;rmdir指令 && rm 指令&#xff08;重要&…

nginx--配置文件

组成 主配置文件&#xff1a;nginx.conf 子配置文件&#xff1a;include conf.d/*.conf 协议相关的配置文件&#xff1a;fastcgi uwsgi scgi等 mime.types&#xff1a;⽀持的mime类型&#xff0c;MIME(Multipurpose Internet Mail Extensions)多用途互联⽹网邮件扩展类型&…

KUKA机器人KR3 R540维护保养——涂润滑脂

KUKA机器人在保养时少不了润滑脂&#xff0c;不同型号的机器人需要的润滑脂类型也不一样&#xff0c;保养时注意选用合适的润滑脂。本篇文章以KUKA机器人KR3 R540为例&#xff0c;在轴盖板 A2、A3、A5 的内侧涂上润滑脂。 一、涂润滑脂的作用 拆开机器人一个轴的盖板&am…

链表面试题2

1&#xff0c;合并两个有序链表 我们先定义一个虚拟节点newH&#xff0c; 然后按照上图所走&#xff0c;但是当其中一个链表走空时&#xff0c;我们只需返回另一个链表即可 class Solution {public ListNode mergeTwoLists(ListNode headA, ListNode headB) {ListNode newhead…

python基础语法--函数

一、函数概述 函数就是执行特定任务完成特定功能的一段代码。可以在程序中将某一段代码定义成函数&#xff0c;并指定一个函数名和接收的输入&#xff08;参数&#xff09;&#xff0c;这样就可以在程序的其他地方通过函数名多次调用并执行该段代码了。 每次调用执行后&#…

Mybatis-Plus学习:快速入门、核心功能、扩展功能、插件功能

文章目录 MybatisPlus快速入门快速开始常见注解常见配置 核心功能条件构造器&#xff08;Wrapper&#xff09;自定义SQLService接口基本用法基础业务接口复杂业务接口Lamda查询Lamda更新批量新增 扩展功能代码生成代码生成器快速开发插件 静态工具逻辑删除枚举处理器JSON处理器…

机器人系统ros2-开发实践04-ROS 2 启动文件管理大型项目的最佳实践

机器人上的大型应用通常涉及多个互连的节点&#xff0c;每个节点可以有许多参数。海龟模拟器中模拟多只海龟就是一个很好的例子。海龟模拟由多个海龟节点、世界配置以及 TF 广播器和监听器节点组成。在所有节点之间&#xff0c;存在大量影响这些节点的行为和外观的 ROS 参数。 …

【C++】哈希的应用---位图

目录 1、引入 2、位图的概念 3、位图的实现 ①框架的搭建 ②设置存在 ③设置不存在 ④检查存在 ​4、位图计算出现的次数 5、完整代码 1、引入 我们可以看一道面试题 给40亿个不重复的无符号整数&#xff0c;没排过序。给一个无符号整数&#xff0c;如何快速判断一个数…

罗宾斯《管理学》第15版笔记/课后习题/考研真题答案

第Ⅰ篇 管理导论 第1章 工作场所中的管理者和你 1.1 知识结构导图 1.2 考点难点归纳 1.3 课后习题详解 1.4 考研真题详解 附加模块一 管理史 知识结构导图 考点难点归纳 课后习题详解 考研真题详解 第2章 决 策 2.1 知识结构导图 2.2 考点难点归纳 2.3 课后习题详解…

C++string类使用大全

目录 温馨提示&#xff1a;这篇文章有约两万字 什么是string类&#xff1f; 一. 定义和初始化string对象 1.string的构造函数的形式&#xff1a; 2.拷贝赋值运算符 3.assign函数 二.string对象上的操作 1.读写string对象 2.读取未知数量的string对象 3.使用getline …

STM32中断系统详解

系列文章目录 STM32单片机系列专栏 C语言术语和结构总结专栏 文章目录 1. 中断基本概念 2. STM32中断 3. NVIC的基本组件 3.1 NVIC的基本组件 3.2 NVIC的优先级 4. EXTI外部中断 4.1 基本概念 4.2 基本结构 5. AFIO 1. 中断基本概念 中断&#xff08;Interrupt&…