|
|
@ -292,42 +292,27 @@ Finally, after all threads have completed their task, the windows displaying the |
|
|
|
|
|
|
|
|
|
|
|
# Define model names and video sources |
|
|
|
# Define model names and video sources |
|
|
|
MODEL_NAMES = ["yolov8n.pt", "yolov8n-seg.pt"] |
|
|
|
MODEL_NAMES = ["yolov8n.pt", "yolov8n-seg.pt"] |
|
|
|
SOURCES = ["path/to/video1.mp4", 0] # local video, 0 for webcam |
|
|
|
SOURCES = ["path/to/video.mp4", "0"] # local video, 0 for webcam |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def run_tracker_in_thread(model_name, filename, index): |
|
|
|
def run_tracker_in_thread(model_name, filename): |
|
|
|
""" |
|
|
|
""" |
|
|
|
Runs a video file or webcam stream concurrently with the YOLOv8 model using threading. This function captures video |
|
|
|
Run YOLO tracker in its own thread for concurrent processing. |
|
|
|
frames from a given file or camera source and utilizes the YOLOv8 model for object tracking. The function runs in |
|
|
|
|
|
|
|
its own thread for concurrent processing. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Args: |
|
|
|
Args: |
|
|
|
|
|
|
|
model_name (str): The YOLOv8 model object. |
|
|
|
filename (str): The path to the video file or the identifier for the webcam/external camera source. |
|
|
|
filename (str): The path to the video file or the identifier for the webcam/external camera source. |
|
|
|
model (obj): The YOLOv8 model object. |
|
|
|
|
|
|
|
index (int): An index to uniquely identify the file being processed, used for display purposes. |
|
|
|
|
|
|
|
""" |
|
|
|
""" |
|
|
|
model = YOLO(model_name) |
|
|
|
model = YOLO(model_name) |
|
|
|
video = cv2.VideoCapture(filename) |
|
|
|
results = model.track(filename, save=True, stream=True) |
|
|
|
|
|
|
|
for r in results: |
|
|
|
while True: |
|
|
|
pass |
|
|
|
ret, frame = video.read() |
|
|
|
|
|
|
|
if not ret: |
|
|
|
|
|
|
|
break |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
results = model.track(frame, persist=True) |
|
|
|
|
|
|
|
res_plotted = results[0].plot() |
|
|
|
|
|
|
|
cv2.imshow(f"Tracking_Stream_{index}", res_plotted) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if cv2.waitKey(1) == ord("q"): |
|
|
|
|
|
|
|
break |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
video.release() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Create and start tracker threads using a for loop |
|
|
|
# Create and start tracker threads using a for loop |
|
|
|
tracker_threads = [] |
|
|
|
tracker_threads = [] |
|
|
|
for i, (video_file, model_name) in enumerate(zip(SOURCES, MODEL_NAMES), start=1): |
|
|
|
for video_file, model_name in zip(SOURCES, MODEL_NAMES): |
|
|
|
thread = threading.Thread(target=run_tracker_in_thread, args=(model_name, video_file, i), daemon=True) |
|
|
|
thread = threading.Thread(target=run_tracker_in_thread, args=(model_name, video_file), daemon=True) |
|
|
|
tracker_threads.append(thread) |
|
|
|
tracker_threads.append(thread) |
|
|
|
thread.start() |
|
|
|
thread.start() |
|
|
|
|
|
|
|
|
|
|
@ -395,35 +380,37 @@ To run object tracking on multiple video streams simultaneously, you can use Pyt |
|
|
|
|
|
|
|
|
|
|
|
from ultralytics import YOLO |
|
|
|
from ultralytics import YOLO |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Define model names and video sources |
|
|
|
|
|
|
|
MODEL_NAMES = ["yolov8n.pt", "yolov8n-seg.pt"] |
|
|
|
|
|
|
|
SOURCES = ["path/to/video.mp4", "0"] # local video, 0 for webcam |
|
|
|
|
|
|
|
|
|
|
|
def run_tracker_in_thread(filename, model, file_index): |
|
|
|
|
|
|
|
video = cv2.VideoCapture(filename) |
|
|
|
|
|
|
|
while True: |
|
|
|
|
|
|
|
ret, frame = video.read() |
|
|
|
|
|
|
|
if not ret: |
|
|
|
|
|
|
|
break |
|
|
|
|
|
|
|
results = model.track(frame, persist=True) |
|
|
|
|
|
|
|
res_plotted = results[0].plot() |
|
|
|
|
|
|
|
cv2.imshow(f"Tracking_Stream_{file_index}", res_plotted) |
|
|
|
|
|
|
|
if cv2.waitKey(1) & 0xFF == ord("q"): |
|
|
|
|
|
|
|
break |
|
|
|
|
|
|
|
video.release() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def run_tracker_in_thread(model_name, filename): |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
Run YOLO tracker in its own thread for concurrent processing. |
|
|
|
|
|
|
|
|
|
|
|
model1 = YOLO("yolov8n.pt") |
|
|
|
Args: |
|
|
|
model2 = YOLO("yolov8n-seg.pt") |
|
|
|
model_name (str): The YOLOv8 model object. |
|
|
|
video_file1 = "path/to/video1.mp4" |
|
|
|
filename (str): The path to the video file or the identifier for the webcam/external camera source. |
|
|
|
video_file2 = 0 # Path to a second video file, or 0 for a webcam |
|
|
|
""" |
|
|
|
|
|
|
|
model = YOLO(model_name) |
|
|
|
|
|
|
|
results = model.track(filename, save=True, stream=True) |
|
|
|
|
|
|
|
for r in results: |
|
|
|
|
|
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
tracker_thread1 = threading.Thread(target=run_tracker_in_thread, args=(video_file1, model1, 1), daemon=True) |
|
|
|
|
|
|
|
tracker_thread2 = threading.Thread(target=run_tracker_in_thread, args=(video_file2, model2, 2), daemon=True) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tracker_thread1.start() |
|
|
|
# Create and start tracker threads using a for loop |
|
|
|
tracker_thread2.start() |
|
|
|
tracker_threads = [] |
|
|
|
|
|
|
|
for video_file, model_name in zip(SOURCES, MODEL_NAMES): |
|
|
|
|
|
|
|
thread = threading.Thread(target=run_tracker_in_thread, args=(model_name, video_file), daemon=True) |
|
|
|
|
|
|
|
tracker_threads.append(thread) |
|
|
|
|
|
|
|
thread.start() |
|
|
|
|
|
|
|
|
|
|
|
tracker_thread1.join() |
|
|
|
# Wait for all tracker threads to finish |
|
|
|
tracker_thread2.join() |
|
|
|
for thread in tracker_threads: |
|
|
|
|
|
|
|
thread.join() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Clean up and close windows |
|
|
|
cv2.destroyAllWindows() |
|
|
|
cv2.destroyAllWindows() |
|
|
|
``` |
|
|
|
``` |
|
|
|
|
|
|
|
|
|
|
|