Code Monkey home page Code Monkey logo

Comments (3)

glenn-jocher avatar glenn-jocher commented on June 2, 2024

Hello!

Currently, Ultralytics YOLOv8 does not support setting a vid_stride dynamically for different streams within a single tracking command. Each instance of model tracking would need to use the same vid_stride.

However, you can achieve this functionality by running separate instances for each stream with the respective vid_stride settings. Here's a simple way to manage this using threading:

import threading
from ultralytics import YOLO

def track_stream(source, vid_stride):
    model = YOLO('yolov8s.pt', task='detect')
    results = model.track(source, device=device, stream=True, classes=classes, verbose=False, stream_buffer=True, tracker=byte_track, vid_stride=vid_stride)
    for pred in results:
        print(pred)

# Define your streams and corresponding stride values
streams = {'rtsp1': 1, 'rtsp2': 1, 'rtsp3': 5, 'rtsp4': 5}

# Create and start a separate thread for each stream
threads = []
for stream, stride in streams.items():
    t = threading.Thread(target=track_stream, args=(stream, stride))
    t.start()
    threads.append(t)

# Wait for all threads to finish
for t in threads:
    t.join()

This script will allow each stream to process at its specific vid_stride, running concurrently in separate threads. Let us know if you need further assistance! πŸš€

from ultralytics.

pornpra avatar pornpra commented on June 2, 2024

@glenn-jocher Thanks for your response :)

Can I use OpenCV to pre-process video streams and skip frames such that rtsp1 and rtsp2 skip every frame, and rtsp3 and rtsp4 skip every 5 frames before passing them to the YOLO model (set vid_stride=1), instead of using threading?

import cv2
from ultralytics import YOLO

def process_stream(stream_url, frame_skip, device):
    cap = cv2.VideoCapture(stream_url)
    model = YOLO('yolov8s.pt', task='detect')
    frame_count = 0

    while True:
        ret, frame = cap.read()
        if not ret:
            break
        if frame_count % (frame_skip + 1) == 0:  # frame_skip + 1 because we include the current frame in the count
            results = model.track(frame, device=device, stream=False, classes=None, verbose=False, stream_buffer=False, tracker='byte_track', vid_stride=1)
            for pred in results:
                path = pred.path
                print(f"Processed frame from {stream_url} saved to {path}")
        frame_count += 1

    cap.release()

# Device configuration
device = 'cuda'  # or 'cpu'

# Define your streams and their respective frame skips (corresponding to vid_stride - 1)
streams = {
    'rtsp1': 0,  # Process every frame
    'rtsp2': 0,  # Process every frame
    'rtsp3': 4,  # Skip 4 frames
    'rtsp4': 4,  # Skip 4 frames
}

# Loop for processing each stream one at a time
for stream_url, skip in streams.items():
    print(f"Starting processing for {stream_url}")
    process_stream(stream_url, skip, device)
    print(f"Finished processing for {stream_url}")

Can this approach simulate the effect of using different vid_stride values directly in the YOLO tracking function?
I am asking about this method because I am concerned about RAM usage when using threads.

from ultralytics.

glenn-jocher avatar glenn-jocher commented on June 2, 2024

@pornpra hi there! 😊

Yes, your approach of using OpenCV to pre-process and skip frames before feeding them into the YOLO model absolutely works as a good alternative to manage different frame skips without threading. By setting vid_stride=1 in the YOLO model and handling the frame skipping manually in your script as you've outlined, you effectively simulate various vid_stride behaviors.

Your code example correctly adjusts the frequency of frames processed from each stream by changing the frame_skip parameter. This will help control the RAM usage as you process one stream at a time, rather than handling multiple streams concurrently with threading. Just ensure your system is capable of handling the looped single-stream processing without lagging, especially when processing higher-resolution video streams.

Let us know how this works out, or if there's anything else you'd like to optimize! πŸ‘

from ultralytics.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.