Yolov8-源码解析-八-

Yolov8 源码解析(八)


comments: true
description: Learn how to manage and optimize queues using Ultralytics YOLOv8 to reduce wait times and increase efficiency in various real-world applications.
keywords: queue management, YOLOv8, Ultralytics, reduce wait times, efficiency, customer satisfaction, retail, airports, healthcare, banks

Queue Management using Ultralytics YOLOv8 🚀

What is Queue Management?

Queue management using Ultralytics YOLOv8 involves organizing and controlling lines of people or vehicles to reduce wait times and enhance efficiency. It's about optimizing queues to improve customer satisfaction and system performance in various settings like retail, banks, airports, and healthcare facilities.



Watch: How to Implement Queue Management with Ultralytics YOLOv8 | Airport and Metro Station

Advantages of Queue Management?

  • Reduced Waiting Times: Queue management systems efficiently organize queues, minimizing wait times for customers. This leads to improved satisfaction levels as customers spend less time waiting and more time engaging with products or services.
  • Increased Efficiency: Implementing queue management allows businesses to allocate resources more effectively. By analyzing queue data and optimizing staff deployment, businesses can streamline operations, reduce costs, and improve overall productivity.

Real World Applications

Logistics Retail
Queue management at airport ticket counter using Ultralytics YOLOv8 Queue monitoring in crowd using Ultralytics YOLOv8
Queue management at airport ticket counter Using Ultralytics YOLOv8 Queue monitoring in crowd Ultralytics YOLOv8

!!! Example "Queue Management using YOLOv8 Example"

=== "Queue Manager"

    ```py
    import cv2

    from ultralytics import YOLO, solutions

    model = YOLO("yolov8n.pt")
    cap = cv2.VideoCapture("path/to/video/file.mp4")

    assert cap.isOpened(), "Error reading video file"
    w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))

    video_writer = cv2.VideoWriter("queue_management.avi", cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h))

    queue_region = [(20, 400), (1080, 404), (1080, 360), (20, 360)]

    queue = solutions.QueueManager(
        names=model.names,
        reg_pts=queue_region,
        line_thickness=3,
        fontsize=1.0,
        region_color=(255, 144, 31),
    )

    while cap.isOpened():
        success, im0 = cap.read()

        if success:
            tracks = model.track(im0, show=False, persist=True, verbose=False)
            out = queue.process_queue(im0, tracks)

            video_writer.write(im0)
            if cv2.waitKey(1) & 0xFF == ord("q"):
                break
            continue

        print("Video frame is empty or video processing has been successfully completed.")
        break

    cap.release()
    cv2.destroyAllWindows()
    ```

=== "Queue Manager Specific Classes"

    ```py
    import cv2

    from ultralytics import YOLO, solutions

    model = YOLO("yolov8n.pt")
    cap = cv2.VideoCapture("path/to/video/file.mp4")

    assert cap.isOpened(), "Error reading video file"
    w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))

    video_writer = cv2.VideoWriter("queue_management.avi", cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h))

    queue_region = [(20, 400), (1080, 404), (1080, 360), (20, 360)]

    queue = solutions.QueueManager(
        names=model.names,
        reg_pts=queue_region,
        line_thickness=3,
        fontsize=1.0,
        region_color=(255, 144, 31),
    )

    while cap.isOpened():
        success, im0 = cap.read()

        if success:
            tracks = model.track(im0, show=False, persist=True, verbose=False, classes=0)  # Only person class
            out = queue.process_queue(im0, tracks)

            video_writer.write(im0)
            if cv2.waitKey(1) & 0xFF == ord("q"):
                break
            continue

        print("Video frame is empty or video processing has been successfully completed.")
        break

    cap.release()
    cv2.destroyAllWindows()
    ```

Arguments QueueManager

Name Type Default Description
names dict model.names A dictionary mapping class IDs to class names.
reg_pts list of tuples [(20, 400), (1260, 400)] Points defining the counting region polygon. Defaults to a predefined rectangle.
line_thickness int 2 Thickness of the annotation lines.
track_thickness int 2 Thickness of the track lines.
view_img bool False Whether to display the image frames.
region_color tuple (255, 0, 255) Color of the counting region lines (BGR).
view_queue_counts bool True Whether to display the queue counts.
draw_tracks bool False Whether to draw tracks of the objects.
count_txt_color tuple (255, 255, 255) Color of the count text (BGR).
track_color tuple None Color of the tracks. If None, different colors will be used for different tracks.
region_thickness int 5 Thickness of the counting region lines.
fontsize float 0.7 Font size for the text annotations.

Arguments model.track

Name Type Default Description
source im0 None source directory for images or videos
persist bool False persisting tracks between frames
tracker str botsort.yaml Tracking method 'bytetrack' or 'botsort'
conf float 0.3 Confidence Threshold
iou float 0.5 IOU Threshold
classes list None filter results by class, i.e. classes=0, or classes=[0,2,3]
verbose bool True Display the object tracking results

FAQ

How can I use Ultralytics YOLOv8 for real-time queue management?

To use Ultralytics YOLOv8 for real-time queue management, you can follow these steps:

  1. Load the YOLOv8 model with YOLO("yolov8n.pt").
  2. Capture the video feed using cv2.VideoCapture.
  3. Define the region of interest (ROI) for queue management.
  4. Process frames to detect objects and manage queues.

Here's a minimal example:

import cv2

from ultralytics import YOLO, solutions

model = YOLO("yolov8n.pt")
cap = cv2.VideoCapture("path/to/video.mp4")
queue_region = [(20, 400), (1080, 404), (1080, 360), (20, 360)]

queue = solutions.QueueManager(
    names=model.names,
    reg_pts=queue_region,
    line_thickness=3,
    fontsize=1.0,
    region_color=(255, 144, 31),
)

while cap.isOpened():
    success, im0 = cap.read()
    if success:
        tracks = model.track(im0, show=False, persist=True, verbose=False)
        out = queue.process_queue(im0, tracks)
        cv2.imshow("Queue Management", im0)
        if cv2.waitKey(1) & 0xFF == ord("q"):
            break

cap.release()
cv2.destroyAllWindows()

Leveraging Ultralytics HUB can streamline this process by providing a user-friendly platform for deploying and managing your queue management solution.

What are the key advantages of using Ultralytics YOLOv8 for queue management?

Using Ultralytics YOLOv8 for queue management offers several benefits:

  • Plummeting Waiting Times: Efficiently organizes queues, reducing customer wait times and boosting satisfaction.
  • Enhancing Efficiency: Analyzes queue data to optimize staff deployment and operations, thereby reducing costs.
  • Real-time Alerts: Provides real-time notifications for long queues, enabling quick intervention.
  • Scalability: Easily scalable across different environments like retail, airports, and healthcare.

For more details, explore our Queue Management solutions.

Why should I choose Ultralytics YOLOv8 over competitors like TensorFlow or Detectron2 for queue management?

Ultralytics YOLOv8 has several advantages over TensorFlow and Detectron2 for queue management:

  • Real-time Performance: YOLOv8 is known for its real-time detection capabilities, offering faster processing speeds.
  • Ease of Use: Ultralytics provides a user-friendly experience, from training to deployment, via Ultralytics HUB.
  • Pretrained Models: Access to a range of pretrained models, minimizing the time needed for setup.
  • Community Support: Extensive documentation and active community support make problem-solving easier.

Learn how to get started with Ultralytics YOLO.

Can Ultralytics YOLOv8 handle multiple types of queues, such as in airports and retail?

Yes, Ultralytics YOLOv8 can manage various types of queues, including those in airports and retail environments. By configuring the QueueManager with specific regions and settings, YOLOv8 can adapt to different queue layouts and densities.

Example for airports:

queue_region_airport = [(50, 600), (1200, 600), (1200, 550), (50, 550)]
queue_airport = solutions.QueueManager(
    names=model.names,
    reg_pts=queue_region_airport,
    line_thickness=3,
    fontsize=1.0,
    region_color=(0, 255, 0),
)

For more information on diverse applications, check out our Real World Applications section.

What are some real-world applications of Ultralytics YOLOv8 in queue management?

Ultralytics YOLOv8 is used in various real-world applications for queue management:

  • Retail: Monitors checkout lines to reduce wait times and improve customer satisfaction.
  • Airports: Manages queues at ticket counters and security checkpoints for a smoother passenger experience.
  • Healthcare: Optimizes patient flow in clinics and hospitals.
  • Banks: Enhances customer service by managing queues efficiently in banks.

Check our blog on real-world queue management to learn more.


comments: true
description: Learn how to deploy Ultralytics YOLOv8 on Raspberry Pi with our comprehensive guide. Get performance benchmarks, setup instructions, and best practices.
keywords: Ultralytics, YOLOv8, Raspberry Pi, setup, guide, benchmarks, computer vision, object detection, NCNN, Docker, camera modules

Quick Start Guide: Raspberry Pi with Ultralytics YOLOv8

This comprehensive guide provides a detailed walkthrough for deploying Ultralytics YOLOv8 on Raspberry Pi devices. Additionally, it showcases performance benchmarks to demonstrate the capabilities of YOLOv8 on these small and powerful devices.



Watch: Raspberry Pi 5 updates and improvements.

!!! Note

This guide has been tested with Raspberry Pi 4 and Raspberry Pi 5 running the latest [Raspberry Pi OS Bookworm (Debian 12)](https://www.raspberrypi.com/software/operating-systems/). Using this guide for older Raspberry Pi devices such as the Raspberry Pi 3 is expected to work as long as the same Raspberry Pi OS Bookworm is installed.

What is Raspberry Pi?

Raspberry Pi is a small, affordable, single-board computer. It has become popular for a wide range of projects and applications, from hobbyist home automation to industrial uses. Raspberry Pi boards are capable of running a variety of operating systems, and they offer GPIO (General Purpose Input/Output) pins that allow for easy integration with sensors, actuators, and other hardware components. They come in different models with varying specifications, but they all share the same basic design philosophy of being low-cost, compact, and versatile.

Raspberry Pi Series Comparison

Raspberry Pi 3 Raspberry Pi 4 Raspberry Pi 5
CPU Broadcom BCM2837, Cortex-A53 64Bit SoC Broadcom BCM2711, Cortex-A72 64Bit SoC Broadcom BCM2712, Cortex-A76 64Bit SoC
CPU Max Frequency 1.4GHz 1.8GHz 2.4GHz
GPU Videocore IV Videocore VI VideoCore VII
GPU Max Frequency 400Mhz 500Mhz 800Mhz
Memory 1GB LPDDR2 SDRAM 1GB, 2GB, 4GB, 8GB LPDDR4-3200 SDRAM 4GB, 8GB LPDDR4X-4267 SDRAM
PCIe N/A N/A 1xPCIe 2.0 Interface
Max Power Draw 2.5A@5V 3A@5V 5A@5V (PD enabled)

What is Raspberry Pi OS?

Raspberry Pi OS (formerly known as Raspbian) is a Unix-like operating system based on the Debian GNU/Linux distribution for the Raspberry Pi family of compact single-board computers distributed by the Raspberry Pi Foundation. Raspberry Pi OS is highly optimized for the Raspberry Pi with ARM CPUs and uses a modified LXDE desktop environment with the Openbox stacking window manager. Raspberry Pi OS is under active development, with an emphasis on improving the stability and performance of as many Debian packages as possible on Raspberry Pi.

Flash Raspberry Pi OS to Raspberry Pi

The first thing to do after getting your hands on a Raspberry Pi is to flash a micro-SD card with Raspberry Pi OS, insert into the device and boot into the OS. Follow along with detailed Getting Started Documentation by Raspberry Pi to prepare your device for first use.

Set Up Ultralytics

There are two ways of setting up Ultralytics package on Raspberry Pi to build your next Computer Vision project. You can use either of them.

Start with Docker

The fastest way to get started with Ultralytics YOLOv8 on Raspberry Pi is to run with pre-built docker image for Raspberry Pi.

Execute the below command to pull the Docker container and run on Raspberry Pi. This is based on arm64v8/debian docker image which contains Debian 12 (Bookworm) in a Python3 environment.

t=ultralytics/ultralytics:latest-arm64 && sudo docker pull $t && sudo docker run -it --ipc=host $t

After this is done, skip to Use NCNN on Raspberry Pi section.

Start without Docker

Install Ultralytics Package

Here we will install Ultralytics package on the Raspberry Pi with optional dependencies so that we can export the PyTorch models to other different formats.

  1. Update packages list, install pip and upgrade to latest

    sudo apt update
    sudo apt install python3-pip -y
    pip install -U pip
    
  2. Install ultralytics pip package with optional dependencies

    pip install ultralytics[export]
    
  3. Reboot the device

    sudo reboot
    

Use NCNN on Raspberry Pi

Out of all the model export formats supported by Ultralytics, NCNN delivers the best inference performance when working with Raspberry Pi devices because NCNN is highly optimized for mobile/ embedded platforms (such as ARM architecture). Therefor our recommendation is to use NCNN with Raspberry Pi.

Convert Model to NCNN and Run Inference

The YOLOv8n model in PyTorch format is converted to NCNN to run inference with the exported model.

!!! Example

=== "Python"

    ```py
    from ultralytics import YOLO

    # Load a YOLOv8n PyTorch model
    model = YOLO("yolov8n.pt")

    # Export the model to NCNN format
    model.export(format="ncnn")  # creates 'yolov8n_ncnn_model'

    # Load the exported NCNN model
    ncnn_model = YOLO("yolov8n_ncnn_model")

    # Run inference
    results = ncnn_model("https://ultralytics.com/images/bus.jpg")
    ```

=== "CLI"

    ```py
    # Export a YOLOv8n PyTorch model to NCNN format
    yolo export model=yolov8n.pt format=ncnn  # creates 'yolov8n_ncnn_model'

    # Run inference with the exported model
    yolo predict model='yolov8n_ncnn_model' source='https://ultralytics.com/images/bus.jpg'
    ```

!!! Tip

For more details about supported export options, visit the [Ultralytics documentation page on deployment options](https://docs.ultralytics.com/guides/model-deployment-options).

Raspberry Pi 5 vs Raspberry Pi 4 YOLOv8 Benchmarks

YOLOv8 benchmarks were run by the Ultralytics team on nine different model formats measuring speed and accuracy: PyTorch, TorchScript, ONNX, OpenVINO, TF SavedModel, TF GraphDef, TF Lite, PaddlePaddle, NCNN. Benchmarks were run on both Raspberry Pi 5 and Raspberry Pi 4 at FP32 precision with default input image size of 640.

!!! Note

We have only included benchmarks for YOLOv8n and YOLOv8s models because other models sizes are too big to run on the Raspberry Pis and does not offer decent performance.

Comparison Chart

!!! tip "Performance"

=== "YOLOv8n"

    <div style="text-align: center;">
        <img width="800" src="https://github.com/ultralytics/ultralytics/assets/20147381/43421a4e-0ac0-42ca-995b-5e71d9748af5" alt="NVIDIA Jetson Ecosystem">
    </div>

=== "YOLOv8s"

    <div style="text-align: center;">
        <img width="800" src="https://github.com/ultralytics/ultralytics/assets/20147381/e85e18a2-abfc-431d-8b23-812820ee390e" alt="NVIDIA Jetson Ecosystem">
    </div>

Detailed Comparison Table

The below table represents the benchmark results for two different models (YOLOv8n, YOLOv8s) across nine different formats (PyTorch, TorchScript, ONNX, OpenVINO, TF SavedModel, TF GraphDef, TF Lite, PaddlePaddle, NCNN), running on both Raspberry Pi 4 and Raspberry Pi 5, giving us the status, size, mAP50-95(B) metric, and inference time for each combination.

!!! tip "Performance"

=== "YOLOv8n on RPi5"

    | Format        | Status | Size on disk (MB) | mAP50-95(B) | Inference time (ms/im) |
    |---------------|--------|-------------------|-------------|------------------------|
    | PyTorch       | ✅      | 6.2               | 0.6381      | 508.61                 |
    | TorchScript   | ✅      | 12.4              | 0.6092      | 558.38                 |
    | ONNX          | ✅      | 12.2              | 0.6092      | 198.69                 |
    | OpenVINO      | ✅      | 12.3              | 0.6092      | 704.70                 |
    | TF SavedModel | ✅      | 30.6              | 0.6092      | 367.64                 |
    | TF GraphDef   | ✅      | 12.3              | 0.6092      | 473.22                 |
    | TF Lite       | ✅      | 12.3              | 0.6092      | 380.67                 |
    | PaddlePaddle  | ✅      | 24.4              | 0.6092      | 703.51                 |
    | NCNN          | ✅      | 12.2              | 0.6034      | 94.28                  |

=== "YOLOv8s on RPi5"

    | Format        | Status | Size on disk (MB) | mAP50-95(B) | Inference time (ms/im) |
    |---------------|--------|-------------------|-------------|------------------------|
    | PyTorch       | ✅      | 21.5              | 0.6967      | 969.49                 |
    | TorchScript   | ✅      | 43.0              | 0.7136      | 1110.04                |
    | ONNX          | ✅      | 42.8              | 0.7136      | 451.37                 |
    | OpenVINO      | ✅      | 42.9              | 0.7136      | 873.51                 |
    | TF SavedModel | ✅      | 107.0             | 0.7136      | 658.15                 |
    | TF GraphDef   | ✅      | 42.8              | 0.7136      | 946.01                 |
    | TF Lite       | ✅      | 42.8              | 0.7136      | 1013.27                |
    | PaddlePaddle  | ✅      | 85.5              | 0.7136      | 1560.23                |
    | NCNN          | ✅      | 42.7              | 0.7204      | 211.26                 |

=== "YOLOv8n on RPi4"

    | Format        | Status | Size on disk (MB) | mAP50-95(B) | Inference time (ms/im) |
    |---------------|--------|-------------------|-------------|------------------------|
    | PyTorch       | ✅      | 6.2               | 0.6381      | 1068.42                |
    | TorchScript   | ✅      | 12.4              | 0.6092      | 1248.01                |
    | ONNX          | ✅      | 12.2              | 0.6092      | 560.04                 |
    | OpenVINO      | ✅      | 12.3              | 0.6092      | 534.93                 |
    | TF SavedModel | ✅      | 30.6              | 0.6092      | 816.50                 |
    | TF GraphDef   | ✅      | 12.3              | 0.6092      | 1007.57                |
    | TF Lite       | ✅      | 12.3              | 0.6092      | 950.29                 |
    | PaddlePaddle  | ✅      | 24.4              | 0.6092      | 1507.75                |
    | NCNN          | ✅      | 12.2              | 0.6092      | 414.73                 |

=== "YOLOv8s on RPi4"

    | Format        | Status | Size on disk (MB) | mAP50-95(B) | Inference time (ms/im) |
    |---------------|--------|-------------------|-------------|------------------------|
    | PyTorch       | ✅      | 21.5              | 0.6967      | 2589.58                |
    | TorchScript   | ✅      | 43.0              | 0.7136      | 2901.33                |
    | ONNX          | ✅      | 42.8              | 0.7136      | 1436.33                |
    | OpenVINO      | ✅      | 42.9              | 0.7136      | 1225.19                |
    | TF SavedModel | ✅      | 107.0             | 0.7136      | 1770.95                |
    | TF GraphDef   | ✅      | 42.8              | 0.7136      | 2146.66                |
    | TF Lite       | ✅      | 42.8              | 0.7136      | 2945.03                |
    | PaddlePaddle  | ✅      | 85.5              | 0.7136      | 3962.62                |
    | NCNN          | ✅      | 42.7              | 0.7136      | 1042.39                |

Reproduce Our Results

To reproduce the above Ultralytics benchmarks on all export formats, run this code:

!!! Example

=== "Python"

    ```py
    from ultralytics import YOLO

    # Load a YOLOv8n PyTorch model
    model = YOLO("yolov8n.pt")

    # Benchmark YOLOv8n speed and accuracy on the COCO8 dataset for all all export formats
    results = model.benchmarks(data="coco8.yaml", imgsz=640)
    ```

=== "CLI"

    ```py
    # Benchmark YOLOv8n speed and accuracy on the COCO8 dataset for all all export formats
    yolo benchmark model=yolov8n.pt data=coco8.yaml imgsz=640
    ```

Note that benchmarking results might vary based on the exact hardware and software configuration of a system, as well as the current workload of the system at the time the benchmarks are run. For the most reliable results use a dataset with a large number of images, i.e. `data='coco8.yaml' (4 val images), or `data='coco.yaml'` (5000 val images).

Use Raspberry Pi Camera

When using Raspberry Pi for Computer Vision projects, it can be essentially to grab real-time video feeds to perform inference. The onboard MIPI CSI connector on the Raspberry Pi allows you to connect official Raspberry PI camera modules. In this guide, we have used a Raspberry Pi Camera Module 3 to grab the video feeds and perform inference using YOLOv8 models.

!!! Tip

Learn more about the [different camera modules offered by Raspberry Pi](https://www.raspberrypi.com/documentation/accessories/camera.html) and also [how to get started with the Raspberry Pi camera modules](https://www.raspberrypi.com/documentation/computers/camera_software.html#introducing-the-raspberry-pi-cameras).

!!! Note

Raspberry Pi 5 uses smaller CSI connectors than the Raspberry Pi 4 (15-pin vs 22-pin), so you will need a [15-pin to 22pin adapter cable](https://www.raspberrypi.com/products/camera-cable) to connect to a Raspberry Pi Camera.

Test the Camera

Execute the following command after connecting the camera to the Raspberry Pi. You should see a live video feed from the camera for about 5 seconds.

rpicam-hello

!!! Tip

Learn more about [`rpicam-hello` usage on official Raspberry Pi documentation](https://www.raspberrypi.com/documentation/computers/camera_software.html#rpicam-hello)

Inference with Camera

There are 2 methods of using the Raspberry Pi Camera to inference YOLOv8 models.

!!! Usage

=== "Method 1"

    We can use `picamera2`which comes pre-installed with Raspberry Pi OS to access the camera and inference YOLOv8 models.

    !!! Example

        === "Python"

            ```py
            import cv2
            from picamera2 import Picamera2

            from ultralytics import YOLO

            # Initialize the Picamera2
            picam2 = Picamera2()
            picam2.preview_configuration.main.size = (1280, 720)
            picam2.preview_configuration.main.format = "RGB888"
            picam2.preview_configuration.align()
            picam2.configure("preview")
            picam2.start()

            # Load the YOLOv8 model
            model = YOLO("yolov8n.pt")

            while True:
                # Capture frame-by-frame
                frame = picam2.capture_array()

                # Run YOLOv8 inference on the frame
                results = model(frame)

                # Visualize the results on the frame
                annotated_frame = results[0].plot()

                # Display the resulting frame
                cv2.imshow("Camera", annotated_frame)

                # Break the loop if 'q' is pressed
                if cv2.waitKey(1) == ord("q"):
                    break

            # Release resources and close windows
            cv2.destroyAllWindows()
            ```

=== "Method 2"

    We need to initiate a TCP stream with `rpicam-vid` from the connected camera so that we can use this stream URL as an input when we are inferencing later. Execute the following command to start the TCP stream.

    ```py
    rpicam-vid -n -t 0 --inline --listen -o tcp://127.0.0.1:8888
    ```

    Learn more about [`rpicam-vid` usage on official Raspberry Pi documentation](https://www.raspberrypi.com/documentation/computers/camera_software.html#rpicam-vid)

    !!! Example

        === "Python"

            ```py
            from ultralytics import YOLO

            # Load a YOLOv8n PyTorch model
            model = YOLO("yolov8n.pt")

            # Run inference
            results = model("tcp://127.0.0.1:8888")
            ```

        === "CLI"

            ```py
            yolo predict model=yolov8n.pt source="tcp://127.0.0.1:8888"
            ```

!!! Tip

Check our document on [Inference Sources](https://docs.ultralytics.com/modes/predict/#inference-sources) if you want to change the image/ video input type

Best Practices when using Raspberry Pi

There are a couple of best practices to follow in order to enable maximum performance on Raspberry Pis running YOLOv8.

  1. Use an SSD

    When using Raspberry Pi for 24x7 continued usage, it is recommended to use an SSD for the system because an SD card will not be able to withstand continuous writes and might get broken. With the onboard PCIe connector on the Raspberry Pi 5, now you can connect SSDs using an adapter such as the NVMe Base for Raspberry Pi 5.

  2. Flash without GUI

    When flashing Raspberry Pi OS, you can choose to not install the Desktop environment (Raspberry Pi OS Lite) and this can save a bit of RAM on the device, leaving more space for computer vision processing.

Next Steps

Congratulations on successfully setting up YOLO on your Raspberry Pi! For further learning and support, visit Ultralytics YOLOv8 Docs and Kashmir World Foundation.

Acknowledgements and Citations

This guide was initially created by Daan Eeltink for Kashmir World Foundation, an organization dedicated to the use of YOLO for the conservation of endangered species. We acknowledge their pioneering work and educational focus in the realm of object detection technologies.

For more information about Kashmir World Foundation's activities, you can visit their website.

FAQ

How do I set up Ultralytics YOLOv8 on a Raspberry Pi without using Docker?

To set up Ultralytics YOLOv8 on a Raspberry Pi without Docker, follow these steps:

  1. Update the package list and install pip:
    sudo apt update
    sudo apt install python3-pip -y
    pip install -U pip
    
  2. Install the Ultralytics package with optional dependencies:
    pip install ultralytics[export]
    
  3. Reboot the device to apply changes:
    sudo reboot
    

For detailed instructions, refer to the Start without Docker section.

Why should I use Ultralytics YOLOv8's NCNN format on Raspberry Pi for AI tasks?

Ultralytics YOLOv8's NCNN format is highly optimized for mobile and embedded platforms, making it ideal for running AI tasks on Raspberry Pi devices. NCNN maximizes inference performance by leveraging ARM architecture, providing faster and more efficient processing compared to other formats. For more details on supported export options, visit the Ultralytics documentation page on deployment options.

How can I convert a YOLOv8 model to NCNN format for use on Raspberry Pi?

You can convert a PyTorch YOLOv8 model to NCNN format using either Python or CLI commands:

!!! Example

=== "Python"

    ```py
    from ultralytics import YOLO

    # Load a YOLOv8n PyTorch model
    model = YOLO("yolov8n.pt")

    # Export the model to NCNN format
    model.export(format="ncnn")  # creates 'yolov8n_ncnn_model'

    # Load the exported NCNN model
    ncnn_model = YOLO("yolov8n_ncnn_model")

    # Run inference
    results = ncnn_model("https://ultralytics.com/images/bus.jpg")
    ```

=== "CLI"

    ```py
    # Export a YOLOv8n PyTorch model to NCNN format
    yolo export model=yolov8n.pt format=ncnn  # creates 'yolov8n_ncnn_model'

    # Run inference with the exported model
    yolo predict model='yolov8n_ncnn_model' source='https://ultralytics.com/images/bus.jpg'
    ```

For more details, see the Use NCNN on Raspberry Pi section.

What are the hardware differences between Raspberry Pi 4 and Raspberry Pi 5 relevant to running YOLOv8?

Key differences include:

  • CPU: Raspberry Pi 4 uses Broadcom BCM2711, Cortex-A72 64-bit SoC, while Raspberry Pi 5 uses Broadcom BCM2712, Cortex-A76 64-bit SoC.
  • Max CPU Frequency: Raspberry Pi 4 has a max frequency of 1.8GHz, whereas Raspberry Pi 5 reaches 2.4GHz.
  • Memory: Raspberry Pi 4 offers up to 8GB of LPDDR4-3200 SDRAM, while Raspberry Pi 5 features LPDDR4X-4267 SDRAM, available in 4GB and 8GB variants.

These enhancements contribute to better performance benchmarks for YOLOv8 models on Raspberry Pi 5 compared to Raspberry Pi 4. Refer to the Raspberry Pi Series Comparison table for more details.

How can I set up a Raspberry Pi Camera Module to work with Ultralytics YOLOv8?

There are two methods to set up a Raspberry Pi Camera for YOLOv8 inference:

  1. Using picamera2:

    import cv2
    from picamera2 import Picamera2
    
    from ultralytics import YOLO
    
    picam2 = Picamera2()
    picam2.preview_configuration.main.size = (1280, 720)
    picam2.preview_configuration.main.format = "RGB888"
    picam2.preview_configuration.align()
    picam2.configure("preview")
    picam2.start()
    
    model = YOLO("yolov8n.pt")
    
    while True:
        frame = picam2.capture_array()
        results = model(frame)
        annotated_frame = results[0].plot()
        cv2.imshow("Camera", annotated_frame)
    
        if cv2.waitKey(1) == ord("q"):
            break
    
    cv2.destroyAllWindows()
    
  2. Using a TCP Stream:

    rpicam-vid -n -t 0 --inline --listen -o tcp://127.0.0.1:8888
    
    from ultralytics import YOLO
    
    model = YOLO("yolov8n.pt")
    results = model("tcp://127.0.0.1:8888")
    

For detailed setup instructions, visit the Inference with Camera section.


comments: true
description: Learn how to use Ultralytics YOLOv8 for precise object counting in specified regions, enhancing efficiency across various applications.
keywords: object counting, regions, YOLOv8, computer vision, Ultralytics, efficiency, accuracy, automation, real-time, applications, surveillance, monitoring

Object Counting in Different Regions using Ultralytics YOLOv8 🚀

What is Object Counting in Regions?

Object counting in regions with Ultralytics YOLOv8 involves precisely determining the number of objects within specified areas using advanced computer vision. This approach is valuable for optimizing processes, enhancing security, and improving efficiency in various applications.



Watch: Ultralytics YOLOv8 Object Counting in Multiple & Movable Regions

Advantages of Object Counting in Regions?

  • Precision and Accuracy: Object counting in regions with advanced computer vision ensures precise and accurate counts, minimizing errors often associated with manual counting.
  • Efficiency Improvement: Automated object counting enhances operational efficiency, providing real-time results and streamlining processes across different applications.
  • Versatility and Application: The versatility of object counting in regions makes it applicable across various domains, from manufacturing and surveillance to traffic monitoring, contributing to its widespread utility and effectiveness.

Real World Applications

Retail Market Streets
People Counting in Different Region using Ultralytics YOLOv8 Crowd Counting in Different Region using Ultralytics YOLOv8
People Counting in Different Region using Ultralytics YOLOv8 Crowd Counting in Different Region using Ultralytics YOLOv8

Steps to Run

Step 1: Install Required Libraries

Begin by cloning the Ultralytics repository, installing dependencies, and navigating to the local directory using the provided commands in Step 2.

# Clone Ultralytics repo
git clone https://github.com/ultralytics/ultralytics

# Navigate to the local directory
cd ultralytics/examples/YOLOv8-Region-Counter

Step 2: Run Region Counting Using Ultralytics YOLOv8

Execute the following basic commands for inference.

???+ tip "Region is Movable"

During video playback, you can interactively move the region within the video by clicking and dragging using the left mouse button.
# Save results
python yolov8_region_counter.py --source "path/to/video.mp4" --save-img

# Run model on CPU
python yolov8_region_counter.py --source "path/to/video.mp4" --device cpu

# Change model file
python yolov8_region_counter.py --source "path/to/video.mp4" --weights "path/to/model.pt"

# Detect specific classes (e.g., first and third classes)
python yolov8_region_counter.py --source "path/to/video.mp4" --classes 0 2

# View results without saving
python yolov8_region_counter.py --source "path/to/video.mp4" --view-img

Optional Arguments

Name Type Default Description
--source str None Path to video file, for webcam 0
--line_thickness int 2 Bounding Box thickness
--save-img bool False Save the predicted video/image
--weights str yolov8n.pt Weights file path
--classes list None Detect specific classes i.e. --classes 0 2
--region-thickness int 2 Region Box thickness
--track-thickness int 2 Tracking line thickness

FAQ

What is object counting in specified regions using Ultralytics YOLOv8?

Object counting in specified regions with Ultralytics YOLOv8 involves detecting and tallying the number of objects within defined areas using advanced computer vision. This precise method enhances efficiency and accuracy across various applications like manufacturing, surveillance, and traffic monitoring.

How do I run the object counting script with Ultralytics YOLOv8?

Follow these steps to run object counting in Ultralytics YOLOv8:

  1. Clone the Ultralytics repository and navigate to the directory:

    git clone https://github.com/ultralytics/ultralytics
    cd ultralytics/examples/YOLOv8-Region-Counter
    
  2. Execute the region counting script:

    python yolov8_region_counter.py --source "path/to/video.mp4" --save-img
    

For more options, visit the Run Region Counting section.

Why should I use Ultralytics YOLOv8 for object counting in regions?

Using Ultralytics YOLOv8 for object counting in regions offers several advantages:

  • Precision and Accuracy: Minimizes errors often seen in manual counting.
  • Efficiency Improvement: Provides real-time results and streamlines processes.
  • Versatility and Application: Applies to various domains, enhancing its utility.

Explore deeper benefits in the Advantages section.

Can the defined regions be adjusted during video playback?

Yes, with Ultralytics YOLOv8, regions can be interactively moved during video playback. Simply click and drag with the left mouse button to reposition the region. This feature enhances flexibility for dynamic environments. Learn more in the tip section for movable regions.

What are some real-world applications of object counting in regions?

Object counting with Ultralytics YOLOv8 can be applied to numerous real-world scenarios:

  • Retail: Counting people for foot traffic analysis.
  • Market Streets: Crowd density management.

Explore more examples in the Real World Applications section.


comments: true
description: todo
keywords: Ultralytics, YOLO, object detection, deep learning, machine learning, guide, ROS, Robot Operating System, robotics, ROS Noetic, Python, Ubuntu, simulation, visualization, communication, middleware, hardware abstraction, tools, utilities, ecosystem, Noetic Ninjemys, autonomous vehicle, AMV

ROS (Robot Operating System) quickstart guide

ROS Introduction (captioned) from Open Robotics on Vimeo.

What is ROS?

The Robot Operating System (ROS) is an open-source framework widely used in robotics research and industry. ROS provides a collection of libraries and tools to help developers create robot applications. ROS is designed to work with various robotic platforms, making it a flexible and powerful tool for roboticists.

Key Features of ROS

  1. Modular Architecture: ROS has a modular architecture, allowing developers to build complex systems by combining smaller, reusable components called nodes. Each node typically performs a specific function, and nodes communicate with each other using messages over topics or services.

  2. Communication Middleware: ROS offers a robust communication infrastructure that supports inter-process communication and distributed computing. This is achieved through a publish-subscribe model for data streams (topics) and a request-reply model for service calls.

  3. Hardware Abstraction: ROS provides a layer of abstraction over the hardware, enabling developers to write device-agnostic code. This allows the same code to be used with different hardware setups, facilitating easier integration and experimentation.

  4. Tools and Utilities: ROS comes with a rich set of tools and utilities for visualization, debugging, and simulation. For instance, RViz is used for visualizing sensor data and robot state information, while Gazebo provides a powerful simulation environment for testing algorithms and robot designs.

  5. Extensive Ecosystem: The ROS ecosystem is vast and continually growing, with numerous packages available for different robotic applications, including navigation, manipulation, perception, and more. The community actively contributes to the development and maintenance of these packages.

???+ note "Evolution of ROS Versions"

Since its development in 2007, ROS has evolved through [multiple versions](https://wiki.ros.org/Distributions), each introducing new features and improvements to meet the growing needs of the robotics community. The development of ROS can be categorized into two main series: ROS 1 and ROS 2. This guide focuses on the Long Term Support (LTS) version of ROS 1, known as ROS Noetic Ninjemys, the code should also work with earlier versions.

### ROS 1 vs. ROS 2

While ROS 1 provided a solid foundation for robotic development, ROS 2 addresses its shortcomings by offering:

- **Real-time Performance**: Improved support for real-time systems and deterministic behavior.
- **Security**: Enhanced security features for safe and reliable operation in various environments.
- **Scalability**: Better support for multi-robot systems and large-scale deployments.
- **Cross-platform Support**: Expanded compatibility with various operating systems beyond Linux, including Windows and macOS.
- **Flexible Communication**: Use of DDS for more flexible and efficient inter-process communication.

ROS Messages and Topics

In ROS, communication between nodes is facilitated through messages and topics. A message is a data structure that defines the information exchanged between nodes, while a topic is a named channel over which messages are sent and received. Nodes can publish messages to a topic or subscribe to messages from a topic, enabling them to communicate with each other. This publish-subscribe model allows for asynchronous communication and decoupling between nodes. Each sensor or actuator in a robotic system typically publishes data to a topic, which can then be consumed by other nodes for processing or control. For the purpose of this guide, we will focus on Image, Depth and PointCloud messages and camera topics.

Setting Up Ultralytics YOLO with ROS

This guide has been tested using this ROS environment, which is a fork of the ROSbot ROS repository. This environment includes the Ultralytics YOLO package, a Docker container for easy setup, comprehensive ROS packages, and Gazebo worlds for rapid testing. It is designed to work with the Husarion ROSbot 2 PRO. The code examples provided will work in any ROS Noetic/Melodic environment, including both simulation and real-world.

Husarion ROSbot 2 PRO

Dependencies Installation

Apart from the ROS environment, you will need to install the following dependencies:

  • ROS Numpy package: This is required for fast conversion between ROS Image messages and numpy arrays.

    pip install ros_numpy
    
  • Ultralytics package:

    pip install ultralytics
    

Use Ultralytics with ROS sensor_msgs/Image

The sensor_msgs/Image message type is commonly used in ROS for representing image data. It contains fields for encoding, height, width, and pixel data, making it suitable for transmitting images captured by cameras or other sensors. Image messages are widely used in robotic applications for tasks such as visual perception, object detection, and navigation.

Detection and Segmentation in ROS Gazebo

Image Step-by-Step Usage

The following code snippet demonstrates how to use the Ultralytics YOLO package with ROS. In this example, we subscribe to a camera topic, process the incoming image using YOLO, and publish the detected objects to new topics for detection and segmentation.

First, import the necessary libraries and instantiate two models: one for segmentation and one for detection. Initialize a ROS node (with the name ultralytics) to enable communication with the ROS master. To ensure a stable connection, we include a brief pause, giving the node sufficient time to establish the connection before proceeding.

import time

import rospy

from ultralytics import YOLO

detection_model = YOLO("yolov8m.pt")
segmentation_model = YOLO("yolov8m-seg.pt")
rospy.init_node("ultralytics")
time.sleep(1)

Initialize two ROS topics: one for detection and one for segmentation. These topics will be used to publish the annotated images, making them accessible for further processing. The communication between nodes is facilitated using sensor_msgs/Image messages.

from sensor_msgs.msg import Image

det_image_pub = rospy.Publisher("/ultralytics/detection/image", Image, queue_size=5)
seg_image_pub = rospy.Publisher("/ultralytics/segmentation/image", Image, queue_size=5)

Finally, create a subscriber that listens to messages on the /camera/color/image_raw topic and calls a callback function for each new message. This callback function receives messages of type sensor_msgs/Image, converts them into a numpy array using ros_numpy, processes the images with the previously instantiated YOLO models, annotates the images, and then publishes them back to the respective topics: /ultralytics/detection/image for detection and /ultralytics/segmentation/image for segmentation.

import ros_numpy


def callback(data):
    """Callback function to process image and publish annotated images."""
    array = ros_numpy.numpify(data)
    if det_image_pub.get_num_connections():
        det_result = detection_model(array)
        det_annotated = det_result[0].plot(show=False)
        det_image_pub.publish(ros_numpy.msgify(Image, det_annotated, encoding="rgb8"))

    if seg_image_pub.get_num_connections():
        seg_result = segmentation_model(array)
        seg_annotated = seg_result[0].plot(show=False)
        seg_image_pub.publish(ros_numpy.msgify(Image, seg_annotated, encoding="rgb8"))


rospy.Subscriber("/camera/color/image_raw", Image, callback)

while True:
    rospy.spin()

??? Example "Complete code"

```py
import time

import ros_numpy
import rospy
from sensor_msgs.msg import Image

from ultralytics import YOLO

detection_model = YOLO("yolov8m.pt")
segmentation_model = YOLO("yolov8m-seg.pt")
rospy.init_node("ultralytics")
time.sleep(1)

det_image_pub = rospy.Publisher("/ultralytics/detection/image", Image, queue_size=5)
seg_image_pub = rospy.Publisher("/ultralytics/segmentation/image", Image, queue_size=5)


def callback(data):
    """Callback function to process image and publish annotated images."""
    array = ros_numpy.numpify(data)
    if det_image_pub.get_num_connections():
        det_result = detection_model(array)
        det_annotated = det_result[0].plot(show=False)
        det_image_pub.publish(ros_numpy.msgify(Image, det_annotated, encoding="rgb8"))

    if seg_image_pub.get_num_connections():
        seg_result = segmentation_model(array)
        seg_annotated = seg_result[0].plot(show=False)
        seg_image_pub.publish(ros_numpy.msgify(Image, seg_annotated, encoding="rgb8"))


rospy.Subscriber("/camera/color/image_raw", Image, callback)

while True:
    rospy.spin()
```

???+ tip "Debugging"

Debugging ROS (Robot Operating System) nodes can be challenging due to the system's distributed nature. Several tools can assist with this process:

1. `rostopic echo <TOPIC-NAME>` : This command allows you to view messages published on a specific topic, helping you inspect the data flow.
2. `rostopic list`: Use this command to list all available topics in the ROS system, giving you an overview of the active data streams.
3. `rqt_graph`: This visualization tool displays the communication graph between nodes, providing insights into how nodes are interconnected and how they interact.
4. For more complex visualizations, such as 3D representations, you can use [RViz](https://wiki.ros.org/rviz). RViz (ROS Visualization) is a powerful 3D visualization tool for ROS. It allows you to visualize the state of your robot and its environment in real-time. With RViz, you can view sensor data (e.g. `sensors_msgs/Image`), robot model states, and various other types of information, making it easier to debug and understand the behavior of your robotic system.

Publish Detected Classes with std_msgs/String

Standard ROS messages also include std_msgs/String messages. In many applications, it is not necessary to republish the entire annotated image; instead, only the classes present in the robot's view are needed. The following example demonstrates how to use std_msgs/String messages to republish the detected classes on the /ultralytics/detection/classes topic. These messages are more lightweight and provide essential information, making them valuable for various applications.

Example Use Case

Consider a warehouse robot equipped with a camera and object detection model. Instead of sending large annotated images over the network, the robot can publish a list of detected classes as std_msgs/String messages. For instance, when the robot detects objects like "box", "pallet" and "forklift" it publishes these classes to the /ultralytics/detection/classes topic. This information can then be used by a central monitoring system to track the inventory in real-time, optimize the robot's path planning to avoid obstacles, or trigger specific actions such as picking up a detected box. This approach reduces the bandwidth required for communication and focuses on transmitting critical data.

String Step-by-Step Usage

This example demonstrates how to use the Ultralytics YOLO package with ROS. In this example, we subscribe to a camera topic, process the incoming image using YOLO, and publish the detected objects to new topic /ultralytics/detection/classes using std_msgs/String messages. The ros_numpy package is used to convert the ROS Image message to a numpy array for processing with YOLO.

import time

import ros_numpy
import rospy
from sensor_msgs.msg import Image
from std_msgs.msg import String

from ultralytics import YOLO

detection_model = YOLO("yolov8m.pt")
rospy.init_node("ultralytics")
time.sleep(1)
classes_pub = rospy.Publisher("/ultralytics/detection/classes", String, queue_size=5)


def callback(data):
    """Callback function to process image and publish detected classes."""
    array = ros_numpy.numpify(data)
    if classes_pub.get_num_connections():
        det_result = detection_model(array)
        classes = det_result[0].boxes.cls.cpu().numpy().astype(int)
        names = [det_result[0].names[i] for i in classes]
        classes_pub.publish(String(data=str(names)))


rospy.Subscriber("/camera/color/image_raw", Image, callback)
while True:
    rospy.spin()

Use Ultralytics with ROS Depth Images

In addition to RGB images, ROS supports depth images, which provide information about the distance of objects from the camera. Depth images are crucial for robotic applications such as obstacle avoidance, 3D mapping, and localization.

A depth image is an image where each pixel represents the distance from the camera to an object. Unlike RGB images that capture color, depth images capture spatial information, enabling robots to perceive the 3D structure of their environment.

!!! tip "Obtaining Depth Images"

Depth images can be obtained using various sensors:

1. [Stereo Cameras](https://en.wikipedia.org/wiki/Stereo_camera): Use two cameras to calculate depth based on image disparity.
2. [Time-of-Flight (ToF) Cameras](https://en.wikipedia.org/wiki/Time-of-flight_camera): Measure the time light takes to return from an object.
3. [Structured Light Sensors](https://en.wikipedia.org/wiki/Structured-light_3D_scanner): Project a pattern and measure its deformation on surfaces.

Using YOLO with Depth Images

In ROS, depth images are represented by the sensor_msgs/Image message type, which includes fields for encoding, height, width, and pixel data. The encoding field for depth images often uses a format like "16UC1", indicating a 16-bit unsigned integer per pixel, where each value represents the distance to the object. Depth images are commonly used in conjunction with RGB images to provide a more comprehensive view of the environment.

Using YOLO, it is possible to extract and combine information from both RGB and depth images. For instance, YOLO can detect objects within an RGB image, and this detection can be used to pinpoint corresponding regions in the depth image. This allows for the extraction of precise depth information for detected objects, enhancing the robot's ability to understand its environment in three dimensions.

!!! warning "RGB-D Cameras"

When working with depth images, it is essential to ensure that the RGB and depth images are correctly aligned. RGB-D cameras, such as the [Intel RealSense](https://www.intelrealsense.com/) series, provide synchronized RGB and depth images, making it easier to combine information from both sources. If using separate RGB and depth cameras, it is crucial to calibrate them to ensure accurate alignment.

Depth Step-by-Step Usage

In this example, we use YOLO to segment an image and apply the extracted mask to segment the object in the depth image. This allows us to determine the distance of each pixel of the object of interest from the camera's focal center. By obtaining this distance information, we can calculate the distance between the camera and the specific object in the scene. Begin by importing the necessary libraries, creating a ROS node, and instantiating a segmentation model and a ROS topic.

import time

import rospy
from std_msgs.msg import String

from ultralytics import YOLO

rospy.init_node("ultralytics")
time.sleep(1)

segmentation_model = YOLO("yolov8m-seg.pt")

classes_pub = rospy.Publisher("/ultralytics/detection/distance", String, queue_size=5)

Next, define a callback function that processes the incoming depth image message. The function waits for the depth image and RGB image messages, converts them into numpy arrays, and applies the segmentation model to the RGB image. It then extracts the segmentation mask for each detected object and calculates the average distance of the object from the camera using the depth image. Most sensors have a maximum distance, known as the clip distance, beyond which values are represented as inf (np.inf). Before processing, it is important to filter out these null values and assign them a value of 0. Finally, it publishes the detected objects along with their average distances to the /ultralytics/detection/distance topic.

import numpy as np
import ros_numpy
from sensor_msgs.msg import Image


def callback(data):
    """Callback function to process depth image and RGB image."""
    image = rospy.wait_for_message("/camera/color/image_raw", Image)
    image = ros_numpy.numpify(image)
    depth = ros_numpy.numpify(data)
    result = segmentation_model(image)

    for index, cls in enumerate(result[0].boxes.cls):
        class_index = int(cls.cpu().numpy())
        name = result[0].names[class_index]
        mask = result[0].masks.data.cpu().numpy()[index, :, :].astype(int)
        obj = depth[mask == 1]
        obj = obj[~np.isnan(obj)]
        avg_distance = np.mean(obj) if len(obj) else np.inf

    classes_pub.publish(String(data=str(all_objects)))


rospy.Subscriber("/camera/depth/image_raw", Image, callback)

while True:
    rospy.spin()

??? Example "Complete code"

```py
import time

import numpy as np
import ros_numpy
import rospy
from sensor_msgs.msg import Image
from std_msgs.msg import String

from ultralytics import YOLO

rospy.init_node("ultralytics")
time.sleep(1)

segmentation_model = YOLO("yolov8m-seg.pt")

classes_pub = rospy.Publisher("/ultralytics/detection/distance", String, queue_size=5)


def callback(data):
    """Callback function to process depth image and RGB image."""
    image = rospy.wait_for_message("/camera/color/image_raw", Image)
    image = ros_numpy.numpify(image)
    depth = ros_numpy.numpify(data)
    result = segmentation_model(image)

    for index, cls in enumerate(result[0].boxes.cls):
        class_index = int(cls.cpu().numpy())
        name = result[0].names[class_index]
        mask = result[0].masks.data.cpu().numpy()[index, :, :].astype(int)
        obj = depth[mask == 1]
        obj = obj[~np.isnan(obj)]
        avg_distance = np.mean(obj) if len(obj) else np.inf

    classes_pub.publish(String(data=str(all_objects)))


rospy.Subscriber("/camera/depth/image_raw", Image, callback)

while True:
    rospy.spin()
```

Use Ultralytics with ROS sensor_msgs/PointCloud2

Detection and Segmentation in ROS Gazebo

The sensor_msgs/PointCloud2 message type is a data structure used in ROS to represent 3D point cloud data. This message type is integral to robotic applications, enabling tasks such as 3D mapping, object recognition, and localization.

A point cloud is a collection of data points defined within a three-dimensional coordinate system. These data points represent the external surface of an object or a scene, captured via 3D scanning technologies. Each point in the cloud has X, Y, and Z coordinates, which correspond to its position in space, and may also include additional information such as color and intensity.

!!! warning "Reference frame"

When working with `sensor_msgs/PointCloud2`, it's essential to consider the reference frame of the sensor from which the point cloud data was acquired. The point cloud is initially captured in the sensor's reference frame. You can determine this reference frame by listening to the `/tf_static` topic. However, depending on your specific application requirements, you might need to convert the point cloud into another reference frame. This transformation can be achieved using the `tf2_ros` package, which provides tools for managing coordinate frames and transforming data between them.

!!! tip "Obtaining Point clouds"

Point Clouds can be obtained using various sensors:

1. **LIDAR (Light Detection and Ranging)**: Uses laser pulses to measure distances to objects and create high-precision 3D maps.
2. **Depth Cameras**: Capture depth information for each pixel, allowing for 3D reconstruction of the scene.
3. **Stereo Cameras**: Utilize two or more cameras to obtain depth information through triangulation.
4. **Structured Light Scanners**: Project a known pattern onto a surface and measure the deformation to calculate depth.

Using YOLO with Point Clouds

To integrate YOLO with sensor_msgs/PointCloud2 type messages, we can employ a method similar to the one used for depth maps. By leveraging the color information embedded in the point cloud, we can extract a 2D image, perform segmentation on this image using YOLO, and then apply the resulting mask to the three-dimensional points to isolate the 3D object of interest.

For handling point clouds, we recommend using Open3D (pip install open3d), a user-friendly Python library. Open3D provides robust tools for managing point cloud data structures, visualizing them, and executing complex operations seamlessly. This library can significantly simplify the process and enhance our ability to manipulate and analyze point clouds in conjunction with YOLO-based segmentation.

Point Clouds Step-by-Step Usage

Import the necessary libraries and instantiate the YOLO model for segmentation.

import time

import rospy

from ultralytics import YOLO

rospy.init_node("ultralytics")
time.sleep(1)
segmentation_model = YOLO("yolov8m-seg.pt")

Create a function pointcloud2_to_array, which transforms a sensor_msgs/PointCloud2 message into two numpy arrays. The sensor_msgs/PointCloud2 messages contain n points based on the width and height of the acquired image. For instance, a 480 x 640 image will have 307,200 points. Each point includes three spatial coordinates (xyz) and the corresponding color in RGB format. These can be considered as two separate channels of information.

The function returns the xyz coordinates and RGB values in the format of the original camera resolution (width x height). Most sensors have a maximum distance, known as the clip distance, beyond which values are represented as inf (np.inf). Before processing, it is important to filter out these null values and assign them a value of 0.

import numpy as np
import ros_numpy


def pointcloud2_to_array(pointcloud2: PointCloud2) -> tuple:
    """
    Convert a ROS PointCloud2 message to a numpy array.

    Args:
        pointcloud2 (PointCloud2): the PointCloud2 message

    Returns:
        (tuple): tuple containing (xyz, rgb)
    """
    pc_array = ros_numpy.point_cloud2.pointcloud2_to_array(pointcloud2)
    split = ros_numpy.point_cloud2.split_rgb_field(pc_array)
    rgb = np.stack([split["b"], split["g"], split["r"]], axis=2)
    xyz = ros_numpy.point_cloud2.get_xyz_points(pc_array, remove_nans=False)
    xyz = np.array(xyz).reshape((pointcloud2.height, pointcloud2.width, 3))
    nan_rows = np.isnan(xyz).all(axis=2)
    xyz[nan_rows] = [0, 0, 0]
    rgb[nan_rows] = [0, 0, 0]
    return xyz, rgb

Next, subscribe to the /camera/depth/points topic to receive the point cloud message and convert the sensor_msgs/PointCloud2 message into numpy arrays containing the XYZ coordinates and RGB values (using the pointcloud2_to_array function). Process the RGB image using the YOLO model to extract segmented objects. For each detected object, extract the segmentation mask and apply it to both the RGB image and the XYZ coordinates to isolate the object in 3D space.

Processing the mask is straightforward since it consists of binary values, with 1 indicating the presence of the object and 0 indicating the absence. To apply the mask, simply multiply the original channels by the mask. This operation effectively isolates the object of interest within the image. Finally, create an Open3D point cloud object and visualize the segmented object in 3D space with associated colors.

import sys

import open3d as o3d

ros_cloud = rospy.wait_for_message("/camera/depth/points", PointCloud2)
xyz, rgb = pointcloud2_to_array(ros_cloud)
result = segmentation_model(rgb)

if not len(result[0].boxes.cls):
    print("No objects detected")
    sys.exit()

classes = result[0].boxes.cls.cpu().numpy().astype(int)
for index, class_id in enumerate(classes):
    mask = result[0].masks.data.cpu().numpy()[index, :, :].astype(int)
    mask_expanded = np.stack([mask, mask, mask], axis=2)

    obj_rgb = rgb * mask_expanded
    obj_xyz = xyz * mask_expanded

    pcd = o3d.geometry.PointCloud()
    pcd.points = o3d.utility.Vector3dVector(obj_xyz.reshape((ros_cloud.height * ros_cloud.width, 3)))
    pcd.colors = o3d.utility.Vector3dVector(obj_rgb.reshape((ros_cloud.height * ros_cloud.width, 3)) / 255)
    o3d.visualization.draw_geometries([pcd])

??? Example "Complete code"

```py
import sys
import time

import numpy as np
import open3d as o3d
import ros_numpy
import rospy

from ultralytics import YOLO

rospy.init_node("ultralytics")
time.sleep(1)
segmentation_model = YOLO("yolov8m-seg.pt")


def pointcloud2_to_array(pointcloud2: PointCloud2) -> tuple:
    """
    Convert a ROS PointCloud2 message to a numpy array.

    Args:
        pointcloud2 (PointCloud2): the PointCloud2 message

    Returns:
        (tuple): tuple containing (xyz, rgb)
    """
    pc_array = ros_numpy.point_cloud2.pointcloud2_to_array(pointcloud2)
    split = ros_numpy.point_cloud2.split_rgb_field(pc_array)
    rgb = np.stack([split["b"], split["g"], split["r"]], axis=2)
    xyz = ros_numpy.point_cloud2.get_xyz_points(pc_array, remove_nans=False)
    xyz = np.array(xyz).reshape((pointcloud2.height, pointcloud2.width, 3))
    nan_rows = np.isnan(xyz).all(axis=2)
    xyz[nan_rows] = [0, 0, 0]
    rgb[nan_rows] = [0, 0, 0]
    return xyz, rgb


ros_cloud = rospy.wait_for_message("/camera/depth/points", PointCloud2)
xyz, rgb = pointcloud2_to_array(ros_cloud)
result = segmentation_model(rgb)

if not len(result[0].boxes.cls):
    print("No objects detected")
    sys.exit()

classes = result[0].boxes.cls.cpu().numpy().astype(int)
for index, class_id in enumerate(classes):
    mask = result[0].masks.data.cpu().numpy()[index, :, :].astype(int)
    mask_expanded = np.stack([mask, mask, mask], axis=2)

    obj_rgb = rgb * mask_expanded
    obj_xyz = xyz * mask_expanded

    pcd = o3d.geometry.PointCloud()
    pcd.points = o3d.utility.Vector3dVector(obj_xyz.reshape((ros_cloud.height * ros_cloud.width, 3)))
    pcd.colors = o3d.utility.Vector3dVector(obj_rgb.reshape((ros_cloud.height * ros_cloud.width, 3)) / 255)
    o3d.visualization.draw_geometries([pcd])
```

Point Cloud Segmentation with Ultralytics

FAQ

What is the Robot Operating System (ROS)?

The Robot Operating System (ROS) is an open-source framework commonly used in robotics to help developers create robust robot applications. It provides a collection of libraries and tools for building and interfacing with robotic systems, enabling easier development of complex applications. ROS supports communication between nodes using messages over topics or services.

How do I integrate Ultralytics YOLO with ROS for real-time object detection?

Integrating Ultralytics YOLO with ROS involves setting up a ROS environment and using YOLO for processing sensor data. Begin by installing the required dependencies like ros_numpy and Ultralytics YOLO:

pip install ros_numpy ultralytics

Next, create a ROS node and subscribe to an image topic to process the incoming data. Here is a minimal example:

import ros_numpy
import rospy
from sensor_msgs.msg import Image

from ultralytics import YOLO

detection_model = YOLO("yolov8m.pt")
rospy.init_node("ultralytics")
det_image_pub = rospy.Publisher("/ultralytics/detection/image", Image, queue_size=5)


def callback(data):
    array = ros_numpy.numpify(data)
    det_result = detection_model(array)
    det_annotated = det_result[0].plot(show=False)
    det_image_pub.publish(ros_numpy.msgify(Image, det_annotated, encoding="rgb8"))


rospy.Subscriber("/camera/color/image_raw", Image, callback)
rospy.spin()

What are ROS topics and how are they used in Ultralytics YOLO?

ROS topics facilitate communication between nodes in a ROS network by using a publish-subscribe model. A topic is a named channel that nodes use to send and receive messages asynchronously. In the context of Ultralytics YOLO, you can make a node subscribe to an image topic, process the images using YOLO for tasks like detection or segmentation, and publish outcomes to new topics.

For example, subscribe to a camera topic and process the incoming image for detection:

rospy.Subscriber("/camera/color/image_raw", Image, callback)

Why use depth images with Ultralytics YOLO in ROS?

Depth images in ROS, represented by sensor_msgs/Image, provide the distance of objects from the camera, crucial for tasks like obstacle avoidance, 3D mapping, and localization. By using depth information along with RGB images, robots can better understand their 3D environment.

With YOLO, you can extract segmentation masks from RGB images and apply these masks to depth images to obtain precise 3D object information, improving the robot's ability to navigate and interact with its surroundings.

How can I visualize 3D point clouds with YOLO in ROS?

To visualize 3D point clouds in ROS with YOLO:

  1. Convert sensor_msgs/PointCloud2 messages to numpy arrays.
  2. Use YOLO to segment RGB images.
  3. Apply the segmentation mask to the point cloud.

Here's an example using Open3D for visualization:

import sys

import open3d as o3d
import ros_numpy
import rospy
from sensor_msgs.msg import PointCloud2

from ultralytics import YOLO

rospy.init_node("ultralytics")
segmentation_model = YOLO("yolov8m-seg.pt")


def pointcloud2_to_array(pointcloud2):
    pc_array = ros_numpy.point_cloud2.pointcloud2_to_array(pointcloud2)
    split = ros_numpy.point_cloud2.split_rgb_field(pc_array)
    rgb = np.stack([split["b"], split["g"], split["r"]], axis=2)
    xyz = ros_numpy.point_cloud2.get_xyz_points(pc_array, remove_nans=False)
    xyz = np.array(xyz).reshape((pointcloud2.height, pointcloud2.width, 3))
    return xyz, rgb


ros_cloud = rospy.wait_for_message("/camera/depth/points", PointCloud2)
xyz, rgb = pointcloud2_to_array(ros_cloud)
result = segmentation_model(rgb)

if not len(result[0].boxes.cls):
    print("No objects detected")
    sys.exit()

classes = result[0].boxes.cls.cpu().numpy().astype(int)
for index, class_id in enumerate(classes):
    mask = result[0].masks.data.cpu().numpy()[index, :, :].astype(int)
    mask_expanded = np.stack([mask, mask, mask], axis=2)

    obj_rgb = rgb * mask_expanded
    obj_xyz = xyz * mask_expanded

    pcd = o3d.geometry.PointCloud()
    pcd.points = o3d.utility.Vector3dVector(obj_xyz.reshape((-1, 3)))
    pcd.colors = o3d.utility.Vector3dVector(obj_rgb.reshape((-1, 3)) / 255)
    o3d.visualization.draw_geometries([pcd])

This approach provides a 3D visualization of segmented objects, useful for tasks like navigation and manipulation.


comments: true
description: Learn how to implement YOLOv8 with SAHI for sliced inference. Optimize memory usage and enhance detection accuracy for large-scale applications.
keywords: YOLOv8, SAHI, Sliced Inference, Object Detection, Ultralytics, High-resolution Images, Computational Efficiency, Integration Guide

Ultralytics Docs: Using YOLOv8 with SAHI for Sliced Inference

Welcome to the Ultralytics documentation on how to use YOLOv8 with SAHI (Slicing Aided Hyper Inference). This comprehensive guide aims to furnish you with all the essential knowledge you'll need to implement SAHI alongside YOLOv8. We'll deep-dive into what SAHI is, why sliced inference is critical for large-scale applications, and how to integrate these functionalities with YOLOv8 for enhanced object detection performance.

SAHI Sliced Inference Overview

Introduction to SAHI

SAHI (Slicing Aided Hyper Inference) is an innovative library designed to optimize object detection algorithms for large-scale and high-resolution imagery. Its core functionality lies in partitioning images into manageable slices, running object detection on each slice, and then stitching the results back together. SAHI is compatible with a range of object detection models, including the YOLO series, thereby offering flexibility while ensuring optimized use of computational resources.



Watch: Inference with SAHI (Slicing Aided Hyper Inference) using Ultralytics YOLOv8

Key Features of SAHI

  • Seamless Integration: SAHI integrates effortlessly with YOLO models, meaning you can start slicing and detecting without a lot of code modification.
  • Resource Efficiency: By breaking down large images into smaller parts, SAHI optimizes the memory usage, allowing you to run high-quality detection on hardware with limited resources.
  • High Accuracy: SAHI maintains the detection accuracy by employing smart algorithms to merge overlapping detection boxes during the stitching process.

What is Sliced Inference?

Sliced Inference refers to the practice of subdividing a large or high-resolution image into smaller segments (slices), conducting object detection on these slices, and then recompiling the slices to reconstruct the object locations on the original image. This technique is invaluable in scenarios where computational resources are limited or when working with extremely high-resolution images that could otherwise lead to memory issues.

Benefits of Sliced Inference

  • Reduced Computational Burden: Smaller image slices are faster to process, and they consume less memory, enabling smoother operation on lower-end hardware.

  • Preserved Detection Quality: Since each slice is treated independently, there is no reduction in the quality of object detection, provided the slices are large enough to capture the objects of interest.

  • Enhanced Scalability: The technique allows for object detection to be more easily scaled across different sizes and resolutions of images, making it ideal for a wide range of applications from satellite imagery to medical diagnostics.

YOLOv8 without SAHI YOLOv8 with SAHI
YOLOv8 without SAHI YOLOv8 with SAHI

Installation and Preparation

Installation

To get started, install the latest versions of SAHI and Ultralytics:

pip install -U ultralytics sahi

Import Modules and Download Resources

Here's how to import the necessary modules and download a YOLOv8 model and some test images:

from sahi.utils.file import download_from_url
from sahi.utils.yolov8 import download_yolov8s_model

# Download YOLOv8 model
yolov8_model_path = "models/yolov8s.pt"
download_yolov8s_model(yolov8_model_path)

# Download test images
download_from_url(
    "https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg",
    "demo_data/small-vehicles1.jpeg",
)
download_from_url(
    "https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/terrain2.png",
    "demo_data/terrain2.png",
)

Standard Inference with YOLOv8

Instantiate the Model

You can instantiate a YOLOv8 model for object detection like this:

from sahi import AutoDetectionModel

detection_model = AutoDetectionModel.from_pretrained(
    model_type="yolov8",
    model_path=yolov8_model_path,
    confidence_threshold=0.3,
    device="cpu",  # or 'cuda:0'
)

Perform Standard Prediction

Perform standard inference using an image path or a numpy image.

from sahi.predict import get_prediction

# With an image path
result = get_prediction("demo_data/small-vehicles1.jpeg", detection_model)

# With a numpy image
result = get_prediction(read_image("demo_data/small-vehicles1.jpeg"), detection_model)

Visualize Results

Export and visualize the predicted bounding boxes and masks:

result.export_visuals(export_dir="demo_data/")
Image("demo_data/prediction_visual.png")

Sliced Inference with YOLOv8

Perform sliced inference by specifying the slice dimensions and overlap ratios:

from sahi.predict import get_sliced_prediction

result = get_sliced_prediction(
    "demo_data/small-vehicles1.jpeg",
    detection_model,
    slice_height=256,
    slice_width=256,
    overlap_height_ratio=0.2,
    overlap_width_ratio=0.2,
)

Handling Prediction Results

SAHI provides a PredictionResult object, which can be converted into various annotation formats:

# Access the object prediction list
object_prediction_list = result.object_prediction_list

# Convert to COCO annotation, COCO prediction, imantics, and fiftyone formats
result.to_coco_annotations()[:3]
result.to_coco_predictions(image_id=1)[:3]
result.to_imantics_annotations()[:3]
result.to_fiftyone_detections()[:3]

Batch Prediction

For batch prediction on a directory of images:

from sahi.predict import predict

predict(
    model_type="yolov8",
    model_path="path/to/yolov8n.pt",
    model_device="cpu",  # or 'cuda:0'
    model_confidence_threshold=0.4,
    source="path/to/dir",
    slice_height=256,
    slice_width=256,
    overlap_height_ratio=0.2,
    overlap_width_ratio=0.2,
)

That's it! Now you're equipped to use YOLOv8 with SAHI for both standard and sliced inference.

Citations and Acknowledgments

If you use SAHI in your research or development work, please cite the original SAHI paper and acknowledge the authors:

!!! Quote ""

=== "BibTeX"

    ```py
    @article{akyon2022sahi,
      title={Slicing Aided Hyper Inference and Fine-tuning for Small Object Detection},
      author={Akyon, Fatih Cagatay and Altinuc, Sinan Onur and Temizel, Alptekin},
      journal={2022 IEEE International Conference on Image Processing (ICIP)},
      doi={10.1109/ICIP46576.2022.9897990},
      pages={966-970},
      year={2022}
    }
    ```

We extend our thanks to the SAHI research group for creating and maintaining this invaluable resource for the computer vision community. For more information about SAHI and its creators, visit the SAHI GitHub repository.

FAQ

How can I integrate YOLOv8 with SAHI for sliced inference in object detection?

Integrating Ultralytics YOLOv8 with SAHI (Slicing Aided Hyper Inference) for sliced inference optimizes your object detection tasks on high-resolution images by partitioning them into manageable slices. This approach improves memory usage and ensures high detection accuracy. To get started, you need to install the ultralytics and sahi libraries:

pip install -U ultralytics sahi

Then, download a YOLOv8 model and test images:

from sahi.utils.file import download_from_url
from sahi.utils.yolov8 import download_yolov8s_model

# Download YOLOv8 model
yolov8_model_path = "models/yolov8s.pt"
download_yolov8s_model(yolov8_model_path)

# Download test images
download_from_url(
    "https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg",
    "demo_data/small-vehicles1.jpeg",
)

For more detailed instructions, refer to our Sliced Inference guide.

Why should I use SAHI with YOLOv8 for object detection on large images?

Using SAHI with Ultralytics YOLOv8 for object detection on large images offers several benefits:

  • Reduced Computational Burden: Smaller slices are faster to process and consume less memory, making it feasible to run high-quality detections on hardware with limited resources.
  • Maintained Detection Accuracy: SAHI uses intelligent algorithms to merge overlapping boxes, preserving the detection quality.
  • Enhanced Scalability: By scaling object detection tasks across different image sizes and resolutions, SAHI becomes ideal for various applications, such as satellite imagery analysis and medical diagnostics.

Learn more about the benefits of sliced inference in our documentation.

Can I visualize prediction results when using YOLOv8 with SAHI?

Yes, you can visualize prediction results when using YOLOv8 with SAHI. Here's how you can export and visualize the results:

result.export_visuals(export_dir="demo_data/")
from IPython.display import Image

Image("demo_data/prediction_visual.png")

This command will save the visualized predictions to the specified directory and you can then load the image to view it in your notebook or application. For a detailed guide, check out the Standard Inference section.

What features does SAHI offer for improving YOLOv8 object detection?

SAHI (Slicing Aided Hyper Inference) offers several features that complement Ultralytics YOLOv8 for object detection:

  • Seamless Integration: SAHI easily integrates with YOLO models, requiring minimal code adjustments.
  • Resource Efficiency: It partitions large images into smaller slices, which optimizes memory usage and speed.
  • High Accuracy: By effectively merging overlapping detection boxes during the stitching process, SAHI maintains high detection accuracy.

For a deeper understanding, read about SAHI's key features.

How do I handle large-scale inference projects using YOLOv8 and SAHI?

To handle large-scale inference projects using YOLOv8 and SAHI, follow these best practices:

  1. Install Required Libraries: Ensure that you have the latest versions of ultralytics and sahi.
  2. Configure Sliced Inference: Determine the optimal slice dimensions and overlap ratios for your specific project.
  3. Run Batch Predictions: Use SAHI's capabilities to perform batch predictions on a directory of images, which improves efficiency.

Example for batch prediction:

from sahi.predict import predict

predict(
    model_type="yolov8",
    model_path="path/to/yolov8n.pt",
    model_device="cpu",  # or 'cuda:0'
    model_confidence_threshold=0.4,
    source="path/to/dir",
    slice_height=256,
    slice_width=256,
    overlap_height_ratio=0.2,
    overlap_width_ratio=0.2,
)

For more detailed steps, visit our section on Batch Prediction.


comments: true
description: Enhance your security with real-time object detection using Ultralytics YOLOv8. Reduce false positives and integrate seamlessly with existing systems.
keywords: YOLOv8, Security Alarm System, real-time object detection, Ultralytics, computer vision, integration, false positives

Security Alarm System Project Using Ultralytics YOLOv8

Security Alarm System

The Security Alarm System Project utilizing Ultralytics YOLOv8 integrates advanced computer vision capabilities to enhance security measures. YOLOv8, developed by Ultralytics, provides real-time object detection, allowing the system to identify and respond to potential security threats promptly. This project offers several advantages:

  • Real-time Detection: YOLOv8's efficiency enables the Security Alarm System to detect and respond to security incidents in real-time, minimizing response time.
  • Accuracy: YOLOv8 is known for its accuracy in object detection, reducing false positives and enhancing the reliability of the security alarm system.
  • Integration Capabilities: The project can be seamlessly integrated with existing security infrastructure, providing an upgraded layer of intelligent surveillance.



Watch: Security Alarm System Project with Ultralytics YOLOv8 Object Detection

Code

Set up the parameters of the message

???+ tip "Note"

App Password Generation is necessary
  • Navigate to App Password Generator, designate an app name such as "security project," and obtain a 16-digit password. Copy this password and paste it into the designated password field as instructed.
password = ""
from_email = ""  # must match the email used to generate the password
to_email = ""  # receiver email

Server creation and authentication

import smtplib

server = smtplib.SMTP("smtp.gmail.com: 587")
server.starttls()
server.login(from_email, password)

Email Send Function

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText


def send_email(to_email, from_email, object_detected=1):
    """Sends an email notification indicating the number of objects detected; defaults to 1 object."""
    message = MIMEMultipart()
    message["From"] = from_email
    message["To"] = to_email
    message["Subject"] = "Security Alert"
    # Add in the message body
    message_body = f"ALERT - {object_detected} objects has been detected!!"

    message.attach(MIMEText(message_body, "plain"))
    server.sendmail(from_email, to_email, message.as_string())

Object Detection and Alert Sender

from time import time

import cv2
import torch

from ultralytics import YOLO
from ultralytics.utils.plotting import Annotator, colors


class ObjectDetection:
    def __init__(self, capture_index):
        """Initializes an ObjectDetection instance with a given camera index."""
        self.capture_index = capture_index
        self.email_sent = False

        # model information
        self.model = YOLO("yolov8n.pt")

        # visual information
        self.annotator = None
        self.start_time = 0
        self.end_time = 0

        # device information
        self.device = "cuda" if torch.cuda.is_available() else "cpu"

    def predict(self, im0):
        """Run prediction using a YOLO model for the input image `im0`."""
        results = self.model(im0)
        return results

    def display_fps(self, im0):
        """Displays the FPS on an image `im0` by calculating and overlaying as white text on a black rectangle."""
        self.end_time = time()
        fps = 1 / round(self.end_time - self.start_time, 2)
        text = f"FPS: {int(fps)}"
        text_size = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, 1.0, 2)[0]
        gap = 10
        cv2.rectangle(
            im0,
            (20 - gap, 70 - text_size[1] - gap),
            (20 + text_size[0] + gap, 70 + gap),
            (255, 255, 255),
            -1,
        )
        cv2.putText(im0, text, (20, 70), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 0), 2)

    def plot_bboxes(self, results, im0):
        """Plots bounding boxes on an image given detection results; returns annotated image and class IDs."""
        class_ids = []
        self.annotator = Annotator(im0, 3, results[0].names)
        boxes = results[0].boxes.xyxy.cpu()
        clss = results[0].boxes.cls.cpu().tolist()
        names = results[0].names
        for box, cls in zip(boxes, clss):
            class_ids.append(cls)
            self.annotator.box_label(box, label=names[int(cls)], color=colors(int(cls), True))
        return im0, class_ids

    def __call__(self):
        """Run object detection on video frames from a camera stream, plotting and showing the results."""
        cap = cv2.VideoCapture(self.capture_index)
        assert cap.isOpened()
        cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
        cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
        frame_count = 0
        while True:
            self.start_time = time()
            ret, im0 = cap.read()
            assert ret
            results = self.predict(im0)
            im0, class_ids = self.plot_bboxes(results, im0)

            if len(class_ids) > 0:  # Only send email If not sent before
                if not self.email_sent:
                    send_email(to_email, from_email, len(class_ids))
                    self.email_sent = True
            else:
                self.email_sent = False

            self.display_fps(im0)
            cv2.imshow("YOLOv8 Detection", im0)
            frame_count += 1
            if cv2.waitKey(5) & 0xFF == 27:
                break
        cap.release()
        cv2.destroyAllWindows()
        server.quit()

Call the Object Detection class and Run the Inference

detector = ObjectDetection(capture_index=0)
detector()

That's it! When you execute the code, you'll receive a single notification on your email if any object is detected. The notification is sent immediately, not repeatedly. However, feel free to customize the code to suit your project requirements.

Email Received Sample

Email Received Sample

FAQ

How does Ultralytics YOLOv8 improve the accuracy of a security alarm system?

Ultralytics YOLOv8 enhances security alarm systems by delivering high-accuracy, real-time object detection. Its advanced algorithms significantly reduce false positives, ensuring that the system only responds to genuine threats. This increased reliability can be seamlessly integrated with existing security infrastructure, upgrading the overall surveillance quality.

Can I integrate Ultralytics YOLOv8 with my existing security infrastructure?

Yes, Ultralytics YOLOv8 can be seamlessly integrated with your existing security infrastructure. The system supports various modes and provides flexibility for customization, allowing you to enhance your existing setup with advanced object detection capabilities. For detailed instructions on integrating YOLOv8 in your projects, visit the integration section.

What are the storage requirements for running Ultralytics YOLOv8?

Running Ultralytics YOLOv8 on a standard setup typically requires around 5GB of free disk space. This includes space for storing the YOLOv8 model and any additional dependencies. For cloud-based solutions, Ultralytics HUB offers efficient project management and dataset handling, which can optimize storage needs. Learn more about the Pro Plan for enhanced features including extended storage.

What makes Ultralytics YOLOv8 different from other object detection models like Faster R-CNN or SSD?

Ultralytics YOLOv8 provides an edge over models like Faster R-CNN or SSD with its real-time detection capabilities and higher accuracy. Its unique architecture allows it to process images much faster without compromising on precision, making it ideal for time-sensitive applications like security alarm systems. For a comprehensive comparison of object detection models, you can explore our guide.

How can I reduce the frequency of false positives in my security system using Ultralytics YOLOv8?

To reduce false positives, ensure your Ultralytics YOLOv8 model is adequately trained with a diverse and well-annotated dataset. Fine-tuning hyperparameters and regularly updating the model with new data can significantly improve detection accuracy. Detailed hyperparameter tuning techniques can be found in our hyperparameter tuning guide.

posted @ 2024-09-05 11:58  绝不原创的飞龙  阅读(4)  评论(0编辑  收藏  举报