分析不同高度障碍物检测情况

import re
import matplotlib.pyplot as plt
from datetime import datetime
import matplotlib.dates as mdates
from matplotlib.font_manager import FontProperties

# 设置中文字体
font_path = '/usr/local/sunlogin/res/font/wqy-zenhei.ttc'  # 替换为你的实际字体路径
my_font = FontProperties(fname=font_path)

# 日志文件路径
log_file = '/home/sany/Documents/xwechat_files/wxid_f0rg2f76s76t22_0b3d/msg/file/2025-03/lidarlog和小蓝瓶log/2025-03-08,11-56-08_syzk-lidar.log'

# 正则表达式
center_pattern = re.compile(r'obj center_x ([\d.-]+) center_y ([\d.-]+)')
obj_start_pattern = re.compile(r'\[Info\]-\[PointCloudHandle:\d+\]:\[(\d{4}-\d{2}-\d{2},\d{2}:\d{2}:\d{2}\.\d+)\]-front obj size (\d+)')
obj_end_pattern = re.compile(r'\[Info\]-\[run:\d+\]:\[(\d{4}-\d{2}-\d{2},\d{2}:\d{2}:\d{2}\.\d+)\]-\[Lidar Front Cost Time (\d+)')

# 时间范围
use_time_filter = True
start_time_str = '2025-03-08,11:58:26.162' 
end_time_str = '2025-03-08,11:58:40.346'

# 转换时间字符串为 datetime
start_time = datetime.strptime(start_time_str, '%Y-%m-%d,%H:%M:%S.%f')
end_time = datetime.strptime(end_time_str, '%Y-%m-%d,%H:%M:%S.%f')

# 数据存储
timestamps = []
detected_y_values = []
total_frames = 0
detected_count = 0  # 记录检测到的帧数

# 读取日志文件并解析
with open(log_file, 'r', encoding='utf-8', errors='ignore') as file:
    recording = False
    detected_y = 0  # 默认未检测到

    for line in file:
        # 解析帧开始
        start_match = obj_start_pattern.search(line)
        if start_match:
            timestamp = datetime.strptime(start_match.group(1), '%Y-%m-%d,%H:%M:%S.%f')
            if not use_time_filter or (start_time <= timestamp <= end_time):
                recording = True
                total_frames += 1
                detected_y = 0  # 初始化未检测
                timestamps.append(timestamp)
            else:
                recording = False

        # 解析中心点数据
        if recording:
            center_match = center_pattern.search(line)
            if center_match:
                center_x = float(center_match.group(1))
                center_y = float(center_match.group(2))
                # 仅记录符合范围的中心点
                if -2 <= center_x < 2 and 7 <= center_y < 79:
                    detected_y = center_y  # 记录 y 值

        # 解析帧结束
        if obj_end_pattern.search(line) and recording:
            detected_y_values.append(detected_y)
            if detected_y > 0:
                detected_count += 1
            recording = False

# 过滤掉 `y=0`,找出最小和最大 y 值
valid_y_values = [y for y in detected_y_values if y > 0]  # 去掉 `0`
min_y = min(valid_y_values, default=0)
max_y = max(valid_y_values, default=0)

# 计算统计指标
detection_rate = detected_count / total_frames if total_frames > 0 else 0  # 检出率
miss_rate = 1 - detection_rate  # 漏检率
total_duration = (timestamps[-1] - timestamps[0]) if timestamps else 0

# 创建绘图
fig, ax = plt.subplots(figsize=(12, 6))

# 绘制检测到的 y 值
ax.scatter(timestamps, detected_y_values, color='blue', label='检测到的Y坐标', s=30)

# 设置 x 轴时间格式,每 1 秒一个刻度
ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S'))  # 仅显示 时:分:秒
ax.xaxis.set_major_locator(mdates.SecondLocator(interval=1))  # 每 1 秒一个刻度
plt.xticks(rotation=45, ha='right')

# 添加注释(仅标注检测到的 y 值)
for time, y_value in zip(timestamps, detected_y_values):
    if y_value > 0:
        ax.text(time, y_value + 2, f'{y_value:.1f}', rotation=45, fontsize=8, ha='right', va='bottom', fontproperties=my_font)

# 画出最小和最大 y 值的横线
if min_y > 0:
    ax.axhline(min_y, color='red', linestyle='--', linewidth=1, label=f'最小距离: {min_y:.1f} m')
    ax.text(timestamps[0], min_y, f'  {min_y:.1f} m', color='red', fontsize=10, verticalalignment='bottom', fontproperties=my_font)

if max_y > 0:
    ax.axhline(max_y, color='green', linestyle='--', linewidth=1, label=f'最大距离: {max_y:.1f} m')
    ax.text(timestamps[0], max_y, f'  {max_y:.1f} m', color='green', fontsize=10, verticalalignment='bottom', fontproperties=my_font)

# 图表设置(使用中文)
ax.set_xlabel('时间', fontproperties=my_font)
ax.set_ylabel('检测到的Y坐标', fontproperties=my_font)
ax.set_title(f'检测目标的Y坐标变化({start_time_str} - {end_time_str})', fontproperties=my_font)
ax.set_ylim(0, 80)  # Y 轴范围
ax.grid(True)

# 统计信息 - 在图表外显示
fig.subplots_adjust(right=0.75)  # 让图表左移,给右侧留空白区域

stats_text = (
    f'总帧数: {total_frames}\n'
    f'检测到的帧数: {detected_count}\n'
    f'检出率: {detection_rate:.2%}\n'
    f'漏检率: {miss_rate:.2%}\n'
    f'最小检测距离: {min_y:.1f} m\n'
    f'最大检测距离: {max_y:.1f} m\n'
    f'总时长: {total_duration}'
)

fig.text(0.78, 0.5, stats_text, fontsize=12, verticalalignment='top',
         bbox=dict(facecolor='white', alpha=0.8), fontproperties=my_font)

plt.legend(prop=my_font)
plt.show()

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Philtell

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值