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'
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
if obj_end_pattern.search(line) and recording:
detected_y_values.append(detected_y)
if detected_y > 0:
detected_count += 1
recording = False
valid_y_values = [y for y in detected_y_values if y > 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))
ax.scatter(timestamps, detected_y_values, color='blue', label='检测到的Y坐标', s=30)
ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S'))
ax.xaxis.set_major_locator(mdates.SecondLocator(interval=1))
plt.xticks(rotation=45, ha='right')
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)
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)
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()
