feat:增加百香果空果拖判断逻辑(如果直径为0,则糖度值也为0,即返回全0数据);添加尺寸转换,现在所有的返回数据均为显示尺寸单位数值(糖度值*1000,绿色占比*100,缺陷面积*100进行数据传输)

This commit is contained in:
TG 2024-06-23 23:26:50 +08:00
parent b0cf31fd60
commit f4ce2fa789
4 changed files with 37 additions and 29 deletions

View File

@ -417,6 +417,7 @@ class Data_processing:
# 初始化统计数据 # 初始化统计数据
count_black_areas = 0 count_black_areas = 0
total_pixels_black_areas = 0 total_pixels_black_areas = 0
s = 0.00021973702422145334
# 对于每个白色区域,查找内部的黑色小区域 # 对于每个白色区域,查找内部的黑色小区域
for contour in contours_white: for contour in contours_white:
@ -436,7 +437,7 @@ class Data_processing:
total_pixels_black_areas += cv2.contourArea(c) total_pixels_black_areas += cv2.contourArea(c)
number_defects = count_black_areas number_defects = count_black_areas
total_pixels = total_pixels_black_areas total_pixels = total_pixels_black_areas * s
return number_defects, total_pixels return number_defects, total_pixels
def weight_estimates(self, long_axis, short_axis): def weight_estimates(self, long_axis, short_axis):
@ -451,11 +452,12 @@ class Data_processing:
float: 估算的西红柿体积 float: 估算的西红柿体积
""" """
density = 0.652228972 density = 0.652228972
a = ((long_axis / 535) * 6.3) / 2 a = ((long_axis / 425) * 6.3) / 2
b = ((short_axis /535) * 6.3) / 2 b = ((short_axis / 425) * 6.3) / 2
volume = 4 / 3 * np.pi * a * b * b volume = 4 / 3 * np.pi * a * b * b
weigth = round(volume * density) weight = round(volume * density)
return weigth #重量单位为g
return weight
def analyze_tomato(self, img): def analyze_tomato(self, img):
""" """
分析给定图像提取和返回西红柿的长径短径缺陷数量和缺陷总面积并返回处理后的图像 分析给定图像提取和返回西红柿的长径短径缺陷数量和缺陷总面积并返回处理后的图像
@ -490,8 +492,10 @@ class Data_processing:
number_defects, total_pixels = self.analyze_defect(new_bin_img) number_defects, total_pixels = self.analyze_defect(new_bin_img)
# 将处理后的图像转换为 RGB 格式 # 将处理后的图像转换为 RGB 格式
rp = cv2.cvtColor(org_defect, cv2.COLOR_BGR2RGB) rp = cv2.cvtColor(org_defect, cv2.COLOR_BGR2RGB)
diameter = (long_axis + short_axis) / 2 #直径单位为cm所以需要除以10
if diameter < 200: diameter = (long_axis + short_axis) /425 * 63 / 2 / 10
# 如果直径小于3判断为空果拖异常图则将所有值重置为0
if diameter < 3:
diameter = 0 diameter = 0
green_percentage = 0 green_percentage = 0
number_defects = 0 number_defects = 0
@ -515,14 +519,15 @@ class Data_processing:
max_mask = pf.find_largest_component(combined_mask) max_mask = pf.find_largest_component(combined_mask)
contour_mask = self.contour_process(max_mask) contour_mask = self.contour_process(max_mask)
long_axis, short_axis = self.analyze_ellipse(contour_mask) long_axis, short_axis = self.analyze_ellipse(contour_mask)
weigth = self.weight_estimates(long_axis, short_axis) weight = self.weight_estimates(long_axis, short_axis)
number_defects, total_pixels = self.analyze_defect(max_mask) number_defects, total_pixels = self.analyze_defect(max_mask)
edge = pf.draw_contours_on_image(img, contour_mask) edge = pf.draw_contours_on_image(img, contour_mask)
org_defect = pf.bitwise_and_rgb_with_binary(edge, max_mask) org_defect = pf.bitwise_and_rgb_with_binary(edge, max_mask)
rp = cv2.cvtColor(org_defect, cv2.COLOR_BGR2RGB) rp = cv2.cvtColor(org_defect, cv2.COLOR_BGR2RGB)
diameter = (long_axis + short_axis) / 2 #直径单位为cm所以需要除以10
diameter = (long_axis + short_axis) /425 * 63 / 2 / 10
return diameter, weigth, number_defects, total_pixels, rp return diameter, weight, number_defects, total_pixels, rp
# #下面封装的是ResNet18和ResNet34的网络模型构建 # #下面封装的是ResNet18和ResNet34的网络模型构建

View File

@ -46,14 +46,14 @@ def process_data(cmd: str, images: list, spec: any, dp: Data_processing, pipe: P
elif cmd == 'PF': elif cmd == 'PF':
# 百香果 # 百香果
diameter, weigth, number_defects, total_pixels, rp = dp.analyze_passion_fruit(img) diameter, weight, number_defects, total_pixels, rp = dp.analyze_passion_fruit(img)
if i <= 2: if i <= 2:
diameter_axis_list.append(diameter) diameter_axis_list.append(diameter)
max_defect_num = max(max_defect_num, number_defects) max_defect_num = max(max_defect_num, number_defects)
max_total_defect_area = max(max_total_defect_area, total_pixels) max_total_defect_area = max(max_total_defect_area, total_pixels)
if i == 1: if i == 1:
rp_result = rp rp_result = rp
weigth = weigth weight = weight
else: else:
logging.error(f'错误指令,指令为{cmd}') logging.error(f'错误指令,指令为{cmd}')
@ -63,14 +63,16 @@ def process_data(cmd: str, images: list, spec: any, dp: Data_processing, pipe: P
if cmd == 'TO': if cmd == 'TO':
brix = 0 brix = 0
weigth = 0 weight = 0
response = pipe.send_data(cmd=cmd, brix=brix, diameter=diameter, green_percentage=gp, weigth=weigth, response = pipe.send_data(cmd=cmd, brix=brix, diameter=diameter, green_percentage=gp, weight=weight,
defect_num=max_defect_num, total_defect_area=max_total_defect_area, rp=rp_result) defect_num=max_defect_num, total_defect_area=max_total_defect_area, rp=rp_result)
return response return response
elif cmd == 'PF': elif cmd == 'PF':
green_percentage = 0 green_percentage = 0
brix = detector.predict(spec) brix = detector.predict(spec)
response = pipe.send_data(cmd=cmd, brix=brix, green_percentage=green_percentage, diameter=diameter, weigth=weigth, if diameter == 0:
brix = 0
response = pipe.send_data(cmd=cmd, brix=brix, green_percentage=green_percentage, diameter=diameter, weight=weight,
defect_num=max_defect_num, total_defect_area=max_total_defect_area, rp=rp_result) defect_num=max_defect_num, total_defect_area=max_total_defect_area, rp=rp_result)
return response return response

View File

@ -150,17 +150,17 @@ class MainWindow(QMainWindow):
# 解析数据 # 解析数据
cmd_result = data[:2].decode('ascii').strip().upper() cmd_result = data[:2].decode('ascii').strip().upper()
brix = (int.from_bytes(data[2:4], byteorder='big')) / 1000 brix = (int.from_bytes(data[2:4], byteorder='big')) / 1000
green_percentage = int.from_bytes(data[4:5], byteorder='big') green_percentage = (int.from_bytes(data[4:5], byteorder='big')) / 100
diameter = int.from_bytes(data[5:7], byteorder='big') diameter = int.from_bytes(data[5:7], byteorder='big')
weight = int.from_bytes(data[7:8], byteorder='big') weight = int.from_bytes(data[7:8], byteorder='big')
defect_num = int.from_bytes(data[8:10], byteorder='big') defect_num = int.from_bytes(data[8:10], byteorder='big')
total_defect_area = int.from_bytes(data[10:14], byteorder='big') total_defect_area = (int.from_bytes(data[10:14], byteorder='big')) / 100
heigth = int.from_bytes(data[14:16], byteorder='big') heigth = int.from_bytes(data[14:16], byteorder='big')
width = int.from_bytes(data[16:18], byteorder='big') width = int.from_bytes(data[16:18], byteorder='big')
rp = data[18:] rp = data[18:]
img = np.frombuffer(rp, dtype=np.uint8).reshape(heigth, width, -1) img = np.frombuffer(rp, dtype=np.uint8).reshape(heigth, width, -1)
print(f"指令:{cmd_result}, 糖度值:{brix}, 绿色占比:{green_percentage}, 直径:{diameter}, " print(f"指令:{cmd_result}, 糖度值:{brix}, 绿色占比:{green_percentage}, 直径:{diameter}cm, "
f"预估重量:{weight}, 缺陷个数:{defect_num}, 缺陷面积:{total_defect_area}, 结果图的尺寸:{img.shape}") f"预估重量:{weight}g, 缺陷个数:{defect_num}, 缺陷面积:{total_defect_area}cm^2, 结果图的尺寸:{img.shape}")
# 显示结果图像 # 显示结果图像

View File

@ -159,13 +159,13 @@ class Pipe:
spec = np.frombuffer(spec, dtype=np.uint16).reshape((n_rows, n_bands, -1)).transpose(0, 2, 1) spec = np.frombuffer(spec, dtype=np.uint16).reshape((n_rows, n_bands, -1)).transpose(0, 2, 1)
return cmd, spec return cmd, spec
def send_data(self,cmd:str, brix, green_percentage, weigth, diameter, defect_num, total_defect_area, rp): def send_data(self,cmd:str, brix, green_percentage, weight, diameter, defect_num, total_defect_area, rp):
''' '''
发送数据 发送数据
:param cmd: :param cmd:
:param brix: :param brix:
:param green_percentage: :param green_percentage:
:param weigth: :param weight:
:param diameter: :param diameter:
:param defect_num: :param defect_num:
:param total_defect_area: :param total_defect_area:
@ -195,6 +195,7 @@ class Pipe:
img_bytes = img.tobytes() img_bytes = img.tobytes()
diameter = diameter.to_bytes(2, byteorder='big') diameter = diameter.to_bytes(2, byteorder='big')
defect_num = defect_num.to_bytes(2, byteorder='big') defect_num = defect_num.to_bytes(2, byteorder='big')
total_defect_area = total_defect_area * 1000
total_defect_area = int(total_defect_area).to_bytes(4, byteorder='big') total_defect_area = int(total_defect_area).to_bytes(4, byteorder='big')
length = len(img_bytes) + 18 length = len(img_bytes) + 18
length = length.to_bytes(4, byteorder='big') length = length.to_bytes(4, byteorder='big')
@ -202,22 +203,22 @@ class Pipe:
brix = 0 brix = 0
brix = brix.to_bytes(2, byteorder='big') brix = brix.to_bytes(2, byteorder='big')
gp = green_percentage.to_bytes(1, byteorder='big') gp = green_percentage.to_bytes(1, byteorder='big')
weigth = 0 weight = 0
weigth = weigth.to_bytes(1, byteorder='big') weight = weight.to_bytes(1, byteorder='big')
send_message = length + cmd_re + brix + gp + diameter + weigth + defect_num + total_defect_area + height + width + img_bytes send_message = length + cmd_re + brix + gp + diameter + weight + defect_num + total_defect_area + height + width + img_bytes
elif cmd == 'PF': elif cmd == 'PF':
brix = int(brix * 1000).to_bytes(2, byteorder='big') brix = int(brix * 1000).to_bytes(2, byteorder='big')
gp = 0 gp = 0
gp = gp.to_bytes(1, byteorder='big') gp = gp.to_bytes(1, byteorder='big')
weigth = weigth.to_bytes(1, byteorder='big') weight = weight.to_bytes(1, byteorder='big')
send_message = length + cmd_re + brix + gp + diameter + weigth + defect_num + total_defect_area + height + width + img_bytes send_message = length + cmd_re + brix + gp + diameter + weight + defect_num + total_defect_area + height + width + img_bytes
elif cmd == 'KO': elif cmd == 'KO':
brix = 0 brix = 0
brix = brix.to_bytes(2, byteorder='big') brix = brix.to_bytes(2, byteorder='big')
gp = 0 gp = 0
gp = gp.to_bytes(1, byteorder='big') gp = gp.to_bytes(1, byteorder='big')
weigth = 0 weight = 0
weigth = weigth.to_bytes(1, byteorder='big') weight = weight.to_bytes(1, byteorder='big')
defect_num = 0 defect_num = 0
defect_num = defect_num.to_bytes(2, byteorder='big') defect_num = defect_num.to_bytes(2, byteorder='big')
total_defect_area = 0 total_defect_area = 0
@ -228,7 +229,7 @@ class Pipe:
width = width.to_bytes(2, byteorder='big') width = width.to_bytes(2, byteorder='big')
img_bytes = np.zeros((100, 100, 3), dtype=np.uint8).tobytes() img_bytes = np.zeros((100, 100, 3), dtype=np.uint8).tobytes()
length = (18).to_bytes(4, byteorder='big') length = (18).to_bytes(4, byteorder='big')
send_message = length + cmd_re + brix + gp + diameter + weigth + defect_num + total_defect_area + height + width + img_bytes send_message = length + cmd_re + brix + gp + diameter + weight + defect_num + total_defect_area + height + width + img_bytes
try: try:
win32file.WriteFile(self.rgb_send, send_message) win32file.WriteFile(self.rgb_send, send_message)
# time.sleep(0.01) # time.sleep(0.01)