From ed4539c56d7dd8f1318ed20c86d040e68de7d479 Mon Sep 17 00:00:00 2001 From: flt6 <1404262047@qq.com> Date: Sun, 18 May 2025 00:10:51 +0800 Subject: [PATCH] clean unused code Former-commit-id: 3b6d3de61757e8833c8066935ccf468acd208eae --- Auto_Ctrl/predictor_Syringe_Pump.py | 96 +---------------------------- 1 file changed, 3 insertions(+), 93 deletions(-) diff --git a/Auto_Ctrl/predictor_Syringe_Pump.py b/Auto_Ctrl/predictor_Syringe_Pump.py index fc8a1dc..e9f326c 100644 --- a/Auto_Ctrl/predictor_Syringe_Pump.py +++ b/Auto_Ctrl/predictor_Syringe_Pump.py @@ -1,16 +1,12 @@ import torch from PIL import Image import torchvision.transforms as transforms -from torch.autograd import Variable -from torch import nn -import matplotlib.pyplot as plt import cv2 import time import os from model import resnet34 import serial from datetime import datetime -from scipy.optimize import curve_fit import numpy as np import joblib import json @@ -27,9 +23,6 @@ class MAT: 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) # 初始化串口 - # self.usb_port = Find_COM.list_USB_ports() # 串口名 - # if self.usb_port: - # self.usb_ser = serial.Serial(self.usb_port, 115200) # 初始化串口 self.classes = classes self.total_volume = 0 # 记录总体积 self.now_volume = 0 # 记录当前注射泵内体积 @@ -99,75 +92,6 @@ class MAT: data = b"q6h6d" self.pump_ser.write(data) - def voltage(self): # 测量电位 - self.usb_ser.write("VOL|\n".encode()) - time.sleep(0.1) - while True: - response = self.usb_ser.readline().decode().strip() - if response: - try: - return float(response) - except: - return 0 - - @staticmethod - def poly_func(x, a, b, c, d): - return a * np.tanh(d * x + b) + c - - def line_chart(self): - x = self.volume_list - y = self.voltage_list - z = self.color_list - - fig, ax1 = plt.subplots() - plt.title("titration curve") - color = 'tab:red' - ax1.set_xlabel('value') - ax1.set_ylabel('voltage', color=color) - ax1.plot(x, y, color=color, antialiased=True) - ax1.tick_params(axis='y', labelcolor=color) - - ax2 = ax1.twinx() - color = 'tab:blue' - ax2.set_ylabel('color', color=color) - ax2.plot(x, z, color=color) - ax2.tick_params(axis='y', labelcolor=color) - ax2.set_yticks([0, 1]) - ax2.set_yticklabels(['yellow', 'orange']) - ax2.spines['right'].set_position(('outward', 60)) - - try: - popt, pcov = curve_fit(self.poly_func, x, y, p0=[max(y) * 3 / 4, -max(x), max(y), 1.5]) - print("最优参数:", popt) - print(f'电位突跃点:{-popt[1] / popt[3]:.3f}') - x_d = np.arange(0, max(x), 0.05) - y_fit = self.poly_func(x_d, *popt) - dE_dV = np.gradient(y_fit) - d2E_dV2 = np.gradient(dE_dV) - y2 = d2E_dV2.tolist() - - ax3 = ax1.twinx() - color = 'tab:green' - ax3.set_ylabel('2nd Derivative', color=color) - ax3.plot(x_d, y2, color=color) - ax3.tick_params(axis='y', labelcolor=color) - ax3.grid(True, linestyle='--', linewidth=0.5, color='gray', axis='both') - - x_d, y_d = -popt[1] / popt[3], 0.0 - ax3.plot(x_d, y_d, 'ro') - ax3.annotate(f'({x_d:.2f})', xy=(x_d, y_d), color='red', xytext=(x_d - 1, y_d + max(y2) / 10)) - - except Exception as e: - print(e) - pass - - fig.tight_layout() - plt.savefig(f'Output/{self.formatted_time}.png') - plt.show() - plt.pause(1) - plt.close() - - def preproc(self, im): try: hsv = cv2.cvtColor(im,cv2.COLOR_BGR2HSV) @@ -264,6 +188,9 @@ class MAT: def save_img(self): suc,im = self.cap.read() + if not suc: + print("Failed to capture frame from camera.") + return cv2.imshow("new",im) name = f"Imgs/{self.formatted_time}_{self.total_volume}.jpg" if not cv2.imwrite(name,im): @@ -297,28 +224,11 @@ class MAT: self.total_volume = round(self.total_volume, 3) - # suc,im = self.cap.read() - # cv2.imshow('Color', im) - # cv2.waitKey(1) - - # class_a, prob_b = self.my_predictor(im_file) - # class_a, prob_b = self.predictor(im_file) self.volume_list.append(self.total_volume) self.save_img() cv2.waitKey(1) - # 如果有电压测量设备,可以在这里读取电压 - # self.voltage_list.append(self.voltage()) - - # if class_a == end_kind and prob_b > end_prob: # 判断终点 - # print('----->>Visual Endpoint<<-----') - # print(f"Total Volume: {self.total_volume} ml") - # print(f"Image File: {im_file}") - # self.color_list.append(1) - # break - # else: - # self.color_list.append(0) print(f"Current Total Volume: {self.total_volume} ml") self.save_img() print('----->>Visual Endpoint<<-----')