history
Former-commit-id: c078eab42975bcbc5fd98aae28228937b5694d19
This commit is contained in:
84
main.py
84
main.py
@ -5,7 +5,7 @@ import numpy as np
|
||||
import ch340
|
||||
import atexit
|
||||
import utils
|
||||
from utils import State
|
||||
from utils import State, History
|
||||
|
||||
|
||||
class MAT:
|
||||
@ -17,7 +17,7 @@ class MAT:
|
||||
self.endpoint_logger = utils.get_endpoint_logger()
|
||||
self.volume_logger = utils.get_volume_logger()
|
||||
|
||||
self.system_logger.info('正在初始化MAT系统...')
|
||||
self.system_logger.info('正在初始化MAT系统...')
|
||||
self.videoSourceIndex = videoSourceIndex
|
||||
self.cap = cv2.VideoCapture(videoSourceIndex, cv2.CAP_DSHOW)
|
||||
self.ch340 = ch340.CH340()
|
||||
@ -26,7 +26,7 @@ class MAT:
|
||||
self.state = State(bounce_time, end_bounce_time)
|
||||
|
||||
atexit.register(self.ch340.stop)
|
||||
self.history = []
|
||||
self.history = History(max(bounce_time, end_bounce_time))
|
||||
self.colored_volume = None
|
||||
self.colored_time = None
|
||||
self.colored_im = None
|
||||
@ -53,7 +53,7 @@ class MAT:
|
||||
r = now - st
|
||||
ret = self.total_volume - (1-r) * self.speeds[self.state.mode.value]
|
||||
if value_only:
|
||||
return ret
|
||||
return ret
|
||||
else:
|
||||
self.total_volume = ret
|
||||
|
||||
@ -71,16 +71,18 @@ class MAT:
|
||||
ret, rate = self.predictor(im)
|
||||
if ret is None:
|
||||
return None
|
||||
|
||||
now = time.time()
|
||||
val = self.process_left(now, value_only=True)
|
||||
|
||||
# 更新滑动窗口历史记录 - 维护最近end_bounce_time内的状态
|
||||
self.history.append((now, ret, val, im))
|
||||
while self.history and self.history[0][0] < now - self.state.end_bounce_time:
|
||||
self.history.pop(0)
|
||||
# 确保val不为None
|
||||
if val is None:
|
||||
self.control_logger.warning("体积计算返回None,跳过本次记录")
|
||||
return ret
|
||||
|
||||
if not self.history:
|
||||
# 更新滑动窗口历史记录 - 维护最近end_bounce_time内的状态
|
||||
self.history.add_record(now, ret,rate, val, im)
|
||||
|
||||
if self.history.is_empty():
|
||||
self.control_logger.error("未预期的没有可用的历史记录")
|
||||
return ret
|
||||
|
||||
@ -111,49 +113,44 @@ class MAT:
|
||||
self.colored_time = now
|
||||
self.colored_im = im.copy()
|
||||
self.endpoint_logger.info(f"检测到colored,开始end检查,记录体积: {val:.2f} ml")
|
||||
|
||||
# === 状态检查逻辑 ===
|
||||
# === 状态检查逻辑 ===
|
||||
|
||||
# middle检查: 进入middle的bounce_time后,在最近bounce_time内middle比例<70%返回fast状态
|
||||
if self.state.should_check_middle_exit(now):
|
||||
# 计算最近bounce_time内的middle比例
|
||||
bounce_start_time = now - self.state.bounce_time
|
||||
recent_history = [(t, state, v, i) for t, state, v, i in self.history if t >= bounce_start_time]
|
||||
recent_records = self.history.get_recent_records(self.state.bounce_time, now)
|
||||
|
||||
if recent_history:
|
||||
trans_count = sum(1 for _, state, _, _ in recent_history if state == "transport")
|
||||
trans_ratio = trans_count / len(recent_history)
|
||||
if recent_records:
|
||||
trans_ratio = self.history.get_state_ratio("transport", recent_records)
|
||||
|
||||
if trans_ratio > 0.3:
|
||||
self.process_left(now)
|
||||
self.state.exit_middle_check()
|
||||
# about状态随middle一起退出
|
||||
self.state.exit_about_with_middle()
|
||||
self.state.exit_about_with_middle()
|
||||
self.control_logger.info(f"middle比例{trans_ratio:.2%}<70%,退出middle检查,返回fast模式")
|
||||
|
||||
# end检查: 进入end之后的end_bounce_time,如果end比例<80%,则重置;否则终止实验
|
||||
if self.state.should_check_end_result(now):
|
||||
colored_count = sum(1 for _, state, _, _ in self.history if state == "colored")
|
||||
colored_ratio = colored_count / len(self.history)
|
||||
colored_ratio = self.history.get_state_ratio("colored", self.history.get_recent_records(self.state.end_bounce_time, now))
|
||||
|
||||
if colored_ratio < 0.8:
|
||||
# end比例<80%,从history中找到第二个end并继续check逻辑
|
||||
self.endpoint_logger.warning(f"colored比例{colored_ratio:.2%}<80%,寻找下一个colored点")
|
||||
|
||||
# 寻找历史中倒数第二个colored状态
|
||||
colored_times = [t for t, state, _, _ in self.history if state == "colored"]
|
||||
colored_times = self.history.get_states_by_type("colored")
|
||||
if len(colored_times) >= 2:
|
||||
# 使用倒数第二个colored时间重新开始检查
|
||||
second_last_colored_time = colored_times[1]
|
||||
self.state.end_detected_time = second_last_colored_time
|
||||
|
||||
# 更新colored记录为对应的体积
|
||||
for t, state, vol, img in self.history:
|
||||
if t == second_last_colored_time and state == "colored":
|
||||
self.colored_volume = vol
|
||||
self.colored_time = t
|
||||
self.colored_im = img.copy()
|
||||
break
|
||||
record = self.history.find_record_by_timestamp(second_last_colored_time)
|
||||
if record:
|
||||
self.colored_volume = record.volume
|
||||
self.colored_time = record.timestamp
|
||||
self.colored_im = record.image.copy()
|
||||
|
||||
self.endpoint_logger.info(f"重置到第二个colored点: {self.colored_volume:.2f} ml")
|
||||
else:
|
||||
@ -170,7 +167,8 @@ class MAT:
|
||||
if self.colored_im is not None:
|
||||
cv2.imwrite(f"colored_{datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg", self.colored_im)
|
||||
return "colored"
|
||||
# 显示状态信息
|
||||
|
||||
# 显示状态信息
|
||||
self._display_status(im, ret, rate, val)
|
||||
return ret
|
||||
|
||||
@ -199,14 +197,20 @@ class MAT:
|
||||
tot = mask.shape[0]*mask.shape[1]
|
||||
val = np.sum(mask)
|
||||
rate = val/tot
|
||||
if rate < 0.02:
|
||||
return "transport",rate
|
||||
elif rate <0.2:
|
||||
return "middle",rate
|
||||
elif rate < 0.35:
|
||||
return "about",rate
|
||||
if self.history.base is not None:
|
||||
base = self.history.base
|
||||
thr = (base*5, base*10, base*30)
|
||||
if rate < thr[0]:
|
||||
return "transport",rate
|
||||
elif rate <thr[1]:
|
||||
return "middle",rate
|
||||
elif rate < thr[2]:
|
||||
return "about",rate
|
||||
else:
|
||||
return "colored",rate
|
||||
else:
|
||||
return "colored",rate
|
||||
return "transport",rate
|
||||
|
||||
|
||||
def __del__(self):
|
||||
self.cap.release()
|
||||
@ -236,11 +240,11 @@ class MAT:
|
||||
self.system_logger.info("实验参数:")
|
||||
for key, value in experiment_params.items():
|
||||
self.system_logger.info(f" {key}: {value}")
|
||||
|
||||
fps = int(self.cap.get(cv2.CAP_PROP_FPS)) or 30
|
||||
width = int(self.cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
||||
height = int(self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
||||
# 初始化视频录制器
|
||||
|
||||
# 初始化视频录制器
|
||||
if vid_name != "Disabled":
|
||||
fourcc = cv2.VideoWriter.fourcc(*'x264') # 使用更兼容的编码器
|
||||
self.capFile = cv2.VideoWriter(vid_name, fourcc, fps, (width, height))
|
||||
@ -302,14 +306,14 @@ if __name__ == "__main__":
|
||||
mat = MAT(
|
||||
videoSourceIndex = 1,
|
||||
bounce_time=3,
|
||||
end_bounce_time=15
|
||||
end_bounce_time=0.01
|
||||
)
|
||||
|
||||
mat.run(
|
||||
slow_speed = 0.05,
|
||||
quick_speed = 0.15,
|
||||
end_time= 2
|
||||
# cap_dir=None
|
||||
end_time= 2,
|
||||
cap_dir=None
|
||||
)
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user