# tile_defect_and_color_detect **Repository Path**: hehuang0724/tile_defect_and_color_detect ## Basic Information - **Project Name**: tile_defect_and_color_detect - **Description**: 瓷砖缺陷和色差检测软件 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-03-27 - **Last Updated**: 2025-02-06 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 使用 ## 添加模型 ### 1. JSON配置 在qt/DetectModel/model.json 中添加 ```json { "defect_model": [ { "name": "yolov8", "weight_path": "", "func_path": "", "type": "" } ], "abberration_model": [ { "name": "resnet50", "weight_path": "", "func_path": "", "type": "" } ] } ``` 注释:主要实现name字段,其他字段暂不需要 ### 2. 添加模型(源码方式) - 工作目录为 + qt/DetectModel/DefectModel + qt/DetectModel/AberrationModel - 以添加YOLOv8为例 1. 在qt/DetectModel/DefectModel下新建yolov8/source文件夹 2. 新建detect_logical.py文件,新建类Yolov8继承BaseModel,并实现__init__、model_init、button_image_open三个方法 ```python from qt.common.core.BaseModel import BaseModel class Yolov8(BaseModel): def __init__(self, ext_info): ''' :param ext_info 额外信息(model.json里面的值) ''' self.cap = cv2.VideoCapture() self.num_stop = 1 # 暂停与播放辅助信号,note:通过奇偶来控制暂停与播放 self.output_folder = 'output/' self.vid_writer = None # 权重初始文件名 self.openfile_name_model = ext_info['defect_model']['weight_path'] self.ext_info = ext_info self.image = None # 加载相关参数,并初始化模型 def model_init(self): pass # 打开图片并检测 def button_image_open(self, image, win, win_info, detect_info): ''' :param image 拍摄图像矩阵(cv.BGR) :param win qt展示图像界面句柄 :param win_info qt展示检测信息界面句柄 :param detect_info 额外展示信息前缀 ''' name_list = [] # image = cv2.imread(r"D:\hakvision\hkpy_4.2.1\hk-pyqt-tile\qt\DetectModel\DefectModel\yolov5\source\data\images\25_4914_1638_1024_1024_0_8192_6000.jpg") if image is not None: # img = cv2.imread(img_name) info_show, img = self.dvide_slice(image) # info_show = "测试" # img = image # info_show = self.detect(name_list, img) print(info_show) # 获取当前系统时间,作为img文件名 now = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime(time.time())) new_filename = now + '.jpg' file_path = self.output_folder + 'img_output/' + new_filename # cv2.imwrite(file_path, img) # 检测信息显示在界面 detect_info.message = detect_info.message + "\n缺陷检测信息:\n" + info_show win_info.append("{}".format(info_show)) # 检测结果显示在界面 result = cv2.cvtColor(img, cv2.COLOR_BGR2BGRA) w, h = result.shape[1], result.shape[0] t_w, t_h =win.width(), win.height() result = cv2.resize(result, (int(t_h / h * w), t_h), interpolation=cv2.INTER_AREA) pad_w = t_w - result.shape[1] # 计算需要填充的像素数目(图像的宽这一维度上) pad_h = t_h - result.shape[0] # 计算需要填充的像素数目(图像的高这一维度上) top, bottom = pad_h // 2, pad_h - (pad_h // 2) left, right = pad_w // 2, pad_w - (pad_w // 2) result = cv2.copyMakeBorder(result, top, bottom, left, right, cv2.BORDER_CONSTANT, None, (0, 0, 0)) QtImg = QtGui.QImage(result.data, result.shape[1], result.shape[0], QtGui.QImage.Format_RGB32) win.setPixmap(QtGui.QPixmap.fromImage(QtImg)) win.setScaledContents(True) # 设置图像自适应界面大小 # 保存图片 save_root = r"D:\hakvision\hkpy_4.2.1\hk-pyqt-tile\qt\Source\Save" detect_name = str(time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime())) try: cv2.imwrite(os.path.join(save_root, "detect_{}.jpg".format(detect_name)), result) except: print(os.path.join(save_root, "detect_{}.jpg".format(detect_name))) ``` 3. 在qt/DetectModel/DefectModel/yolov8新建__init__.py ```python from .source.detect_logical import Yolov8 __all__ = ('Yolov8') ``` ### 3.在ModelFactory类中注册 1. 导入具体检测类 2. 在qt/DetectModel/ModelFactory.py 中新建判断语句 ```python from .DefectModel.yolov8 import Yolov8 class ModelFactory(): def getModel(self, ext_info): message = '' if False: pass elif str(ext_info['defect_model']['name']).startswith('yolov8'): model = Yolov8(ext_info) model.model_init() return model ``` ### 4. fileName.json 该文件内保存了权重路径和检测后要保存的图片路径