From f4ce2fa7895486641e6e79e5fbe8d95757078f3b Mon Sep 17 00:00:00 2001 From: TG <905865530@qq.com> Date: Sun, 23 Jun 2024 23:26:50 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=A2=9E=E5=8A=A0=E7=99=BE=E9=A6=99?= =?UTF-8?q?=E6=9E=9C=E7=A9=BA=E6=9E=9C=E6=8B=96=E5=88=A4=E6=96=AD=E9=80=BB?= =?UTF-8?q?=E8=BE=91=EF=BC=88=E5=A6=82=E6=9E=9C=E7=9B=B4=E5=BE=84=E4=B8=BA?= =?UTF-8?q?0=EF=BC=8C=E5=88=99=E7=B3=96=E5=BA=A6=E5=80=BC=E4=B9=9F?= =?UTF-8?q?=E4=B8=BA0=EF=BC=8C=E5=8D=B3=E8=BF=94=E5=9B=9E=E5=85=A80?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=EF=BC=89=EF=BC=9B=E6=B7=BB=E5=8A=A0=E5=B0=BA?= =?UTF-8?q?=E5=AF=B8=E8=BD=AC=E6=8D=A2=EF=BC=8C=E7=8E=B0=E5=9C=A8=E6=89=80?= =?UTF-8?q?=E6=9C=89=E7=9A=84=E8=BF=94=E5=9B=9E=E6=95=B0=E6=8D=AE=E5=9D=87?= =?UTF-8?q?=E4=B8=BA=E6=98=BE=E7=A4=BA=E5=B0=BA=E5=AF=B8=E5=8D=95=E4=BD=8D?= =?UTF-8?q?=E6=95=B0=E5=80=BC=EF=BC=88=E7=B3=96=E5=BA=A6=E5=80=BC*1000?= =?UTF-8?q?=EF=BC=8C=E7=BB=BF=E8=89=B2=E5=8D=A0=E6=AF=94*100=EF=BC=8C?= =?UTF-8?q?=E7=BC=BA=E9=99=B7=E9=9D=A2=E7=A7=AF*100=E8=BF=9B=E8=A1=8C?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E4=BC=A0=E8=BE=93=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 20240529RGBtest3/classifer.py | 25 +++++++++++++++---------- 20240529RGBtest3/main.py | 12 +++++++----- 20240529RGBtest3/qt_test.py | 8 ++++---- 20240529RGBtest3/utils.py | 21 +++++++++++---------- 4 files changed, 37 insertions(+), 29 deletions(-) diff --git a/20240529RGBtest3/classifer.py b/20240529RGBtest3/classifer.py index 1a80dc5..f029739 100644 --- a/20240529RGBtest3/classifer.py +++ b/20240529RGBtest3/classifer.py @@ -417,6 +417,7 @@ class Data_processing: # 初始化统计数据 count_black_areas = 0 total_pixels_black_areas = 0 + s = 0.00021973702422145334 # 对于每个白色区域,查找内部的黑色小区域 for contour in contours_white: @@ -436,7 +437,7 @@ class Data_processing: total_pixels_black_areas += cv2.contourArea(c) number_defects = count_black_areas - total_pixels = total_pixels_black_areas + total_pixels = total_pixels_black_areas * s return number_defects, total_pixels def weight_estimates(self, long_axis, short_axis): @@ -451,11 +452,12 @@ class Data_processing: float: 估算的西红柿体积 """ density = 0.652228972 - a = ((long_axis / 535) * 6.3) / 2 - b = ((short_axis /535) * 6.3) / 2 + a = ((long_axis / 425) * 6.3) / 2 + b = ((short_axis / 425) * 6.3) / 2 volume = 4 / 3 * np.pi * a * b * b - weigth = round(volume * density) - return weigth + weight = round(volume * density) + #重量单位为g + return weight def analyze_tomato(self, img): """ 分析给定图像,提取和返回西红柿的长径、短径、缺陷数量和缺陷总面积,并返回处理后的图像。 @@ -490,8 +492,10 @@ class Data_processing: number_defects, total_pixels = self.analyze_defect(new_bin_img) # 将处理后的图像转换为 RGB 格式 rp = cv2.cvtColor(org_defect, cv2.COLOR_BGR2RGB) - diameter = (long_axis + short_axis) / 2 - if diameter < 200: + #直径单位为cm,所以需要除以10 + diameter = (long_axis + short_axis) /425 * 63 / 2 / 10 + # 如果直径小于3,判断为空果拖异常图,则将所有值重置为0 + if diameter < 3: diameter = 0 green_percentage = 0 number_defects = 0 @@ -515,14 +519,15 @@ class Data_processing: max_mask = pf.find_largest_component(combined_mask) contour_mask = self.contour_process(max_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) edge = pf.draw_contours_on_image(img, contour_mask) org_defect = pf.bitwise_and_rgb_with_binary(edge, max_mask) 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的网络模型构建 diff --git a/20240529RGBtest3/main.py b/20240529RGBtest3/main.py index ab44b60..59893b6 100644 --- a/20240529RGBtest3/main.py +++ b/20240529RGBtest3/main.py @@ -46,14 +46,14 @@ def process_data(cmd: str, images: list, spec: any, dp: Data_processing, pipe: P 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: diameter_axis_list.append(diameter) max_defect_num = max(max_defect_num, number_defects) max_total_defect_area = max(max_total_defect_area, total_pixels) if i == 1: rp_result = rp - weigth = weigth + weight = weight else: 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': brix = 0 - weigth = 0 - response = pipe.send_data(cmd=cmd, brix=brix, diameter=diameter, green_percentage=gp, weigth=weigth, + weight = 0 + 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) return response elif cmd == 'PF': green_percentage = 0 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) return response diff --git a/20240529RGBtest3/qt_test.py b/20240529RGBtest3/qt_test.py index ce1ac79..b4f96bb 100644 --- a/20240529RGBtest3/qt_test.py +++ b/20240529RGBtest3/qt_test.py @@ -150,17 +150,17 @@ class MainWindow(QMainWindow): # 解析数据 cmd_result = data[:2].decode('ascii').strip().upper() 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') weight = int.from_bytes(data[7:8], 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') width = int.from_bytes(data[16:18], byteorder='big') rp = data[18:] img = np.frombuffer(rp, dtype=np.uint8).reshape(heigth, width, -1) - print(f"指令:{cmd_result}, 糖度值:{brix}, 绿色占比:{green_percentage}, 直径:{diameter}, " - f"预估重量:{weight}, 缺陷个数:{defect_num}, 缺陷面积:{total_defect_area}, 结果图的尺寸:{img.shape}") + print(f"指令:{cmd_result}, 糖度值:{brix}, 绿色占比:{green_percentage}, 直径:{diameter}cm, " + f"预估重量:{weight}g, 缺陷个数:{defect_num}, 缺陷面积:{total_defect_area}cm^2, 结果图的尺寸:{img.shape}") # 显示结果图像 diff --git a/20240529RGBtest3/utils.py b/20240529RGBtest3/utils.py index 59ff965..d928dcc 100644 --- a/20240529RGBtest3/utils.py +++ b/20240529RGBtest3/utils.py @@ -159,13 +159,13 @@ class Pipe: spec = np.frombuffer(spec, dtype=np.uint16).reshape((n_rows, n_bands, -1)).transpose(0, 2, 1) 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 brix: :param green_percentage: - :param weigth: + :param weight: :param diameter: :param defect_num: :param total_defect_area: @@ -195,6 +195,7 @@ class Pipe: img_bytes = img.tobytes() diameter = diameter.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') length = len(img_bytes) + 18 length = length.to_bytes(4, byteorder='big') @@ -202,22 +203,22 @@ class Pipe: brix = 0 brix = brix.to_bytes(2, byteorder='big') gp = green_percentage.to_bytes(1, byteorder='big') - weigth = 0 - weigth = weigth.to_bytes(1, byteorder='big') - send_message = length + cmd_re + brix + gp + diameter + weigth + defect_num + total_defect_area + height + width + img_bytes + weight = 0 + weight = weight.to_bytes(1, byteorder='big') + send_message = length + cmd_re + brix + gp + diameter + weight + defect_num + total_defect_area + height + width + img_bytes elif cmd == 'PF': brix = int(brix * 1000).to_bytes(2, byteorder='big') gp = 0 gp = gp.to_bytes(1, byteorder='big') - weigth = weigth.to_bytes(1, byteorder='big') - send_message = length + cmd_re + brix + gp + diameter + weigth + defect_num + total_defect_area + height + width + img_bytes + weight = weight.to_bytes(1, byteorder='big') + send_message = length + cmd_re + brix + gp + diameter + weight + defect_num + total_defect_area + height + width + img_bytes elif cmd == 'KO': brix = 0 brix = brix.to_bytes(2, byteorder='big') gp = 0 gp = gp.to_bytes(1, byteorder='big') - weigth = 0 - weigth = weigth.to_bytes(1, byteorder='big') + weight = 0 + weight = weight.to_bytes(1, byteorder='big') defect_num = 0 defect_num = defect_num.to_bytes(2, byteorder='big') total_defect_area = 0 @@ -228,7 +229,7 @@ class Pipe: width = width.to_bytes(2, byteorder='big') img_bytes = np.zeros((100, 100, 3), dtype=np.uint8).tobytes() 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: win32file.WriteFile(self.rgb_send, send_message) # time.sleep(0.01)