使用 YOLOv8 和 ByteTracker 進(jìn)行實(shí)時人員跟蹤和計數(shù)
在計算機(jī)視覺領(lǐng)域,實(shí)時跟蹤和統(tǒng)計人數(shù)對于各種應(yīng)用至關(guān)重要,從監(jiān)控到事件管理。在這篇博客文章中,我們將探討如何利用YOLOv8和ByteTracker實(shí)現(xiàn)準(zhǔn)確的人數(shù)統(tǒng)計。
引言
- YOLOv8(You Only Look Once,第八版)是一種以其速度和準(zhǔn)確性而聞名的最新對象檢測模型。
- ByteTracker是一種先進(jìn)的跟蹤算法,旨在維持對象在幀之間的身份,使其成為執(zhí)行人數(shù)統(tǒng)計等任務(wù)的理想選擇。
這種組合不僅允許我們在幀中檢測到人,而且還能夠跟蹤他們在幀之間的移動,為實(shí)時人數(shù)統(tǒng)計提供了強(qiáng)大的解決方案。
先決條件
在深入實(shí)現(xiàn)之前,請確保您具備以下條件:
- Python 3.10
- Ultralytics
設(shè)置環(huán)境
首先,創(chuàng)建并激活虛擬環(huán)境以管理依賴項(xiàng):
conda create -n person-tracker python==3.10
conda activate person-tracker
安裝必要的庫:
pip install ultralytics
實(shí)現(xiàn)人數(shù)統(tǒng)計
(1) 導(dǎo)入庫從導(dǎo)入所需的庫開始:
from ultralytics import YOLO
from datetime import datetime
import os
(2) 定義PersonTracker類
創(chuàng)建一個PersonTracker類,該類集成了用于檢測的YOLOv8和用于跟蹤的ByteTracker:
class PersonTracker:
def __init__(self, model_path, result_dir='results/', tracker_config="bytetrack.yaml", conf=0.5, device='cuda:0',
iou=0.5, img_size=(720, 1080)):
self.model = YOLO(model_path)
self.result_dir = result_dir
self.tracker_config = tracker_config
self.conf = conf
self.device = device
self.iou = iou
self.img_size = img_size
def create_result_file(self):
folder_name = datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
result_file_path = os.path.join(self.result_dir, folder_name + ".txt")
os.makedirs(self.result_dir, exist_ok=True)
with open(result_file_path, 'w') as file:
file.write(folder_name + "\n")
return result_file_path
def detect_and_track(self, source, show=True, logger=None):
result_file = self.create_result_file()
person_count = 0
previous_person_count = 0
results = self.model.track(
source, show=show, stream=True, tracker=self.tracker_config, conf=self.conf,
device=self.device, iou=self.iou, stream_buffer=True, classes=[0], imgsz=self.img_size
)
for i, result in enumerate(results):
boxes = result.boxes
try:
id_count = boxes.id.int().tolist()
max_id = max(id_count)
if max_id > person_count:
person_count = max_id
if person_count != previous_person_count:
previous_person_count = person_count
with open(result_file, 'a') as filewrite:
filewrite.write(f"Person count: {person_count}\n")
if logger:
logger.info(f"Person count: {person_count}")
except Exception as e:
pass
(3) 運(yùn)行人員跟蹤器
視頻文件跟蹤:
if __name__ == '__main__':
source = "path/to/your/video.mp4"
tracker = PersonTracker(model_path='path/to/yolov8_model.pt')
tracker.detect_and_track(source=source)
網(wǎng)絡(luò)攝像頭跟蹤:
if __name__ == '__main__':
source = 0 # Use 0 for the default webcam
tracker = PersonTracker(model_path='path/to/yolov8_model.pt')
tracker.detect_and_track(source=source)
RTSP流跟蹤:
if __name__ == '__main__':
source = 'rtsp://username:password@ip_address:port/path'
tracker = PersonTracker(model_path='path/to/yolov8_model.pt')
tracker.detect_and_track(source=source)
支持的模型
對于YOLOv8,模型通常根據(jù)它們的準(zhǔn)確性和速度權(quán)衡進(jìn)行分類。通常支持以下模型:
- YOLOv8n(Nano):提供高速度和較低的準(zhǔn)確性。非常適合處理速度的實(shí)時應(yīng)用
- YOLOv8s(Small):平衡速度和準(zhǔn)確性。適用于許多實(shí)際應(yīng)用。
- YOLOv8m(Medium):在速度和準(zhǔn)確性之間提供良好的權(quán)衡。適用于更苛刻的應(yīng)用。
- YOLOv8l(Large):高準(zhǔn)確性,速度較低。最適合準(zhǔn)確性為優(yōu)先考慮的場景。
結(jié)論
通過結(jié)合YOLOv8和ByteTracker,您可以有效地在幀之間檢測和跟蹤人員,提供準(zhǔn)確的計數(shù)和有價值的洞察。這個解決方案可以擴(kuò)展到需要實(shí)時個人監(jiān)控和分析的各種應(yīng)用。
參考文獻(xiàn):
- YOLOv8文檔:https://docs.ultralytics.com/
- ByteTracker論文:https://arxiv.org/abs/2005.03659
- 源碼:https://github.com/ishworrsubedii/person_tracker