upload
Former-commit-id: 3af1a19b5b0e0bb4dbc367468b36c62aa8762a91
This commit is contained in:
95
tmp.py
95
tmp.py
@ -1,80 +1,23 @@
|
||||
import numpy as np
|
||||
|
||||
class DistributionChangeDetector:
|
||||
def __init__(self, baseline_windows: list[np.ndarray]):
|
||||
"""
|
||||
参数 baseline_windows: List of arrays,代表初始稳定期的多个窗口
|
||||
"""
|
||||
self.baseline = self._compute_baseline(baseline_windows)
|
||||
|
||||
def _compute_stats(self, window: np.ndarray) -> tuple[float, float, float]:
|
||||
"""返回 (P_under30, std, mode)"""
|
||||
p_under30 = np.mean(window < 30)
|
||||
std = np.std(window, ddof=1)
|
||||
|
||||
# 快速估计众数:最大 bin 的中心
|
||||
hist, bin_edges = np.histogram(window, bins=50)
|
||||
max_bin_index = np.argmax(hist)
|
||||
mode_est = (bin_edges[max_bin_index] + bin_edges[max_bin_index + 1]) / 2
|
||||
return p_under30, std, mode_est
|
||||
|
||||
def _compute_baseline(self, windows: list[np.ndarray]) -> tuple[np.ndarray, np.ndarray]:
|
||||
"""
|
||||
返回 baseline 向量 (P0, σ0, mode0) 和对应标准差(用于归一化)
|
||||
"""
|
||||
stats = np.array([self._compute_stats(w) for w in windows])
|
||||
mean = stats.mean(axis=0)
|
||||
std = stats.std(axis=0) + 1e-6 # 防止除0
|
||||
return mean, std
|
||||
|
||||
def update(self, window: np.ndarray) -> float:
|
||||
"""
|
||||
输入:当前窗口数据(长度 = 窗口大小)
|
||||
输出:变化分数(越大表示分布越偏离基准)
|
||||
"""
|
||||
x = np.array(self._compute_stats(window))
|
||||
mean, std = self.baseline
|
||||
norm_diff = (x - mean) / std
|
||||
change_score = np.linalg.norm(norm_diff)
|
||||
return float(change_score)
|
||||
from datetime import datetime
|
||||
import json
|
||||
import requests
|
||||
import time
|
||||
from utils import login_to_platform,send_data_to_platform
|
||||
|
||||
|
||||
import cv2
|
||||
def gen_data():
|
||||
cap = cv2.VideoCapture()
|
||||
cap.open(1)
|
||||
while True:
|
||||
ret, frame = cap.read()
|
||||
cv2.imshow("Camera Feed", frame)
|
||||
if not ret:
|
||||
break
|
||||
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
|
||||
s = hsv[:, :, 1] # 直接提取饱和度通道
|
||||
s = s[s > 0] # 只保留非零饱和度值,减少噪声
|
||||
yield s
|
||||
if cv2.waitKey(1) & 0xFF == ord('a'):
|
||||
break
|
||||
token = login_to_platform("13504022184","password")
|
||||
|
||||
def format_date_time(timestamp):
|
||||
return datetime.fromtimestamp(timestamp).strftime("%Y-%m-%d %H:%M:%S")
|
||||
|
||||
gen = gen_data()
|
||||
baseline_data = [gen.__next__() for _ in range(5)] # 获取10个窗口作为基线
|
||||
|
||||
det = DistributionChangeDetector(baseline_data)
|
||||
|
||||
|
||||
|
||||
results = []
|
||||
for x in gen:
|
||||
out = det.update(x)
|
||||
if out is not None:
|
||||
results.append(out)
|
||||
|
||||
|
||||
# 作图查看
|
||||
import matplotlib.pyplot as plt
|
||||
plt.plot(results, label="ChangeScore")
|
||||
plt.xlabel("Window index")
|
||||
plt.ylabel("Score")
|
||||
plt.title("Streaming Change Detection")
|
||||
plt.legend()
|
||||
plt.show()
|
||||
print(token)
|
||||
final_data ={
|
||||
'start_time': '2025-06-17 14:59:33',
|
||||
'end_time': '2025-06-17 15:01:18',
|
||||
'volume_record': '[0.8, 1.6, 2.4000000000000004, 3.2, 4.0, 4.8, 5.6, 6.3999999999999995, 7.199999999999999, 7.999999999999999, 8.799999999999999, 9.6, 10.4, 11.200000000000001, 12.000000000000002, 12.800000000000002, 13.200000000000003, 13.600000000000003, 14.000000000000004, 14.400000000000004, 14.800000000000004, 15.200000000000005, 15.600000000000005, 16.000000000000004, 16.400000000000002, 16.8, 17.2, 17.599999999999998, 17.999999999999996, 18.399999999999995, 18.799999999999994, 19.199999999999992, 19.59999999999999, 19.99999999999999, 20.399999999999988, 20.799999999999986, 21.199999999999985, 21.599999999999984, 21.999999999999982, 22.39999999999998, 22.79999999999998, 23.199999999999978, 23.599999999999977, 23.999999999999975, 24.399999999999974, 24.799999999999972, 25.19999999999997, 25.59999999999997, 25.999999999999968, 26.399999999999967, 26.799999999999965, 27.199999999999964, 27.599999999999962, 27.99999999999996, 28.39999999999996, 28.447450351715048, 28.49745035171505, 28.54745035171505, 28.59745035171505, 28.98371173143383, 28.781944286823233, 28.761131591796836]',
|
||||
'voltage_record': "[]",
|
||||
'color_record': "['transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'transport', 'middle', 'transport', 'transport', 'transport', 'transport', 'middle', 'colored']",
|
||||
'final_volume': "28.761131591796836"
|
||||
}
|
||||
# print(json.dumps(final_data))
|
||||
send_data_to_platform(token, final_data, "im.jpg")
|
Reference in New Issue
Block a user