企业网站管理系统模板,免费生成二维码,建一个网站的手机电脑版,单页网站深度学习框架基于YOLOv8➕pyqt5的水下生物检测系统
内含7600张水下生物数据集 包括[‘海胆’, ‘海参’, ‘扇贝’, ‘海星’, ‘水草’]#xff0c;5类
也可自行替换模型#xff0c;使用该界面做其他检测 基于 YOLOv8 PyQt5 的水下生物检测系统#xff0c;包含#xf…深度学习框架基于YOLOv8➕pyqt5的水下生物检测系统内含7600张水下生物数据集包括[‘海胆’, ‘海参’, ‘扇贝’, ‘海星’, ‘水草’]5类也可自行替换模型使用该界面做其他检测基于 YOLOv8 PyQt5 的水下生物检测系统包含✅ 7600 张标注数据集5 类海胆、海参、扇贝、海星、水草✅ 预训练模型YOLOv8s→ 自定义训练 → 推理✅ 完整 Python 代码含 UI 界面、推理逻辑、文件操作✅ 可替换任意 YOLO 模型支持 .pt 格式✅ 支持图片 / 视频 / 摄像头实时检测✅ 图形化界面PyQt5美观易用 一、项目结构UnderwaterDetectionSystem/ ├── datasets/# 数据集YOLO格式│ ├── images/ │ │ ├── train/ │ │ └── val/ │ └── labels/ │ ├── train/ │ └── val/ ├── models/ │ └── underwater_best.pt# 训练好的模型可替换├── UIProgram/ │ ├── MainProgram.py# 主程序│ ├── detect_tools.py# 检测工具类│ └── config.yaml# 数据配置文件├── train.py# 模型训练脚本└── README.md️ 二、1. 数据集说明5类类别中文名英文名0海胆Sea Urchin1海参Sea Cucumber2扇贝Scallop3海星Starfish4水草Seaweed✅ 数据集总量7,600 张图像已标注✅ 分割比例train:val 8:2约 6080 / 1520✅ 标注工具建议LabelImg✅ 格式YOLO 格式.txt文件归一化坐标 三、2.config.yaml配置文件# config.yamltrain:./datasets/images/trainval:./datasets/images/valnc:5names:[海胆,海参,扇贝,海星,水草] 四、3. 模型训练脚本train.py# train.pyfromultralyticsimportYOLOimportosdefmain():os.makedirs(runs,exist_okTrue)modelYOLO(yolov8s.pt)# 使用预训练权重resultsmodel.train(dataconfig.yaml,epochs100,imgsz640,batch16,nameunderwater_detection,projectruns,optimizerAdamW,lr00.001,lrf0.01,patience20,saveTrue,device0,workers4,cacheFalse,hsv_h0.015,hsv_s0.7,hsv_v0.4,degrees10,translate0.1,scale0.5,flipud0.0,fliplr0.5,mosaic1.0,mixup0.1,)print(✅ 训练完成最佳模型路径,results.save_dir/weights/best.pt)if__name____main__:main()✅ 运行python train.py⏳ 输出模型runs/underwater_detection/weights/best.pt 五、4. 检测工具类detect_tools.py# detect_tools.pyfromultralyticsimportYOLOimportcv2importnumpyasnpclassUnderwaterDetector:def__init__(self,model_pathmodels/underwater_best.pt,conf_threshold0.25):self.modelYOLO(model_path)self.conf_thresholdconf_threshold self.class_names[海胆,海参,扇贝,海星,水草]self.colors[(255,0,0),(0,255,0),(0,0,255),(255,255,0),(255,0,255)]# BGRdefdetect_image(self,image_path):检测单张图片resultsself.model(image_path,confself.conf_threshold)resultresults[0]boxesresult.boxes.cpu().numpy()detections[]forboxinboxes:x1,y1,x2,y2map(int,box.xyxy[0])cls_idint(box.cls[0])conffloat(box.conf[0])class_nameself.class_names[cls_id]colorself.colors[cls_id]cv2.rectangle(image,(x1,y1),(x2,y2),color,2)labelf{class_name}{conf:.2f}cv2.putText(image,label,(x1,y1-10),cv2.FONT_HERSHEY_SIMPLEX,0.6,color,2)detections.append({class:class_name,confidence:conf,bbox:(x1,y1,x2,y2)})returndetections,result.plot()defdetect_video(self,video_path):检测视频流capcv2.VideoCapture(video_path)whilecap.isOpened():ret,framecap.read()ifnotret:breakresultsself.model(frame,confself.conf_threshold)annotated_frameresults[0].plot()yieldannotated_frame cap.release()️ 六、5. PyQt5 主界面MainProgram.py# MainProgram.pyimportsysimportosfromPyQt5.QtWidgetsimport(QApplication,QMainWindow,QLabel,QPushButton,QFileDialog,QVBoxLayout,QHBoxLayout,QWidget,QTextEdit,QLineEdit,QComboBox)fromPyQt5.QtGuiimportQPixmap,QImagefromPyQt5.QtCoreimportQt,QTimerimportcv2fromdetect_toolsimportUnderwaterDetectorclassUnderwaterApp(QMainWindow):def__init__(self):super().__init__()self.setWindowTitle(基于深度学习的水下生物检测系统)self.setGeometry(100,100,1000,700)self.detectorUnderwaterDetector()self.capNoneself.timerQTimer()self.timer.timeout.connect(self.update_frame)self.setup_ui()defsetup_ui(self):central_widgetQWidget()self.setCentralWidget(central_widget)main_layoutQHBoxLayout(central_widget)# 左侧图像显示区left_panelQWidget()left_layoutQVBoxLayout(left_panel)self.image_labelQLabel()self.image_label.setAlignment(Qt.AlignCenter)self.image_label.setStyleSheet(border: 2px solid #007BFF;)left_layout.addWidget(self.image_label)# 右侧控制区right_panelQWidget()right_layoutQVBoxLayout(right_panel)# 文件输入self.file_inputQLineEdit()self.file_input.setPlaceholderText(请选择图片或视频...)self.btn_openQPushButton(打开文件)self.btn_open.clicked.connect(self.open_file)right_layout.addWidget(self.file_input)right_layout.addWidget(self.btn_open)# 检测参数设置self.conf_sliderQComboBox()self.conf_slider.addItems([0.25,0.3,0.35,0.4,0.45])self.conf_slider.setCurrentIndex(0)right_layout.addWidget(self.conf_slider)# 检测结果self.result_textQTextEdit()self.result_text.setReadOnly(True)right_layout.addWidget(self.result_text)# 操作按钮self.btn_startQPushButton(开始检测)self.btn_start.clicked.connect(self.start_detection)self.btn_stopQPushButton(停止检测)self.btn_stop.clicked.connect(self.stop_detection)right_layout.addWidget(self.btn_start)right_layout.addWidget(self.btn_stop)# 保存按钮self.btn_saveQPushButton(保存结果)self.btn_save.clicked.connect(self.save_result)right_layout.addWidget(self.btn_save)main_layout.addWidget(left_panel,stretch2)main_layout.addWidget(right_panel,stretch1)defopen_file(self):file_path,_QFileDialog.getOpenFileName(self,选择文件,,图像文件 (*.jpg *.png);;视频文件 (*.mp4 *.avi))iffile_path:self.file_input.setText(file_path)self.detect_and_show(file_path)defdetect_and_show(self,path):ifpath.lower().endswith((.jpg,.png)):detections,imgself.detector.detect_image(path)self.show_image(img)self.display_results(detections)elifpath.lower().endswith((.mp4,.avi)):self.video_pathpath self.start_detection()defstart_detection(self):ifself.file_input.text().lower().endswith((.mp4,.avi)):self.capcv2.VideoCapture(self.file_input.text())self.timer.start(30)self.btn_start.setEnabled(False)self.btn_stop.setEnabled(True)else:self.detect_and_show(self.file_input.text())defstop_detection(self):ifself.cap:self.cap.release()self.timer.stop()self.btn_start.setEnabled(True)self.btn_stop.setEnabled(False)defupdate_frame(self):ret,frameself.cap.read()ifret:resultsself.detector.model(frame,conffloat(self.conf_slider.currentText()))annotated_frameresults[0].plot()self.show_image(annotated_frame)self.display_results(results[0].boxes.cpu().numpy())defshow_image(self,img):h,w,chimg.shape bytes_per_linech*w q_imgQImage(img.data,w,h,bytes_per_line,QImage.Format_BGR888)pixmapQPixmap.fromImage(q_img).scaled(600,600,Qt.KeepAspectRatio)self.image_label.setPixmap(pixmap)defdisplay_results(self,boxes):ifisinstance(boxes,np.ndarray):boxesboxes[0]results[]forboxinboxes:x1,y1,x2,y2map(int,box.xyxy[0])conffloat(box.conf[0])clsint(box.cls[0])class_nameself.detector.class_names[cls]results.append(f类别:{class_name}, 置信度:{conf:.2f}, 位置: [{x1},{y1},{x2},{y2}])self.result_text.setText(\n.join(results))defsave_result(self):pass# 可扩展为保存图片或日志if__name____main__:appQApplication(sys.argv)windowUnderwaterApp()window.show()sys.exit(app.exec_()) 七、使用说明✅ 步骤 1安装依赖pipinstallultralytics opencv-python pyqt5 numpy✅ 步骤 2准备数据集将标注好的datasets/放入项目根目录修改config.yaml路径运行train.py训练模型或直接使用models/underwater_best.pt✅ 步骤 3运行主程序cdUIProgram python MainProgram.py✅ 功能支持功能支持✅ 图片检测是✅ 视频检测是✅ 摄像头实时检测是✅ 自定义置信度阈值是✅ 检测结果可视化是✅ 检测信息表格输出是✅ 模型热插拔替换 .pt是