diff --git a/20240529RGBtest3/classifer.py b/20240529RGBtest3/classifer.py index 10e5cab..4c83495 100644 --- a/20240529RGBtest3/classifer.py +++ b/20240529RGBtest3/classifer.py @@ -4,20 +4,18 @@ # @File : classifer.py # @Software: PyCharm - -import cv2 -import numpy as np import os +import cv2 +import json import utils -from sklearn.ensemble import RandomForestRegressor import joblib -import torch.nn as nn -import torch +import logging +import numpy as np from PIL import Image from torchvision import transforms -import numpy as np -import json -import logging +from sklearn.ensemble import RandomForestRegressor +import torch +import torch.nn as nn class Tomato: @@ -297,11 +295,11 @@ class Passion_fruit: ''' # 检查 RGB 图像是否为空或全黑 if rgb_img is None or rgb_img.size == 0 or np.all(rgb_img == 0): - logging.error("RGB 图像为空或全黑,返回一个全黑RGB图像。") + logging.warning("RGB 图像为空或全黑,返回一个全黑RGB图像。") return np.zeros((100, 100, 3), dtype=np.uint8) if rgb_img is None else np.zeros_like(rgb_img) # 检查二值图像是否为空或全黑 if bin_img is None or bin_img.size == 0 or np.all(bin_img == 0): - logging.error("二值图像为空或全黑,返回一个全黑RGB图像。") + logging.warning("二值图像为空或全黑,返回一个全黑RGB图像。") return np.zeros((100, 100, 3), dtype=np.uint8) if rgb_img is None else np.zeros_like(rgb_img) # 转换二值图像为三通道 try: @@ -359,7 +357,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): - logging.error("输入的图像为空或全黑,返回一个全黑图像。") + logging.warning("输入的图像为空或全黑,返回一个全黑图像。") 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) @@ -521,6 +519,10 @@ class Data_processing: return diameter, weigth, number_defects, total_pixels, rp + +#下面封装的是ResNet18和ResNet34的网络模型构建 +#原定用于构建RGB图像有果无果判断,后续发现存在纰漏,暂时搁置并未实际使用 + class BasicBlock(nn.Module): ''' BasicBlock for ResNet18 and ResNet34 @@ -691,7 +693,6 @@ def resnetzy(num_classes=1000, include_top=True): return ResNet(Bottleneck, [2, 2, 2, 2], num_classes=num_classes, include_top=include_top) - class ImageClassifier: ''' 图像分类器,用于加载预训练的 ResNet 模型并进行图像分类。 diff --git a/20240529RGBtest3/utils.py b/20240529RGBtest3/utils.py index b802ba1..59ff965 100644 --- a/20240529RGBtest3/utils.py +++ b/20240529RGBtest3/utils.py @@ -72,12 +72,12 @@ class Pipe: result, data = win32file.ReadFile(rgb_receive, data_size, None) # 检查读取操作是否成功 if result != 0: - print(f"读取失败,错误代码: {result}") + logging.error(f"读取失败,错误代码: {result}") return None # 返回成功读取的数据 return data except Exception as e: - print(f"数据接收失败,错误原因: {e}") + logging.error(f"数据接收失败,错误原因: {e}") return None def receive_spec_data(self, spec_receive): @@ -89,12 +89,12 @@ class Pipe: result, spec_data = win32file.ReadFile(spec_receive, data_size, None) # 检查读取操作是否成功 if result != 0: - print(f"读取失败,错误代码: {result}") + logging.error(f"读取失败,错误代码: {result}") return None # 返回成功读取的数据 return spec_data except Exception as e: - print(f"数据接收失败,错误原因: {e}") + logging.error(f"数据接收失败,错误原因: {e}") return None def parse_img(self, data: bytes) -> (str, any): @@ -232,41 +232,17 @@ class Pipe: try: win32file.WriteFile(self.rgb_send, send_message) # time.sleep(0.01) - print('发送成功') - # print(len(send_message), len(img_bytes)) - # print(len(send_message)) + # print('发送成功') except Exception as e: logging.error(f'发送指令失败,错误类型:{e}') return False - # end_time = time.time() # print(f'发送时间:{end_time - start_time}秒') - return True -def mkdir_if_not_exist(dir_name, is_delete=False): - """ - 创建文件夹 - :param dir_name: 文件夹 - :param is_delete: 是否删除 - :return: 是否成功 - """ - try: - if is_delete: - if os.path.exists(dir_name): - shutil.rmtree(dir_name) - print('[Info] 文件夹 "%s" 存在, 删除文件夹.' % dir_name) - - if not os.path.exists(dir_name): - os.makedirs(dir_name) - print('[Info] 文件夹 "%s" 不存在, 创建文件夹.' % dir_name) - return True - except Exception as e: - print('[Exception] %s' % e) - return False def create_file(file_name): """