已在 mcpclient.py 中添加 自動 CSV 記錄功能,每次呼叫 detect_stream_frame_simple 工具時,會自動將結果記錄到 CSV 檔案。
C:\Users\user\MCPproject-YOLOv8\detection_logs.csv
| 欄位名稱 | 資料類型 | 說明 | 範例 |
|---|---|---|---|
timestamp |
字串 | 偵測時間 | 2025-11-07 14:30:45 |
success |
布林值 | 是否成功 | True / False |
detection_count |
整數 | 偵測到的物體數量 | 2 |
detections |
JSON 字串 | 詳細偵測結果 | [{"class":"dog","confidence":0.92,"bbox":[...]}] |
total_time |
浮點數 | 總處理時間(秒) | 0.123 |
yolo_inference_time |
浮點數 | YOLO 推理時間(秒) | 0.050 |
timestamp,success,detection_count,detections,total_time,yolo_inference_time
2025-11-07 14:30:45,True,2,"[{""class"":""dog"",""confidence"":0.92,""bbox"":[100,200,300,400]},{""class"":""dog"",""confidence"":0.88,""bbox"":[350,180,500,380]}]",0.123,0.05
2025-11-07 14:31:12,True,1,"[{""class"":""cat"",""confidence"":0.85,""bbox":[150,220,280,350]}]",0.118,0.048
2025-11-07 14:31:45,True,0,"[]",0.095,0.042執行偵測:
請使用 detect_stream_frame_simple 工具偵測這個串流:
http://192.168.0.104:81/stream
自動記錄: 每次執行後,結果會自動追加到 detection_logs.csv
方法 1: 使用測試腳本
python test_csv_logging.py方法 2: 直接開啟 CSV
# 用 Excel 開啟
start detection_logs.csv
# 用記事本開啟
notepad detection_logs.csv方法 3: 使用 Python 分析
import pandas as pd
# 讀取 CSV
df = pd.read_csv('detection_logs.csv')
# 顯示最近 10 筆
print(df.tail(10))
# 計算統計
print(f"平均偵測數: {df['detection_count'].mean():.2f}")
print(f"平均時間: {df['total_time'].mean():.3f} 秒")import pandas as pd
import matplotlib.pyplot as plt
# 讀取資料
df = pd.read_csv('detection_logs.csv')
# 轉換時間戳記為 datetime
df['timestamp'] = pd.to_datetime(df['timestamp'])
# 繪製偵測數量趨勢圖
plt.figure(figsize=(12, 6))
plt.plot(df['timestamp'], df['detection_count'], marker='o')
plt.xlabel('時間')
plt.ylabel('偵測數量')
plt.title('物體偵測數量趨勢')
plt.xticks(rotation=45)
plt.tight_layout()
plt.savefig('detection_trend.png')
# 繪製處理時間分布圖
plt.figure(figsize=(10, 5))
plt.hist(df['total_time'], bins=20, alpha=0.7, label='總時間')
plt.hist(df['yolo_inference_time'], bins=20, alpha=0.7, label='YOLO 時間')
plt.xlabel('時間(秒)')
plt.ylabel('次數')
plt.title('處理時間分布')
plt.legend()
plt.tight_layout()
plt.savefig('time_distribution.png')
print("✅ 圖表已儲存")import pandas as pd
df = pd.read_csv('detection_logs.csv')
total = len(df)
success_count = df['success'].sum()
success_rate = (success_count / total) * 100
print(f"總執行次數: {total}")
print(f"成功次數: {success_count}")
print(f"成功率: {success_rate:.2f}%")import csv
import jsonCSV_LOG_PATH = os.path.join(SCRIPT_DIR, "detection_logs.csv")def log_detection_to_csv(result_data):
"""將偵測結果記錄到 CSV 檔案"""
fieldnames = ['timestamp', 'success', 'detection_count', 'detections', 'total_time', 'yolo_inference_time']
file_exists = os.path.isfile(CSV_LOG_PATH)
with open(CSV_LOG_PATH, 'a', newline='', encoding='utf-8') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
if not file_exists:
writer.writeheader()
row_data = {
'timestamp': result_data.get('timestamp', ''),
'success': result_data.get('success', False),
'detection_count': result_data.get('detection_count', 0),
'detections': json.dumps(result_data.get('detections', []), ensure_ascii=False),
'total_time': result_data.get('total_time', 0),
'yolo_inference_time': result_data.get('yolo_inference_time', 0)
}
writer.writerow(row_data)# 在返回結果前記錄
result = {
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"success": True,
"detection_count": len(detections),
"detections": detections,
"total_time": round(total_time, 3),
"yolo_inference_time": round(yolo_time, 3)
}
# ✅ 記錄到 CSV
log_detection_to_csv(result)
return result- 首次執行時會自動建立
detection_logs.csv - 會自動寫入欄位標題列
- 每次執行都會在檔案末尾新增一列
- 不會覆蓋舊資料
- 以 JSON 字串儲存
- 可以用
json.loads()解析
import json
detections = json.loads(row['detections'])success = False時仍會記錄- 可用於分析失敗原因
# 刪除 CSV 檔案
Remove-Item detection_logs.csv
# 下次執行時會自動重新建立# 複製檔案並加上日期
Copy-Item detection_logs.csv "detection_logs_backup_$(Get-Date -Format 'yyyyMMdd').csv"如果記錄太多,可以定期清理:
import pandas as pd
# 只保留最近 1000 筆記錄
df = pd.read_csv('detection_logs.csv')
df_recent = df.tail(1000)
df_recent.to_csv('detection_logs.csv', index=False)# 1. 完全關閉 Claude Desktop
taskkill /F /IM "Claude.exe"
# 2. 等待 15 秒
Start-Sleep -Seconds 15
# 3. 重新啟動
Start-Process "C:\Users\user\AppData\Local\Programs\claude\Claude.exe"
# 4. 等待 30 秒
Start-Sleep -Seconds 30請使用 detect_stream_frame_simple 工具偵測串流 http://192.168.0.104:81/stream
python test_csv_logging.py重複執行步驟 2,觀察 CSV 檔案的增長
import pandas as pd
df = pd.read_csv('detection_logs.csv')
print(df.describe())- ✅
detection_logs.csv檔案自動建立 - ✅ 每次執行偵測後,CSV 增加一列
- ✅ 所有 6 個欄位都有正確的值
- ✅ 時間戳記格式正確 (
YYYY-MM-DD HH:MM:SS) - ✅ detections 欄位包含有效的 JSON
- ✅ 成功和失敗的執行都被記錄
- 記錄不同參數下的處理時間
- 分析 YOLO 推理效能
- 統計偵測數量分布
- 分析信心度變化
- 追蹤成功率
- 記錄錯誤模式
- 累積實驗數據
- 用於論文或報告
版本: 1.0
最後更新: 2025-11-07
相容: Python 3.7+
依賴: csv, json, pandas (選用,用於分析)