fix:修复一些小问题,添加简单番茄背景判断,如果直径小于200,那么全赋0.返回(100,100,3)空图;由于耀哥&旻哥暂时没写管道预热,预热部分注释掉

This commit is contained in:
TG 2024-06-19 08:54:01 +08:00
parent e14f5f1f4e
commit 86cf933d24
3 changed files with 26 additions and 19 deletions

View File

@ -359,7 +359,7 @@ class Data_processing:
def contour_process(self, image_array):
# 检查图像是否为空或全黑
if image_array is None or image_array.size == 0 or np.all(image_array == 0):
# print("输入的图像为空或全黑,返回一个全黑图像。")
logging.error("输入的图像为空或全黑,返回一个全黑图像。")
return np.zeros_like(image_array) if image_array is not None else np.zeros((100, 100), dtype=np.uint8)
# 应用中值滤波
image_filtered = cv2.medianBlur(image_array, 5)
@ -488,11 +488,18 @@ class Data_processing:
# 将处理后的图像转换为 RGB 格式
rp = cv2.cvtColor(org_defect, cv2.COLOR_BGR2RGB)
diameter = (long_axis + short_axis) / 2
if diameter < 200:
diameter = 0
green_percentage = 0
number_defects = 0
total_pixels = 0
rp = cv2.cvtColor(np.ones((100, 100, 3), dtype=np.uint8), cv2.COLOR_BGR2RGB)
return diameter, green_percentage, number_defects, total_pixels, rp
return diameter, green_percentage, number_defects, total_pixels, rp
def analyze_passion_fruit(self, img, hue_value=37, hue_delta=10, value_target=25, value_delta=10):
if img is None:
print("Error: 无图像数据.")
logging.error("Error: 无图像数据.")
return None
# 创建PassionFruit类的实例

View File

@ -99,14 +99,14 @@ def main(is_debug=False):
pipe = Pipe(rgb_receive_name, rgb_send_name, spec_receive_name)
rgb_receive, rgb_send, spec_receive = pipe.create_pipes(rgb_receive_name, rgb_send_name, spec_receive_name)
# 预热循环只处理cmd为'YR'的数据
while True:
start_time00 = time.time()
data = pipe.receive_rgb_data(rgb_receive)
cmd, _ = pipe.parse_img(data)
end_time00 = time.time()
print(f'接收预热数据时间:{end_time00 - start_time00}')
if cmd == 'YR':
break # 当接收到的不是预热命令时,结束预热循环
# while True:
# start_time00 = time.time()
# data = pipe.receive_rgb_data(rgb_receive)
# cmd, _ = pipe.parse_img(data)
# end_time00 = time.time()
# print(f'接收预热数据时间:{end_time00 - start_time00}秒')
# if cmd == 'YR':
# break # 当接收到的不是预热命令时,结束预热循环
while True:
start_time = time.time()
images = []
@ -115,13 +115,13 @@ def main(is_debug=False):
start_time1 = time.time()
data = pipe.receive_rgb_data(rgb_receive)
end_time10 = time.time()
print(f'接收一份数据时间:{end_time10 - start_time1}')
# print(f'接收一份数据时间:{end_time10 - start_time1}秒')
start_time11 = time.time()
cmd, img = pipe.parse_img(data)
end_time1 = time.time()
print(f'处理一份数据时间:{end_time1 - start_time11}')
print(f'接收一张图时间:{end_time1 - start_time1}')
# print(f'处理一份数据时间:{end_time1 - start_time11}秒')
# print(f'接收一张图时间:{end_time1 - start_time1}秒')
# 使用分类器进行预测
# prediction = classifier.predict(img)
@ -133,7 +133,7 @@ def main(is_debug=False):
else:
response = pipe.send_data(cmd='KO', brix=0, diameter=0, green_percentage=0, weigth=0, defect_num=0,
total_defect_area=0, rp=np.zeros((100, 100, 3), dtype=np.uint8))
print("图像中无果,跳过此图像")
logging.info("图像中无果,跳过此图像")
continue
if cmd not in ['TO', 'PF', 'YR', 'KO']:
@ -146,19 +146,19 @@ def main(is_debug=False):
spec_data = pipe.receive_spec_data(spec_receive)
_, spec = pipe.parse_spec(spec_data)
end_time2 = time.time()
print(f'接收光谱数据时间:{end_time2 - start_time2}')
# print(f'接收光谱数据时间:{end_time2 - start_time2}秒')
start_time3 = time.time()
if images: # 确保images不为空
response = process_data(cmd, images, spec, dp, pipe, detector)
end_time3 = time.time()
print(f'处理时间:{end_time3 - start_time3}')
# print(f'处理时间:{end_time3 - start_time3}秒')
if response:
logging.info(f'处理成功,响应为: {response}')
else:
logging.error('处理失败')
else:
print("没有有效的图像进行处理")
logging.error("没有有效的图像进行处理")
end_time = time.time()
print(f'全流程时间:{end_time - start_time}')

View File

@ -90,7 +90,7 @@ class MainWindow(QMainWindow):
img_data = img.tobytes()
length = (len(img_data) + 6).to_bytes(4, byteorder='big')
# cmd = 'TO'测试番茄数据cmd = 'PF':测试百香果数据
cmd = 'PF'
cmd = 'TO'
data_send = length + cmd.upper().encode('ascii') + height + width + img_data
win32file.WriteFile(self.rgb_send, data_send)
print(f'发送的图像数据长度: {len(data_send)}')
@ -113,7 +113,7 @@ class MainWindow(QMainWindow):
bands = bands.to_bytes(2, byteorder='big')
length = (len(spec_data)+8).to_bytes(4, byteorder='big')
# cmd = 'TO'测试番茄数据cmd = 'PF':测试百香果数据
cmd = 'PF'
cmd = 'TO'
data_send = length + cmd.upper().encode('ascii') + heigth + weight + bands + spec_data
win32file.WriteFile(self.spec_send, data_send)
print(f'发送的光谱数据长度: {len(data_send)}')