mirror of
https://github.com/NanjingForestryUniversity/supermachine--tomato-passion_fruit.git
synced 2025-11-09 06:44:02 +00:00
fix:修改部分logging信息,添加部分功能块注释
This commit is contained in:
parent
ebf41ab0fe
commit
28f81a135a
@ -4,20 +4,18 @@
|
|||||||
# @File : classifer.py
|
# @File : classifer.py
|
||||||
# @Software: PyCharm
|
# @Software: PyCharm
|
||||||
|
|
||||||
|
|
||||||
import cv2
|
|
||||||
import numpy as np
|
|
||||||
import os
|
import os
|
||||||
|
import cv2
|
||||||
|
import json
|
||||||
import utils
|
import utils
|
||||||
from sklearn.ensemble import RandomForestRegressor
|
|
||||||
import joblib
|
import joblib
|
||||||
import torch.nn as nn
|
import logging
|
||||||
import torch
|
import numpy as np
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
from torchvision import transforms
|
from torchvision import transforms
|
||||||
import numpy as np
|
from sklearn.ensemble import RandomForestRegressor
|
||||||
import json
|
import torch
|
||||||
import logging
|
import torch.nn as nn
|
||||||
|
|
||||||
|
|
||||||
class Tomato:
|
class Tomato:
|
||||||
@ -297,11 +295,11 @@ class Passion_fruit:
|
|||||||
'''
|
'''
|
||||||
# 检查 RGB 图像是否为空或全黑
|
# 检查 RGB 图像是否为空或全黑
|
||||||
if rgb_img is None or rgb_img.size == 0 or np.all(rgb_img == 0):
|
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)
|
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):
|
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)
|
return np.zeros((100, 100, 3), dtype=np.uint8) if rgb_img is None else np.zeros_like(rgb_img)
|
||||||
# 转换二值图像为三通道
|
# 转换二值图像为三通道
|
||||||
try:
|
try:
|
||||||
@ -359,7 +357,7 @@ class Data_processing:
|
|||||||
def contour_process(self, image_array):
|
def contour_process(self, image_array):
|
||||||
# 检查图像是否为空或全黑
|
# 检查图像是否为空或全黑
|
||||||
if image_array is None or image_array.size == 0 or np.all(image_array == 0):
|
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)
|
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)
|
image_filtered = cv2.medianBlur(image_array, 5)
|
||||||
@ -521,6 +519,10 @@ class Data_processing:
|
|||||||
|
|
||||||
return diameter, weigth, number_defects, total_pixels, rp
|
return diameter, weigth, number_defects, total_pixels, rp
|
||||||
|
|
||||||
|
|
||||||
|
#下面封装的是ResNet18和ResNet34的网络模型构建
|
||||||
|
#原定用于构建RGB图像有果无果判断,后续发现存在纰漏,暂时搁置并未实际使用
|
||||||
|
|
||||||
class BasicBlock(nn.Module):
|
class BasicBlock(nn.Module):
|
||||||
'''
|
'''
|
||||||
BasicBlock for ResNet18 and ResNet34
|
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)
|
return ResNet(Bottleneck, [2, 2, 2, 2], num_classes=num_classes, include_top=include_top)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class ImageClassifier:
|
class ImageClassifier:
|
||||||
'''
|
'''
|
||||||
图像分类器,用于加载预训练的 ResNet 模型并进行图像分类。
|
图像分类器,用于加载预训练的 ResNet 模型并进行图像分类。
|
||||||
|
|||||||
@ -72,12 +72,12 @@ class Pipe:
|
|||||||
result, data = win32file.ReadFile(rgb_receive, data_size, None)
|
result, data = win32file.ReadFile(rgb_receive, data_size, None)
|
||||||
# 检查读取操作是否成功
|
# 检查读取操作是否成功
|
||||||
if result != 0:
|
if result != 0:
|
||||||
print(f"读取失败,错误代码: {result}")
|
logging.error(f"读取失败,错误代码: {result}")
|
||||||
return None
|
return None
|
||||||
# 返回成功读取的数据
|
# 返回成功读取的数据
|
||||||
return data
|
return data
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"数据接收失败,错误原因: {e}")
|
logging.error(f"数据接收失败,错误原因: {e}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def receive_spec_data(self, spec_receive):
|
def receive_spec_data(self, spec_receive):
|
||||||
@ -89,12 +89,12 @@ class Pipe:
|
|||||||
result, spec_data = win32file.ReadFile(spec_receive, data_size, None)
|
result, spec_data = win32file.ReadFile(spec_receive, data_size, None)
|
||||||
# 检查读取操作是否成功
|
# 检查读取操作是否成功
|
||||||
if result != 0:
|
if result != 0:
|
||||||
print(f"读取失败,错误代码: {result}")
|
logging.error(f"读取失败,错误代码: {result}")
|
||||||
return None
|
return None
|
||||||
# 返回成功读取的数据
|
# 返回成功读取的数据
|
||||||
return spec_data
|
return spec_data
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"数据接收失败,错误原因: {e}")
|
logging.error(f"数据接收失败,错误原因: {e}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def parse_img(self, data: bytes) -> (str, any):
|
def parse_img(self, data: bytes) -> (str, any):
|
||||||
@ -232,41 +232,17 @@ class Pipe:
|
|||||||
try:
|
try:
|
||||||
win32file.WriteFile(self.rgb_send, send_message)
|
win32file.WriteFile(self.rgb_send, send_message)
|
||||||
# time.sleep(0.01)
|
# time.sleep(0.01)
|
||||||
print('发送成功')
|
# print('发送成功')
|
||||||
# print(len(send_message), len(img_bytes))
|
|
||||||
# print(len(send_message))
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f'发送指令失败,错误类型:{e}')
|
logging.error(f'发送指令失败,错误类型:{e}')
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# end_time = time.time()
|
# end_time = time.time()
|
||||||
# print(f'发送时间:{end_time - start_time}秒')
|
# print(f'发送时间:{end_time - start_time}秒')
|
||||||
|
|
||||||
return True
|
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):
|
def create_file(file_name):
|
||||||
"""
|
"""
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user