yolov8训练及测试(ubuntu18.04、tensorrt、ros)

1 数据集制作

1.1标注数据

Linux/Ubuntu/Mac
至少需要 Python 2.6 (推荐使用 Python 3 或更高版本 及 PyQt5)
Ubuntu Linux (Python 3 + Qt5)

git clone https://gitcode.com/gh_mirrors/la/labelImg.git
sudo apt-get install pyqt5-dev-tools
cd labelImg
sudo pip3 install -r requirements/requirements-linux-python3.txt
make qt5py3
python3 labelImg.py

运行python3 labelImg.py出错, File "/home/wyh/environment_setting/labelImg-master/libs/labelDialog.py", line 37, in __init__ layout.addWidget(bb, alignment=Qt.AlignmentFlag.AlignLeft) AttributeError: type object 'AlignmentFlag' has no attribute 'AlignLeft'
原因:因为 PyQtPySide 的版本问题
解决:如果确定用的时PYQT5,将layout.addWidget(bb, alignment=Qt.AlignmentFlag.AlignLeft)更改为layout.addWidget(bb, alignment=Qt.AlignLeft)

1.2 建立对应的数据文件夹

在这里插入图片描述
images:图片数据,labels:标注转换后的yolotxt文件,xmls:labelimg标注的xml格式数据,class.txt:标签txt文件

1.3 将标注后的xml转为txt

#! /usr/local/bin/ python
# -*- coding: utf-8 -*-
# .xml文件转换成.txt文件import copy
from xml.etree import Element, SubElement, tostring, ElementTree
import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join# 检测目标的类别
classes = ["ore carrier", "passenger ship","container ship", "bulk cargo carrier","general cargo ship", "fishing boat"]CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))def convert(size, box):dw = 1. / size[0]dh = 1. / size[1]x = (box[0] + box[1]) / 2.0    # (x_min + x_max) / 2.0y = (box[2] + box[3]) / 2.0    # (y_min + y_max) / 2.0w = box[1] - box[0]   # x_max - x_minh = box[3] - box[2]   # y_max - y_minx = x * dww = w * dwy = y * dhh = h * dhreturn (x, y, w, h)def convert_annotation(image_id):# .xml格式文件的地址in_file = open('地址1\%s.xml' % (image_id), encoding='UTF-8')# 生成的.txt格式文件的地址out_file = open('地址2\%s.txt' % (image_id), 'w')tree = ET.parse(in_file)root = tree.getroot()size = root.find('size')w = int(size.find('width').text)h = int(size.find('height').text)for obj in root.iter('object'):cls = obj.find('name').textif cls not in classes:continuecls_id = classes.index(cls)xmlbox = obj.find('bndbox')b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),float(xmlbox.find('ymax').text))bb = convert((w, h), b)out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')# .xml格式文件的地址
xml_path = os.path.join(CURRENT_DIR, '地址1/')# xml列表
img_xmls = os.listdir(xml_path)
for img_xml in img_xmls:label_name = img_xml.split('.')[0]print(label_name)convert_annotation(label_name)

将代码中路径更改为对应的路径

2 将yolo数据拆分为train、val、test

import os
import random
import shutildef split_dataset(images_dir, labels_dir, output_dir, split_ratio=(0.8, 0.1, 0.1)):"""将图像和标签数据集划分为训练集、验证集和测试集。:param images_dir: 图像文件夹路径:param labels_dir: 标签文件夹路径:param output_dir: 输出目录路径:param split_ratio: 划分比例 (train, val, test)"""# 确保输出目录存在os.makedirs(output_dir, exist_ok=True)for subdir in ['train', 'val', 'test']:os.makedirs(os.path.join(output_dir, subdir, 'images'), exist_ok=True)os.makedirs(os.path.join(output_dir, subdir, 'labels'), exist_ok=True)# 获取所有图像文件名images = [f for f in os.listdir(images_dir) if f.endswith('.jpg') or f.endswith('.png')]labels = [f.replace('.jpg', '.txt').replace('.png', '.txt') for f in images]# 打乱顺序combined = list(zip(images, labels))random.shuffle(combined)images[:], labels[:] = zip(*combined)# 计算划分点num_train = int(len(images) * split_ratio[0])num_val = int(len(images) * split_ratio[1])# 划分数据集for i, image in enumerate(images):label = labels[i]if i < num_train:subset = 'train'elif i < num_train + num_val:subset = 'val'else:subset = 'test'shutil.copy(os.path.join(images_dir, image), os.path.join(output_dir, subset, 'images', image))shutil.copy(os.path.join(labels_dir, label), os.path.join(output_dir, subset, 'labels', label))# 示例调用
split_dataset('/home/wyh/artrc_catkin/src/artrc_yolov8/datasets/origin_data/images','/home/wyh/artrc_catkin/src/artrc_yolov8/datasets/origin_data/labels','/home/wyh/artrc_catkin/src/artrc_yolov8/datasets/split_data')

运行后如图所示
在这里插入图片描述

3 根据数据集添加yaml文件

import yaml
import os
def create_yaml(output_dir, train_dir, val_dir, test_dir, class_names, num_classes):"""创建 YOLOv8 数据集配置文件。:param output_dir: 输出目录路径:param train_dir: 训练集目录路径:param val_dir: 验证集目录路径:param test_dir: 测试集目录路径:param class_names: 类别名称列表:param num_classes: 类别数量"""data = {'train': train_dir,'val': val_dir,'test': test_dir,'nc': num_classes,'names': class_names}with open(os.path.join(output_dir, 'dataset.yaml'), 'w') as f:yaml.dump(data, f, default_flow_style=False)# 示例调用
create_yaml('/home/wyh/artrc_catkin/src/artrc_yolov8/datasets/split_data','/home/wyh/artrc_catkin/src/artrc_yolov8/datasets/split_data/train/images','/home/wyh/artrc_catkin/src/artrc_yolov8/datasets/split_data/val/images','/home/wyh/artrc_catkin/src/artrc_yolov8/datasets/split_data/test/images',['corrosion','craze', 'hide_craze','surface_attach','surface_corrosion','surface_eye','surface_injure','surface_oil','thunderstrike'], 9)

运行结果如下文件:
在这里插入图片描述

4 训练数据集

cd ultralytics
yolo task=detect mode=train model=yolov8n.pt data=ultralytics/cfg/datasets/dataset.yaml batch=8 epochs=200 imgsz=640 workers=32 device=0

5 训练后使用

5.1 训练后的各中形式数据转换

5.1.1 将.pt转换为onnx

方式一:利用下述pt_to_onnx.py进行转换

#! /usr/local/bin/ python
# -*- coding: utf-8 -*-
from ultralytics import YOLOmodel = YOLO("best.pt")success = model.export(format="onnx", half=False, dynamic=True, opset=17)print("demo")
cd ultralytics
python pt_to_onnx.py

方式二:命令行操作转换

# 到相应的权重文件所在文件夹
cd ultralytics 
setconda
conda activate yolov8
yolo mode=export model=yolov8n.pt format=onnx dynamic=True    #simplify=True
yolo mode=export model=yolov8s.pt format=onnx dynamic=True    # 不同模型

5.1.2将.onnx转换为.trt

cd /environment_setting/tensorrt-alpha/data/yolov8
# 生成trt文件
# 640  ../../../TensorRT-8.4.1.5/bin/trtexec为各路径,根据实际情况填写
../../../TensorRT-8.4.1.5/bin/trtexec   --onnx=best.onnx  --saveEngine=best.trt  --buildOnly --minShapes=images:1x3x640x640 --optShapes=images:4x3x640x640 --maxShapes=images:8x3x640x640
../../../TensorRT-8.4.1.5/bin/trtexec   --onnx=yolov8s.onnx  --saveEngine=yolov8s.trt  --buildOnly --minShapes=images:1x3x640x640 --optShapes=images:4x3x640x640 --maxShapes=images:8x3x640x640
../../../TensorRT-8.4.1.5/bin/trtexec   --onnx=yolov8m.onnx  --saveEngine=yolov8m.trt  --buildOnly --minShapes=images:1x3x640x640 --optShapes=images:4x3x640x640 --maxShapes=images:8x3x640x640

5.2 利用pt文件进行检测

#!/home/wyh/.conda/envs/yolov8/bin/python3.8
# -*- coding: utf-8 -*-
import cv2
import torch
import rospy
import numpy as np
from ultralytics import YOLO
from time import time
from std_msgs.msg import Header
from sensor_msgs.msg import Image
from artrc_yolov8.msg import BoundingBox, BoundingBoxesclass Yolo_Dect:def __init__(self):# load parametersweight_path = rospy.get_param('~weight_path', '')image_topic = rospy.get_param('~image_topic', '/camera/color/image_raw')pub_topic = rospy.get_param('~pub_topic', '/yolov8/BoundingBoxes')self.camera_frame = rospy.get_param('~camera_frame', '')conf = rospy.get_param('~conf', '0.5')self.visualize = rospy.get_param('~visualize', 'True')# which device will be usedif (rospy.get_param('/use_cpu', 'true')):self.device = 'cpu'else:self.device = 'cuda'self.model = YOLO(weight_path)self.model.fuse()self.model.conf = confself.color_image = Image()self.getImageStatus = False# Load class colorself.classes_colors = {}# image subscribeself.color_sub = rospy.Subscriber(image_topic, Image, self.image_callback,queue_size=1, buff_size=52428800)# output publishersself.position_pub = rospy.Publisher(pub_topic,  BoundingBoxes, queue_size=1)self.image_pub = rospy.Publisher('/yolov8/detection_image',  Image, queue_size=1)# Load image and detectself.load_and_detect()def image_callback(self, image):# Existing image callback logicpassdef load_and_detect(self):# Load image from file or a specific sourceimage_path = '/home/wyh/artrc_catkin/src/artrc_yolov8/image/60.jpg'  # Replace with your image pathself.color_image = cv2.imread(image_path)if self.color_image is None:rospy.logerr("Failed to load image from path: %s", image_path)returnself.color_image = cv2.cvtColor(self.color_image, cv2.COLOR_BGR2RGB)results = self.model(self.color_image, show=False, conf=0.3)self.dectshow(results, self.color_image.shape[0], self.color_image.shape[1])cv2.waitKey(3)def dectshow(self, results, height, width):# Existing detection logicself.frame = results[0].plot()print(str(results[0].speed['inference']))fps = 1000.0 / results[0].speed['inference']cv2.putText(self.frame, f'FPS: {int(fps)}', (20, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2, cv2.LINE_AA)self.boundingBoxes = BoundingBoxes()self.boundingBoxes.header = Header(stamp=rospy.Time.now())self.boundingBoxes.image_header = Header(stamp=rospy.Time.now())# 统计数量class_count = {}total_count = 0for result in results[0].boxes:boundingBox = BoundingBox()boundingBox.xmin = np.int64(result.xyxy[0][0].item())boundingBox.ymin = np.int64(result.xyxy[0][1].item())boundingBox.xmax = np.int64(result.xyxy[0][2].item())boundingBox.ymax = np.int64(result.xyxy[0][3].item())boundingBox.Class = results[0].names[result.cls.item()]boundingBox.probability = result.conf.item()self.boundingBoxes.bounding_boxes.append(boundingBox)if boundingBox.Class in class_count:class_count[boundingBox.Class] += 1else:class_count[boundingBox.Class] = 1total_count += 1print("cl:",boundingBox.Class)self.position_pub.publish(self.boundingBoxes)self.publish_image(self.frame, height, width)print("data",self.boundingBoxes)print("Class Count:", class_count)print("total count:",total_count)# if self.visualize:# cv2.imshow('YOLOv8', self.frame)def publish_image(self, imgdata, height, width):image_temp = Image()header = Header(stamp=rospy.Time.now())header.frame_id = self.camera_frameimage_temp.height = heightimage_temp.width = widthimage_temp.encoding = 'bgr8'image_temp.data = np.array(imgdata).tobytes()image_temp.header = headerimage_temp.step = width * 3self.image_pub.publish(image_temp)def main():rospy.init_node('yolov8_ros', anonymous=True)yolo_dect = Yolo_Dect()rospy.spin()if __name__ == "__main__":main()

5.3 利用.onnx文件进行检测

#!/home/wyh/.conda/envs/yolov8/bin/python3.8
# -*- coding: utf-8 -*-
import onnxruntime as rt
import numpy as np
import cv2
import matplotlib.pyplot as plt# 定义类别标签
CLASS_NAMES = ['corrosion','craze', 'hide_craze','surface_attach','surface_corrosion','surface_eye','surface_injure','surface_oil','thunderstrike']  # 请根据你的模型定义实际的类标签COLOR_MAP = {"label_0": (255, 0, 0),       # 红色"label_1": (0, 255, 0),       # 绿色"label_2": (0, 0, 255),       # 蓝色"label_3": (255, 255, 0),     # 黄色"label_4": (255, 0, 255),     # 品红色"label_5": (0, 255, 255),     # 青色"label_6": (128, 0, 128),     # 紫色"label_7": (255, 165, 0),     # 橙色"label_8": (128, 128, 128),   # 灰色
}def nms(pred, conf_thres, iou_thres): conf = pred[..., 4] > conf_thresbox = pred[conf == True] cls_conf = box[..., 5:]cls = []for i in range(len(cls_conf)):cls.append(int(np.argmax(cls_conf[i])))total_cls = list(set(cls))  output_box = []  for i in range(len(total_cls)):clss = total_cls[i] cls_box = []for j in range(len(cls)):if cls[j] == clss:box[j][5] = clsscls_box.append(box[j][:6])cls_box = np.array(cls_box)box_conf = cls_box[..., 4]  box_conf_sort = np.argsort(box_conf) max_conf_box = cls_box[box_conf_sort[len(box_conf) - 1]]output_box.append(max_conf_box) cls_box = np.delete(cls_box, 0, 0) while len(cls_box) > 0:max_conf_box = output_box[len(output_box) - 1]  del_index = []for j in range(len(cls_box)):current_box = cls_box[j]  interArea = getInter(max_conf_box, current_box)  iou = getIou(max_conf_box, current_box, interArea)  if iou > iou_thres:del_index.append(j)  cls_box = np.delete(cls_box, del_index, 0)  if len(cls_box) > 0:output_box.append(cls_box[0])cls_box = np.delete(cls_box, 0, 0)return output_boxdef getIou(box1, box2, inter_area):box1_area = box1[2] * box1[3]box2_area = box2[2] * box2[3]union = box1_area + box2_area - inter_areaiou = inter_area / unionreturn ioudef getInter(box1, box2):box1_x1, box1_y1, box1_x2, box1_y2 = box1[0] - box1[2] / 2, box1[1] - box1[3] / 2, \box1[0] + box1[2] / 2, box1[1] + box1[3] / 2box2_x1, box2_y1, box2_x2, box2_y2 = box2[0] - box2[2] / 2, box2[1] - box1[3] / 2, \box2[0] + box2[2] / 2, box2[1] + box2[3] / 2if box1_x1 > box2_x2 or box1_x2 < box2_x1:return 0if box1_y1 > box2_y2 or box1_y2 < box2_y1:return 0x_list = [box1_x1, box1_x2, box2_x1, box2_x2]x_list = np.sort(x_list)x_inter = x_list[2] - x_list[1]y_list = [box1_y1, box1_y2, box2_y1, box2_y2]y_list = np.sort(y_list)y_inter = y_list[2] - y_list[1]inter = x_inter * y_interreturn inter# 画框并添加标签
def draw(img, xscale, yscale, pred):img_ = img.copy()if len(pred):for detect in pred:label = int(detect[5])  # 获取类别标签label_name = CLASS_NAMES[label]  # 通过类索引获取类名detect_coords = [int((detect[0] - detect[2] / 2) * xscale), int((detect[1] - detect[3] / 2) * yscale),int((detect[0] + detect[2] / 2) * xscale), int((detect[1] + detect[3] / 2) * yscale)]# 获取颜色,如果没有对应的颜色,就使用默认颜色color = COLOR_MAP.get(label_name, (255, 255, 255))  # 默认为白色# 绘制矩形框img_ = cv2.rectangle(img_, (detect_coords[0], detect_coords[1]), (detect_coords[2], detect_coords[3]), color, 2)# 绘制标签img_ = cv2.putText(img_, label_name, (detect_coords[0], detect_coords[1]-5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 1)return img_if __name__ == '__main__':height, width = 640, 640img0 = cv2.imread('/home/wyh/artrc_catkin/src/artrc_yolov8/image/60.jpg')x_scale = img0.shape[1] / widthy_scale = img0.shape[0] / heightimg = img0 / 255.img = cv2.resize(img, (width, height))img = np.transpose(img, (2, 0, 1))data = np.expand_dims(img, axis=0)sess = rt.InferenceSession('/home/wyh/artrc_catkin/src/artrc_yolov8/weights/best.onnx')input_name = sess.get_inputs()[0].namelabel_name = sess.get_outputs()[0].namepred = sess.run([label_name], {input_name: data.astype(np.float32)})[0]pred = np.squeeze(pred)pred = np.transpose(pred, (1, 0))pred_class = pred[..., 4:]pred_conf = np.max(pred_class, axis=-1)pred = np.insert(pred, 4, pred_conf, axis=-1)result = nms(pred, 0.3, 0.45)ret_img = draw(img0, x_scale, y_scale, result)# 使用OpenCV显示图像cv2.imshow('Detection Result', ret_img)cv2.waitKey(0)  # 等待按键事件cv2.destroyAllWindows()  # 关闭所有OpenCV窗口

5.3 利用.trt文件进行检测

#include <ros/ros.h>
#include <image_transport/image_transport.h>
#include <cv_bridge/cv_bridge.h>
#include <sensor_msgs/image_encodings.h>
#include <std_msgs/Header.h>
#include <opencv2/opencv.hpp>
#include "../include/artrc_yolov8/yolo.h"
#include "../include/artrc_yolov8/yolov8.h"
#include <NvInfer.h>
#include <NvUtils.h>
#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <fstream>
#include "../include/artrc_yolov8/yolov8_trt.h"
#include <mutex>
cv::Mat image_;
namespace artrc_yolov8
{YoloResultData::~YoloResultData(){;}void YoloResultData::init(){ros::NodeHandle nh;img_receive_sub_ = nh.subscribe("/usb_camera/image_raw",1,&YoloResultData::image_receive_callback,this);img_detect_pub_ = nh.advertise<sensor_msgs::Image>("/detect_img", 1);boundingbox_result_pub_ = nh.advertise<artrc_yolov8::boundingbox_result_msgs>("/boundingbox_result",1);}void YoloResultData::processImage() {if (!image_.empty()) {// 在这里处理图像cv::Mat processedImage = image_.clone(); // 例如,你可以对图像进行一些处理// 显示图像// cv::imshow("Processed Image", processedImage);// cv::waitKey(30); // 等待30毫秒} else {ROS_WARN("No image received yet.");}}// 设置检测内参数void YoloResultData::setParameters(utils::InitParameter& initParameters){initParameters.class_names = utils::dataSets::coco80;initParameters.num_class = 80; // for cocoinitParameters.batch_size = 8;initParameters.dst_h = 640;initParameters.dst_w = 640;initParameters.input_output_names = { "images",  "output0" };initParameters.conf_thresh = 0.25f;initParameters.iou_thresh = 0.45f;initParameters.save_path = "";}//  yolo模型预测void YoloResultData::task(YOLOV8& yolo, const utils::InitParameter& param, std::vector<cv::Mat>& imgsBatch, const int& delayTime, const int& batchi,const bool& isShow, const bool& isSave){if (imgsBatch.empty()) {std::cerr << "Input image batch is empty." << std::endl;return;}// std::cout<< "--------------------------------"<< std::endl;std::clock_t start = std::clock();utils::DeviceTimer d_t0; yolo.copy(imgsBatch);	      float t0 = d_t0.getUsedTime();utils::DeviceTimer d_t1; yolo.preprocess(imgsBatch);  float t1 = d_t1.getUsedTime();utils::DeviceTimer d_t2; yolo.infer();				  float t2 = d_t2.getUsedTime();utils::DeviceTimer d_t3; yolo.postprocess(imgsBatch); float t3 = d_t3.getUsedTime();std::clock_t end = std::clock();// 计算时间差double duration = static_cast<double>(end - start) / CLOCKS_PER_SEC;// 输出运行时间// std::cout << "程序运行时间: " << duration << " 秒" << std::endl;// std::cout << "delayTime"<< delayTime << std::endl;if(isShow)utils::show(yolo.getObjectss(), param.class_names, delayTime, imgsBatch);// if(isSave)// 	utils::save(yolo.getObjectss(), param.class_names, param.save_path, imgsBatch, param.batch_size, batchi);// 在终端输出检测结果YoloResultData::result_show(yolo, param, t1, t2, t3);// YoloResultData::result_show(yolo, param);// // std::cout<<"77777777777777777"<<std::endl;for (size_t bi = 0; bi < imgsBatch.size(); bi++){cv_bridge::CvImagePtr cv_ptr(new cv_bridge::CvImage);cv_ptr->image = imgsBatch[bi];cv_ptr->encoding = "bgr8";img_detect_pub_.publish(cv_ptr->toImageMsg());}yolo.reset();}// 显示输出结果void YoloResultData::result_show(const YOLOV8& yolo, const utils::InitParameter& param, float t1, float t2, float t3) // void YoloResultData::result_show(const YOLOV8& yolo, const utils::InitParameter& param) {const auto& objectss = yolo.getObjectss();for (size_t bi = 0; bi < objectss.size(); bi++){for (const auto& box : objectss[bi]){// std::cout<< "preprocess time:"<< t1 / param.batch_size <<";   "  // "infer time:"<< t2 / param.batch_size << ";   "  // "postprocess time:"<<t3 / param.batch_size<<std::endl;// std::cout << "Image " << bi << ": Detected box - "// std::cout << "Label: " << param.class_names[box.label] << ", "// 		<< "Confidence: " << box.confidence << ", "// 		<< "Bounding Box: [" << box.left << ", "// 		<< box.top << ", "// 		<< box.right << ", "// 		<< box.bottom << "]" << std::endl;pub_msg_.label = param.class_names[box.label];pub_msg_.confidence = box.confidence;pub_msg_.xmin = box.left;pub_msg_.xmax = box.right;pub_msg_.ymin = box.top;// 填充边界框数组pub_msg_.bounding_box.clear();  // 确保清空之前的数据pub_msg_.bounding_box.push_back(box.left);pub_msg_.bounding_box.push_back(box.top);pub_msg_.bounding_box.push_back(box.right);pub_msg_.bounding_box.push_back(box.bottom);boundingbox_result_pub_.publish(pub_msg_);}}}// 订阅图像数据void YoloResultData::image_receive_callback(const sensor_msgs::Image& image_msg){cv_bridge::CvImagePtr cv_ptr;try {cv_ptr = cv_bridge::toCvCopy(image_msg, sensor_msgs::image_encodings::BGR8);// 处理图像(例如显示)image_ = cv_ptr->image;// cv::imshow("Image", cv_ptr->image);// cv::waitKey(30); // 等待30毫秒} catch (cv_bridge::Exception& e) {ROS_ERROR("cv_bridge exception: %s", e.what());return;}}
}int main(int argc, char** argv)
{ros::init(argc, argv, "yolov8_ros_node");artrc_yolov8::YoloResultData YoloResultData_node;YoloResultData_node.init();utils::InitParameter param;YoloResultData_node.setParameters(param);std::string model_path = "/home/wyh/artrc_catkin/src/artrc_yolov8/weights/yolov8n.trt";//加载模型std::string video_path = "/home/wyh/artrc_catkin/src/artrc_yolov8/image/行人视频.mp4";std::string image_path = "/home/wyh/artrc_catkin/src/artrc_yolov8/image/6406406.jpg";int camera_id = 0;//  get input 输入源 判断utils::InputStream source;source = utils::InputStream::IMAGE;// source = utils::InputStream::VIDEO;// source = utils::InputStream::CAMERA;// source = utils::InputStream::TOPIC_IMAGE;// update params from command line parserint size = -1; // w or hint batch_size = 8;bool is_show = false;bool is_save = false;int total_batches = 0;int delay_time = 50;// / 从参数服务器获取参数ros::param::get("~size", size);ros::param::get("~batch_size", batch_size);ros::param::get("~show", is_show);// 参数赋值param.dst_h = param.dst_w = size;param.batch_size = batch_size;param.is_show = is_show;// cv::VideoCapture capture(1);cv::VideoCapture capture(1);if (!setInputStream(source, image_path, video_path, camera_id,capture, total_batches, delay_time, param)){sample::gLogError << "read the input data errors!" << std::endl;return -1;}std::vector<unsigned char> trt_file = utils::loadModel(model_path);// // read modelif (trt_file.empty()){	std::cout << "trt_file is empty!" << std::endl;}else{std::cout << "trt_file is load!" << std::endl;}YOLOV8 yolo(param);// // init modelif (!yolo.init(trt_file)){std::cout << "initEngine() ocur errors!" << std::endl;}else{std::cout << "initEngine() ocur success!" << std::endl;	}yolo.check();std::vector<cv::Mat> imgs_batch;imgs_batch.reserve(param.batch_size);int batchi = 0;cv::Mat frame;ros::Rate rate(50);while (ros::ok()){// std::cout << "imgs_batch_" << imgs_batch.size() << ";"<< "batch_size" << param.batch_size << std::endl;if (imgs_batch.size() < param.batch_size) // get input{	if (source == utils::InputStream::VIDEO){capture.read(frame);// std::cout<<"00000_video"<< std::endl;}else if (source == utils::InputStream::CAMERA){capture.read(frame);// std::cout<<"11111_camera"<< std::endl;}else if (source == utils::InputStream::IMAGE){// std::cout<<"22222_image"<< std::endl;// frame = cv::imread(image_path);// 获取图像数据frame = YoloResultData_node.image_;	}else {// std::cout<<"33333_topic"<<std::endl;frame = YoloResultData_node.image_;	}if (!frame.empty()){imgs_batch.emplace_back(frame.clone());}else{int delay_time = 5;sample::gLogWarning << "no more video or camera frame" << std::endl;YoloResultData_node.task(yolo, param, imgs_batch, delay_time, batchi, is_show, is_save);imgs_batch.clear();batchi++;}}else{int delay_time = 1;YoloResultData_node.task(yolo, param, imgs_batch, delay_time, batchi, is_show, is_save);imgs_batch.clear();batchi++;}ros::spinOnce();  // Handle all callbacksrate.sleep();     // Sleep for a while before next loop iteration}// ros::spin();return 0;
}
# 将下述程序参数更改为自己类别
initParameters.class_names = utils::dataSets::coco80;
initParameters.num_class = 80; 
# 将权重文件替换为相应的文件

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/463760.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

海睿思产品体系二次开发能力介绍

海睿思产品体系支持用户通过编写开发代码&#xff08;含低代码&#xff09;&#xff0c;提供定制化功能&#xff0c;解决数据治理和共享场景的个性化需求。 1、数据集成 应用场景&#xff1a;自定义数据集成能力 开发语言&#xff1a;JAVA 使用效果&#xff1a; 1&#xff…

SpringMVC笔记 一万字

此笔记来自于B站尚硅谷 文章目录 一、SpringMVC 简介1、什么是MVC2、什么是SpringMVC3、SpringMVC的特点 二、HelloWorld1、开发环境2、创建maven工程a>添加web模块b>打包方式&#xff1a;warc>引入依赖 3、配置web.xmla>默认配置方式b>扩展配置方式 4、创建请求…

Maven随笔

文章目录 1、什么是MAVEN2、Maven模型3、Maven仓库4、项目集成1_Idea集成Maven设置2_创建Maven项目3_POM配置详解4_maven 坐标详情5_Maven工程类型6_导入Maven项目 5、依赖管理1_依赖配置2_依赖传递3_可选依赖4_排除依赖4_可选依赖和排除依赖的区别5_依赖范围6_继承与聚合7_版本…

看门狗有什么用?

看门狗(WATCH-DOG Timer)全称看门狗定时器 用途&#xff1a;防止程序死机&#xff08;主要用途&#xff09; 或 单纯用来计时 直接上代码&#xff1a; 只要程序可以正常运行&#xff0c;看门狗就会清零&#xff0c;如果崩了&#xff0c;看门狗就会复位程序

云原生开源开发者沙龙丨AI 应用工程化专场杭州站邀您参会

云原生开源开发者沙龙 AI 原生应用架构专场&#xff0c;邀您一起交流&#xff0c;探索 AI 原生应用的工程化落地&#xff01; 活动简介 AI 驱动的应用程序开发、部署和运维&#xff0c;给应用带来了新的生命力和想象空间。但大部分开发者对 AI 应用的编程框架、可观测体系、网…

UFO:Windows操作系统的具象智能代理

近年来&#xff0c;随着AI技术的发展&#xff0c;智能代理在各种应用中扮演着越来越重要的角色。微软推出的UFO&#xff08;User-Focused Operator&#xff09;正是这样一个出色的多代理框架&#xff0c;旨在通过无缝导航和操作&#xff0c;满足用户在Windows操作系统中跨多个应…

基于人工智能的搜索和推荐系统

互联网上的搜索历史分析和用户活动是个性化推荐的基础&#xff0c;这些推荐已成为电子商务行业和在线业务的强大营销工具。随着人工智能的使用&#xff0c;在线搜索也在改进&#xff0c;因为它会根据用户的视觉偏好提出建议&#xff0c;而不是根据每个客户的需求和偏好量身定制…

使用ffmpeg和mediamtx模拟多通道rtsp相机

首先下载ffmpeg&#xff0c;在windows系统上直接下载可执行文件&#xff0c;并配置环境变量即可在命令行当中调用执行。 下载地址&#xff1a; https://ffmpeg.org/再在github上下载mediamtx搭建rtsp服务器&#xff0c;使用ffmpeg将码流推流到rtsp服务器。 下载地址&#xff1…

Spring Boot框架:校园社团信息管理的现代化解决方案

3系统分析 3.1可行性分析 通过对本校园社团信息管理系统实行的目的初步调查和分析&#xff0c;提出可行性方案并对其一一进行论证。我们在这里主要从技术可行性、经济可行性、操作可行性等方面进行分析。 3.1.1技术可行性 本校园社团信息管理系统采用SSM框架&#xff0c;JAVA作…

软件测试面试八股文个人总结

一、软件测试基础面试题 1、阐述软件生命周期都有哪些阶段? 常见的软件生命周期模型有哪些? 软件生命周期是指一个计算机软件从功能确定设计&#xff0c;到开发成功投入使用&#xff0c;并在使用中不断地修改、增补和完善&#xff0c;直到停止该软件的使用的全过程(从酝酿到…

彻底理解链表(LinkedList)结构

目录 比较操作结构封装单向链表实现面试题 循环链表实现 双向链表实现 链表&#xff08;Linked List&#xff09;是一种线性数据结构&#xff0c;由一组节点&#xff08;Node&#xff09;组成&#xff0c;每个节点包含两个部分&#xff1a;数据域&#xff08;存储数据&#xff…

C#/.NET/.NET Core优秀项目和框架2024年10月简报

前言 每月定期推广和分享的C#/.NET/.NET Core优秀项目和框架&#xff08;每周至少会推荐两个优秀的项目和框架当然节假日除外&#xff09;&#xff0c;推文中有项目和框架的介绍、功能特点、使用方式以及部分功能截图等&#xff08;打不开或者打开GitHub很慢的同学可以优先查看…

数据库作业5

1&#xff0c;建立触发器&#xff0c;订单表中增加订单数量后&#xff0c;商品表商品数量同步减少对应的商品订单出数量,并测试 测试&#xff1a; 2.建立触发器&#xff0c;实现功能:客户取消订单&#xff0c;恢复商品表对应商品的数量 测试 3.建立触发器&#xff0c;实现功能:…

证书下行,这些高质量IT证书仍值得考

在快速变化的信息技术领域&#xff0c;专业认证不仅证明了个人的专业技能&#xff0c;还常常是许多企业在招聘时考虑的重要标准&#xff0c;因此很多IT朋友都会选择考一本证书来傍身。 然而&#xff0c;随着技术的发展和市场的变化&#xff0c;并不是所有的IT证书都能保持其原有…

华为荣耀曲面屏手机下面空白部分设置颜色的方法

荣耀部分机型下面有一块空白区域&#xff0c;如下图红框部分 设置这部分的颜色需要在themes.xml里面设置navigationBarColor属性 <item name"android:navigationBarColor">android:color/white</item>

【开源免费】基于SpringBoot+Vue.JS新闻推荐系统(JAVA毕业设计)

博主说明&#xff1a;本文项目编号 T 056 &#xff0c;文末自助获取源码 \color{red}{T056&#xff0c;文末自助获取源码} T056&#xff0c;文末自助获取源码 目录 一、系统介绍二、演示录屏三、启动教程四、功能截图五、文案资料5.1 选题背景5.2 国内外研究现状5.3 可行性分析…

使用DJL和PaddlePaddle的口罩检测详细指南

使用DJL和PaddlePaddle的口罩检测详细指南 完整代码 该项目利用DJL和PaddlePaddle的预训练模型&#xff0c;构建了一个口罩检测应用程序。该应用能够在图片中检测人脸&#xff0c;并将每张人脸分类为“戴口罩”或“未戴口罩”。我们将深入分析代码的每个部分&#xff0c;以便…

【MySQL】架构

1. MySQL架构基本理解 与餐厅就餐类比理解 每次数据库查询就像一次餐厅服务 应用程序发出查询相当于点菜MySQL解析和执行查询&#xff0c;后厨根据订单制作食物事务管理保证数据的一致性&#xff0c;类似于结账的时候保证账单正确查询的时候考虑优化器&#xff0c;类似于厨师选…

Linux服务器或Linux计算机设置不记录历史命令

1.设置环境变量 打开命令终端&#xff0c;编辑.bashrc文件中&#xff0c;修改HISTSIZE和HISTFILESIZE都为0 sudo nano ~/.bashrcfor setting history length see HISTSIZE and HISTFILESIZE in bash(1) HISTSIZE0 HISTFILESIZE0 2.生效 source ~/.bashrc 3.验证 cat ./b…

基于Spring Boot的中小型制造企业质量管理系统设计与实现,LW+源码+讲解

摘 要 信息数据从传统到当代&#xff0c;是一直在变革当中&#xff0c;突如其来的互联网让传统的信息管理看到了革命性的曙光&#xff0c;因为传统信息管理从时效性&#xff0c;还是安全性&#xff0c;还是可操作性等各个方面来讲&#xff0c;遇到了互联网时代才发现能补上自…