clean unused code and add LOCAL_DEBUG const

Former-commit-id: 39c7c8ba6f9c66e963c176690b8839456d251e25
This commit is contained in:
2025-05-18 00:19:41 +08:00
parent ed4539c56d
commit 11e41764a8

View File

@ -13,6 +13,11 @@ import json
import Find_COM
from threading import Thread
LOCAL_DEBUG = True
if LOCAL_DEBUG:
print("WARNING: Local debug mode is enabled. Serial communication will be skipped.")
time.sleep(2)
class MAT:
def __init__(self, videoSourceIndex=0, weights_path = "resnet34-1Net.pth", json_path = 'class_indices.json', classes = 2):
@ -21,13 +26,13 @@ class MAT:
self.videoSourceIndex = videoSourceIndex # 摄像机编号
self.cap = cv2.VideoCapture(videoSourceIndex, cv2.CAP_DSHOW) # 打开摄像头
self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
self.port = Find_COM.list_ch340_ports()[0] # 串口名
self.pump_ser = serial.Serial(self.port, 9600) # 初始化串口
if not LOCAL_DEBUG:
self.port = Find_COM.list_ch340_ports()[0] # 串口
self.pump_ser = serial.Serial(self.port, 9600) # 初始化串口
self.classes = classes
self.total_volume = 0 # 记录总体积
self.now_volume = 0 # 记录当前注射泵内体积
self.volume_list = [] # 记录体积变化
self.voltage_list = [] # 记录电位变化(如有需要)
self.color_list = [] # 记录颜色变化
self.start_time = time.time() # 记录实验开始时间
self.weights_path = os.path.join(self.data_root, weights_path) # 权重文件路径
@ -38,16 +43,8 @@ class MAT:
self.model = joblib.load("model.pkl")
print("实验开始于", self.formatted_time)
def get_picture(self, frame, typ=0, date=''): # 拍摄照片并保存
if frame is None:
print(frame)
image_name = f'{date}_{self.total_volume}.jpg' # 照片保存在Input文件夹下以开始时间+体积数的方式命名
filepath = os.path.join(self.data_root, "Input", image_name)
str_name = filepath.replace('%s', '1')
cv2.imwrite(str_name, frame)
return image_name
def start_move_1(self): # 抽料程序
if LOCAL_DEBUG:return
data = b"q1h24d" # *2
self.pump_ser.write(data)
time.sleep(0.01)
@ -66,6 +63,9 @@ class MAT:
print('完成抽取')
def start_move_2(self, speed=0.1): # 进料程序
if LOCAL_DEBUG:
time.sleep(1)
return
# 计算单次滴定体积并传输至控制器
speed_min = speed * 30
speed_min_int = int(speed_min)
@ -89,6 +89,7 @@ class MAT:
time.sleep(1)
def start_move_3(self): # 进料急停
if LOCAL_DEBUG:return
data = b"q6h6d"
self.pump_ser.write(data)
@ -177,10 +178,6 @@ class MAT:
return class_a, prob_b
def __del__(self):
# 绘制滴定曲线
# self.line_chart()
# 关闭串口和摄像头
self.pump_ser.close()
self.cap.release()
cv2.destroyAllWindows()
@ -196,10 +193,7 @@ class MAT:
if not cv2.imwrite(name,im):
print("Failed to save image",name)
def run(self,quick_speed = 0.2, mid_speed=0.1,slow_speed = 0.05,expect = 5, end_kind = 'orange', end_prob =0.5):
n = 1
total_n = n
# self.wait = False
def run(self,quick_speed = 0.2, mid_speed=0.1,slow_speed = 0.05,expect = 5, end_kind = 'orange'):
self.running = True
self.end_kind = end_kind
self.cnt = 0
@ -251,7 +245,6 @@ if __name__ == "__main__":
slow_speed = 0.2,
expect = 11.2,
end_kind = 'orange',
end_prob = 0.5
)