0517
Former-commit-id: 3aa4827fc8d838566a43ce36572a3cc975199242
This commit is contained in:
4
Auto_Ctrl/.gitignore
vendored
Normal file
4
Auto_Ctrl/.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
Input
|
||||
Output
|
||||
Imgs
|
||||
*.jpg
|
3
Auto_Ctrl/model-old.pkl
Normal file
3
Auto_Ctrl/model-old.pkl
Normal file
@ -0,0 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:1ca6d267a1b151f02a2096b1e7fbcea8abc298b6feec224c200a8b9a81fc2fc8
|
||||
size 107513
|
3
Auto_Ctrl/model.pkl
Normal file
3
Auto_Ctrl/model.pkl
Normal file
@ -0,0 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:1ca6d267a1b151f02a2096b1e7fbcea8abc298b6feec224c200a8b9a81fc2fc8
|
||||
size 107513
|
@ -12,10 +12,10 @@ import serial
|
||||
from datetime import datetime
|
||||
from scipy.optimize import curve_fit
|
||||
import numpy as np
|
||||
import re
|
||||
import joblib
|
||||
import json
|
||||
import Find_COM
|
||||
import builtins
|
||||
from threading import Thread
|
||||
|
||||
|
||||
class MAT:
|
||||
@ -27,9 +27,9 @@ 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.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 # 记录当前注射泵内体积
|
||||
@ -42,6 +42,7 @@ class MAT:
|
||||
# 将开始时间转化为年月日时分秒的格式,后续文件命名都已此命名
|
||||
self.formatted_time = datetime.fromtimestamp(self.start_time).strftime('%Y%m%d_%H%M%S')
|
||||
|
||||
self.model = joblib.load("model.pkl")
|
||||
print("实验开始于", self.formatted_time)
|
||||
|
||||
def get_picture(self, frame, typ=0, date=''): # 拍摄照片并保存
|
||||
@ -166,6 +167,60 @@ class MAT:
|
||||
plt.pause(1)
|
||||
plt.close()
|
||||
|
||||
|
||||
def preproc(self, im):
|
||||
try:
|
||||
hsv = cv2.cvtColor(im,cv2.COLOR_BGR2HSV)
|
||||
mask = hsv[:,:,1] > 150
|
||||
mask = mask[:,:,np.newaxis]
|
||||
cnt = np.count_nonzero(mask)
|
||||
hsv*=mask
|
||||
h = round(np.sum(hsv[:,:,0])/cnt)
|
||||
s = round(np.sum(hsv[:,:,1])/cnt)
|
||||
v = round(np.sum(hsv[:,:,2])/cnt)
|
||||
return h,s,v
|
||||
except Exception as e :
|
||||
print(e)
|
||||
return None
|
||||
# name = f"{cl}_{h}_{s}_{v}.jpg"
|
||||
|
||||
def _pred(self):
|
||||
suc,im = self.cap.read()
|
||||
if not suc:
|
||||
print("Failed to capture frame from camera.")
|
||||
return None
|
||||
|
||||
ret = self.my_predictor(im)
|
||||
if ret is None:
|
||||
cv2.imwrite("tmp.jpg",im)
|
||||
return self.predictor("tmp.jpg")
|
||||
else:
|
||||
if ret == self.end_kind:
|
||||
print("Stop at ",self.total_volume)
|
||||
self.running = False
|
||||
self.start_move_3()
|
||||
else:
|
||||
self.thr = Thread(target=self._pred).start()
|
||||
return ret,0.9
|
||||
|
||||
def my_predictor(self,im):
|
||||
model = self.model
|
||||
ret = self.preproc(im)
|
||||
if ret is None:
|
||||
return None
|
||||
arr = np.array(ret)
|
||||
if len(arr.shape) == 1:
|
||||
arr = arr.reshape(1, -1)
|
||||
|
||||
# 进行预测并转换为类别名
|
||||
pred_labels = model.predict(arr)
|
||||
# pred_classes = [label_map[label] for label in pred_labels]
|
||||
mp = ["orange", "yellow"]
|
||||
if len(pred_labels) == 1:
|
||||
return mp[pred_labels[0]]
|
||||
else:
|
||||
return None
|
||||
|
||||
def predictor(self, im_file): # 预测分类
|
||||
image = Image.open(im_file)
|
||||
data_transform = transforms.Compose([
|
||||
@ -207,11 +262,24 @@ class MAT:
|
||||
cv2.destroyAllWindows()
|
||||
print("Experiment finished.")
|
||||
|
||||
def save_img(self):
|
||||
suc,im = self.cap.read()
|
||||
cv2.imshow("new",im)
|
||||
name = f"Imgs/{self.formatted_time}_{self.total_volume}.jpg"
|
||||
if not cv2.imwrite(name,im):
|
||||
print("Failed to save image",name)
|
||||
|
||||
def run(self,quick_speed = 0.2, slow_speed = 0.05,switching_point = 5, end_kind = 'orange', end_prob =0.5):
|
||||
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
|
||||
while True:
|
||||
# self.wait = False
|
||||
self.running = True
|
||||
self.end_kind = end_kind
|
||||
self.cnt = 0
|
||||
self.thr = Thread(target=self._pred)
|
||||
self.thr.start()
|
||||
switching_point = expect * 0.9
|
||||
while self.running:
|
||||
if self.now_volume <= 0:
|
||||
self.start_move_1() # 抽取12ml
|
||||
self.now_volume += 12
|
||||
@ -229,44 +297,37 @@ class MAT:
|
||||
|
||||
self.total_volume = round(self.total_volume, 3)
|
||||
|
||||
# 读取图片
|
||||
ret, frame = self.cap.read()
|
||||
if not ret:
|
||||
print("Failed to capture frame from camera.")
|
||||
break
|
||||
# suc,im = self.cap.read()
|
||||
# cv2.imshow('Color', im)
|
||||
# cv2.waitKey(1)
|
||||
|
||||
name = self.get_picture(frame, 0, self.formatted_time)
|
||||
im_file = 'Input/' + name
|
||||
|
||||
cv2.imshow('Color', frame)
|
||||
cv2.waitKey(1)
|
||||
|
||||
class_a, prob_b = self.predictor(im_file)
|
||||
# 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)
|
||||
|
||||
# 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<<-----')
|
||||
print(f"Total Volume: {self.total_volume} ml")
|
||||
# print(f"Image File: {im_file}")
|
||||
print("Volume List:", self.volume_list)
|
||||
print("Voltage List:", self.voltage_list)
|
||||
print("Color List:", self.color_list)
|
||||
|
||||
# 保存实验数据到JSON文件
|
||||
with builtins.open(f'Output/{self.formatted_time}.json', 'w') as f:
|
||||
json.dump(
|
||||
{"volume_list": self.volume_list, 'voltage_list': self.voltage_list, 'color_list': self.color_list},
|
||||
f)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import warnings
|
||||
@ -275,6 +336,12 @@ if __name__ == "__main__":
|
||||
|
||||
# 创建MAT类的实例并运行
|
||||
mat = MAT(videoSourceIndex = 0, weights_path = "resnet34-1Net.pth", json_path = 'class_indices.json', classes = 2)
|
||||
mat.run(quick_speed = 0.2, slow_speed = 0.05, switching_point = 5, end_kind = 'orange', end_prob = 0.5)
|
||||
mat.run(
|
||||
quick_speed = 0.3,
|
||||
slow_speed = 0.2,
|
||||
expect = 11.2,
|
||||
end_kind = 'orange',
|
||||
end_prob = 0.5
|
||||
)
|
||||
|
||||
|
||||
|
1
Picture_Train/.gitignore
vendored
Normal file
1
Picture_Train/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
data
|
Reference in New Issue
Block a user