### Add Installation Directory to PATH Source: https://github.com/laugh12321/tensorrt-yolo/blob/main/README.en.md Add the TensorRT-YOLO installation directory to your system's PATH environment variable to allow CMake to find the configuration file. Examples for both Linux and Windows are provided. ```bash export PATH=$PATH:/your/tensorrt-yolo/install/dir # linux $env:PATH = "$env:PATH;C:\your\tensorrt-yolo\install\dir;C:\your\tensorrt-yolo\install\dir\bin" # windows ``` -------------------------------- ### Install Target Function Source: https://github.com/laugh12321/tensorrt-yolo/blob/main/modules/trtyolo/CMakeLists.txt This function handles the installation of targets based on their type. It supports 'library' types (installing to lib directory) and 'python' types (installing to a specific project source directory). ```cmake function(install_target target target_type) if(target_type STREQUAL "library") # Install library target install(TARGETS ${target} EXPORT ${target}Targets LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} # ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} # RUNTIME DESTINATION ${CMAKE_INSTALL_LIBDIR} ) elseif(target_type STREQUAL "python") # Install Python binding target install(TARGETS ${target} EXPORT ${target}Targets LIBRARY DESTINATION ${PROJECT_SOURCE_DIR}/trtyolo/libs ) else() message(WARNING "Unknown target type: ${target_type}") endif() endfunction() ``` -------------------------------- ### Build and Install trtyolo Library Source: https://github.com/laugh12321/tensorrt-yolo/blob/main/modules/trtyolo/CMakeLists.txt Defines the main shared library 'trtyolo', adds its compile files, configures common properties, and installs it as a library. It also sets up package version and directory variables. ```cmake # trtyolo library add_library(trtyolo SHARED) add_target_compile_files(trtyolo) configure_target_common_properties(trtyolo) install_target(trtyolo "library") # Set variables set(PACKAGE_VERSION "${PROJECT_VERSION}") set(PACKAGE_DIRS "${CMAKE_INSTALL_PREFIX}") set(PACKAGE_INCLUDE_DIRS "${CMAKE_INSTALL_INCLUDEDIR}") set(PACKAGE_LIBRARY_DIRS "${CMAKE_INSTALL_LIBDIR}") set(PACKAGE_LIBRARIES "trtyolo") # Install header files install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/infer/trtyolo.hpp" DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" ) # Create tensorrt-yolo-config.cmake file configure_file( "${PROJECT_SOURCE_DIR}/cmake/tensorrt-yolo-config.cmake.in" "${PROJECT_SOURCE_DIR}/cmake/tensorrt-yolo-config.cmake" @ONLY ) install(FILES "${PROJECT_SOURCE_DIR}/cmake/tensorrt-yolo-config.cmake" DESTINATION "${CMAKE_INSTALL_PREFIX}" ) ``` -------------------------------- ### Build and Install Python Bindings Source: https://github.com/laugh12321/tensorrt-yolo/blob/main/README.en.md Build the Python wheel package and install it to enable Python inference. This requires the BUILD_PYTHON option to be enabled during CMake compilation. ```bash pip install --upgrade build python -m build --wheel pip install dist/trtyolo-6.*-py3-none-any.whl ``` -------------------------------- ### Build and Install Python Bindings Source: https://github.com/laugh12321/tensorrt-yolo/blob/main/modules/trtyolo/CMakeLists.txt Conditionally builds and installs Python bindings for 'trtyolo' if `BUILD_PYTHON` is enabled. It uses `pybind11_add_module` to create the Python module and installs it as a Python target. ```cmake # Python bindings (optional) if(BUILD_PYTHON) # Create Python binding module pybind11_add_module(py_trtyolo ${CMAKE_CURRENT_SOURCE_DIR}/binding/trtyolo.cpp) add_target_compile_files(py_trtyolo) configure_target_common_properties(py_trtyolo) install_target(py_trtyolo "python") # Configure Python configuration file configure_file( ${PROJECT_SOURCE_DIR}/trtyolo/c_lib_wrap.py.in ${PROJECT_SOURCE_DIR}/trtyolo/c_lib_wrap.py ) endif() ``` -------------------------------- ### Clone TensorRT-YOLO Repository Source: https://github.com/laugh12321/tensorrt-yolo/blob/main/README.en.md Clone the TensorRT-YOLO repository and navigate into the directory. This is the first step for compilation and installation. ```bash git clone https://github.com/laugh12321/TensorRT-YOLO cd TensorRT-YOLO ``` -------------------------------- ### Compile TensorRT-YOLO with CMake Source: https://github.com/laugh12321/tensorrt-yolo/blob/main/README.en.md Compile the project using CMake, specifying TensorRT path and build options for Python bindings. Ensure pybind11 is installed. ```bash pip install "pybind11[global]" # Install pybind11 to generate Python bindings cmake -S . -B build -D TRT_PATH=/your/tensorrt/dir -D BUILD_PYTHON=ON -D CMAKE_INSTALL_PREFIX=/your/tensorrt-yolo/install/dir cmake --build build -j$(nproc) --config Release --target install ``` -------------------------------- ### Compile VideoPipe Project with TensorRT-YOLO Source: https://github.com/laugh12321/tensorrt-yolo/blob/main/examples/VideoPipe/README.en.md Configure and compile the project, linking it with the VideoPipe library. The executable will be generated in the workspace folder. ```bash cmake -S . -B build -D VIDEOPIPE_PATH="/path/to/your/VideoPipe" cmake --build . -j8 --config Release ``` -------------------------------- ### Run VideoPipe Inference with TensorRT-YOLO Source: https://github.com/laugh12321/tensorrt-yolo/blob/main/examples/VideoPipe/README.en.md Navigate to the workspace directory and execute the compiled PipeDemo application for video inference. ```bash cd workspace ./PipeDemo ``` -------------------------------- ### Python Multi-threading Inference with Model Cloning Source: https://github.com/laugh12321/tensorrt-yolo/blob/main/examples/mutli_thread/README.en.md Demonstrates initializing a TensorRT-YOLO model, performing inference on an image, and cloning the model for multi-threading scenarios. It also shows how to evaluate performance metrics. ```python import cv2 import supervision as sv from trtyolo import TRTYOLO # -------------------- Initialize the model -------------------- # Note: The task parameter must match the task type of the exported ONNX model ("detect", "segment", "classify", "pose", "obb") # The profile parameter, when enabled, calculates performance metrics during inference, which can be retrieved by calling model.profile() # The swap_rb parameter, when enabled, swaps the channel order before inference (ensuring the model input is RGB) model = TRTYOLO("yolo11n-with-plugin.engine", task="detect", profile=True, swap_rb=True) # -------------------- Load the test image and perform inference -------------------- image = cv2.imread("test_image.jpg") result = model.predict(image) print(f"==> result: {result}") # -------------------- Visualize the results -------------------- box_annotator = sv.BoxAnnotator() annotated_frame = box_annotator.annotate(scene=image.copy(), detections=result) # -------------------- Performance evaluation -------------------- throughput, cpu_latency, gpu_latency = model.profile() print(throughput) print(cpu_latency) print(gpu_latency) # -------------------- Clone the model -------------------- # Clone the model instance (suitable for multi-threading scenarios) cloned_model = model.clone() # Create an independent copy to avoid resource contention # Verify the consistency of inference with the cloned model cloned_result = cloned_model.predict(input_img) print(f"==> cloned_result: {cloned_result}") ``` -------------------------------- ### C++ Multi-threading Inference with Model Cloning Source: https://github.com/laugh12321/tensorrt-yolo/blob/main/examples/mutli_thread/README.en.md Shows how to initialize a TensorRT-YOLO detection model in C++, perform inference on an image, and clone the model instance for multi-threaded applications. Includes error handling. ```cpp #include #include #include "trtyolo.hpp" int main() { try { // -------------------- Initialization -------------------- trtyolo::InferOption option; option.enableSwapRB(); // BGR->RGB conversion // Special model parameter setup example // const std::vector mean{0.485f, 0.456f, 0.406f}; // const std::vector std{0.229f, 0.224f, 0.225f}; // option.setNormalizeParams(mean, std); // -------------------- Model Initialization -------------------- // The models ClassifyModel, DetectModel, OBBModel, SegmentModel, and PoseModel correspond to image classification, detection, oriented bounding box, segmentation, and pose estimation models, respectively. auto detector = std::make_unique( "yolo11n-with-plugin.engine", // Model path option // Inference settings ); // -------------------- Data Loading -------------------- cv::Mat cv_image = cv::imread("test_image.jpg"); if (cv_image.empty()) { throw std::runtime_error("Failed to load test image."); } // Encapsulate image data (no pixel data copying) trtyolo::Image input_image( cv_image.data, // Pixel data pointer cv_image.cols, // Image width cv_image.rows // Image height ); // -------------------- Inference Execution -------------------- trtyolo::DetectRes result = detector->predict(input_image); std::cout << result << std::endl; // -------------------- Result Visualization (Example) -------------------- // Implement visualization logic in actual development, e.g.: // cv::Mat vis_image = visualize_detections(cv_image, result); // cv::imwrite("vis_result.jpg", vis_image); // -------------------- Model Cloning Demo -------------------- auto cloned_detector = detector->clone(); // Create an independent instance trtyolo::DetectRes cloned_result = cloned_detector->predict(input_image); // Verify result consistency std::cout << cloned_result << std::endl; } catch (const std::exception& e) { std::cerr << "Program Exception: " << e.what() << std::endl; return EXIT_FAILURE; } return EXIT_SUCCESS; } ``` -------------------------------- ### Export YOLO Model to ONNX and Convert to TensorRT-YOLO Format Source: https://github.com/laugh12321/tensorrt-yolo/blob/main/examples/VideoPipe/README.en.md Export the YOLO model to ONNX format and then convert it to the TensorRT-YOLO compatible format using trtyolo-export. Ensure the EfficientNMS plugin is integrated. ```bash yolo export model=workspace/yolo11n.pt format=onnx batch=2 trtyolo-export -i workspace/yolo11n.onnx -o workspace/yolo11n-trtyolo.onnx -s ``` -------------------------------- ### Build TensorRT Engine from Converted ONNX Model Source: https://github.com/laugh12321/tensorrt-yolo/blob/main/examples/VideoPipe/README.en.md Use trtexec to build a TensorRT engine from the converted ONNX file, specifying fp16 precision. ```bash trtexec --onnx=workspace/yolo11n-trtyolo.onnx --saveEngine=workspace/yolo11n.engine --fp16 ``` -------------------------------- ### Python Inference with TensorRT-YOLO Source: https://github.com/laugh12321/tensorrt-yolo/blob/main/README.en.md Use this snippet to perform object detection inference with a TensorRT-YOLO model in Python. Ensure the model engine file and test image are accessible. The `profile` parameter enables performance metric collection. ```python import cv2 import supervision as sv from trtyolo import TRTYOLO # -------------------- Initialize the model -------------------- # Note: The task parameter must match the task type specified during export ("detect", "segment", "classify", "pose", "obb") # The profile parameter, when enabled, calculates performance metrics during inference, which can be retrieved by calling model.profile() # The swap_rb parameter, when enabled, swaps the channel order before inference (ensuring the model input is RGB) model = TRTYOLO("yolo11n-with-plugin.engine", task="detect", profile=True, swap_rb=True) # -------------------- Load the test image and perform inference -------------------- image = cv2.imread("test_image.jpg") result = model.predict(image) print(f"==> result: {result}") # -------------------- Visualize the results -------------------- box_annotator = sv.BoxAnnotator() annotated_frame = box_annotator.annotate(scene=image.copy(), detections=result) # -------------------- Performance evaluation -------------------- throughput, cpu_latency, gpu_latency = model.profile() print(throughput) print(cpu_latency) print(gpu_latency) # -------------------- Clone the model -------------------- # Clone the model instance (suitable for multi-threading scenarios) cloned_model = model.clone() # Create an independent copy to avoid resource contention # Verify the consistency of inference with the cloned model cloned_result = cloned_model.predict(input_img) print(f"==> cloned_result: {cloned_result}") ``` -------------------------------- ### Add Target Compile Files Function Source: https://github.com/laugh12321/tensorrt-yolo/blob/main/modules/trtyolo/CMakeLists.txt This function collects source files from specified directories and adds them to a target. It uses `file(GLOB_RECURSE)` to find C++ and CUDA files and `target_sources` to add them to the target. `CONFIGURE_DEPENDS` ensures automatic updates when source files change. ```cmake function(add_target_compile_files target) # Add project source directory to target's private include directories target_include_directories(${target} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) # Collect source files (use CONFIGURE_DEPENDS for automatic updates) file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/core/*.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/utils/*.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/infer/*.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/infer/*.cu" ) # Add collected source files to the target target_sources(${target} PRIVATE ${SOURCES}) endfunction() ``` -------------------------------- ### Configure Target Common Properties Function Source: https://github.com/laugh12321/tensorrt-yolo/blob/main/modules/trtyolo/CMakeLists.txt This function sets common properties for a target, including its output name and C++ visibility. It also calls helper functions to configure CUDA/TensorRT properties and compile options. ```cmake function(configure_target_common_properties target) # Set common properties for the target set_target_properties(${target} PROPERTIES OUTPUT_NAME "${target}" CXX_VISIBILITY_PRESET "hidden" ) # Configure CUDA and TensorRT related properties configure_cuda_trt_target(${target}) # Set target compile options set_target_compile_options(${target}) endfunction() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.