fix:修复百香果模型在处理空果拖时gp计算分母可能为0的问题

This commit is contained in:
TG 2024-06-27 09:36:49 +08:00
parent f10aee7753
commit 0d10cf5d23
5 changed files with 16 additions and 9 deletions

View File

@ -373,6 +373,7 @@ class Spec_predict(object):
self.model = model_dic
self.log.log(f'Model loaded successfully from {path}')
def predict(self, data_x):
'''
预测数据
@ -381,7 +382,7 @@ class Spec_predict(object):
'''
# 对数据进行切片,筛选谱段
#qt_test进行测试时如果读取的是3030224需要解开注释进行数据切片筛选谱段
# data_x = data_x[ :25, :, setting.selected_bands ]
data_x = data_x[ :25, :, setting.selected_bands ]
# 将筛选后的数据重塑为二维数组,每行代表一个样本
data_x = data_x.reshape(-1, setting.n_spec_rows * setting.n_spec_cols * setting.n_spec_bands)
data_y = self.model.predict(data_x)
@ -574,6 +575,9 @@ class Data_processing:
# print(np.sum(fore_g_r_t == 255))
# print(np.sum(mask == 255))
# print(np.sum(fore_g_r_t == 255) / np.sum(mask == 255))
if np.sum(mask == 255) == 0:
green_percentage = 0
else:
green_percentage = np.sum(fore_g_r_t == 255) / np.sum(mask == 255)
green_percentage = round(green_percentage, 2)
# 获取西红柿的尺寸信息
@ -616,6 +620,9 @@ class Data_processing:
fore = pf.bitwise_and_rgb_with_binary(img, contour_mask)
mask = pf.extract_green_pixels_cv(fore)
green_img = pf.pixel_comparison(defect, mask)
if np.sum(contour_mask == 255) == 0:
green_percentage = 0
else:
green_percentage = np.sum(green_img == 255) / np.sum(contour_mask == 255)
green_percentage = round(green_percentage, 2)
long_axis, short_axis = self.analyze_ellipse(contour_mask)

View File

@ -31,8 +31,8 @@ def main(is_debug=False):
dp = Data_processing()
print('系统初始化中...')
#模型预热
#与qt_test测试时需要注释掉预热模型接收尺寸为253013qt_test发送的数据为3030224需要对数据进行切片classifer.py第352行)
_ = detector.predict(np.ones((setting.n_spec_rows, setting.n_spec_cols, setting.n_spec_bands), dtype=np.uint16))
#与qt_test测试时需要注释掉预热模型接收尺寸为253013qt_test发送的数据为3030224需要对数据进行切片classifer.py第385行
# _ = detector.predict(np.ones((setting.n_spec_rows, setting.n_spec_cols, setting.n_spec_bands), dtype=np.uint16))
# _ = classifier.predict(np.ones((setting.n_rgb_rows, setting.n_rgb_cols, setting.n_rgb_bands), dtype=np.uint8))
# _, _, _, _, _ =dp.analyze_tomato(cv2.imread(str(setting.tomato_img_dir)))
# _, _, _, _, _ = dp.analyze_passion_fruit(cv2.imread(str(setting.passion_fruit_img_dir))

View File

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB