はじめに
前回は Yolov5 を使用した物体追跡について説明しました。
今回は Yolov8 を使用した物体追跡について説明します。公式の github はこちらです。
前提条件
前提条件は以下の通りです。
- Windows11
- Python3.9
- PyTorch == 1.12.1+cu113
プログラムの準備
早速、準備していきます。
git clone -b v9.0 https://github.com/mikel-brostrom/yolov8_tracking.git
yolov8 もクローンします。
git clone https://github.com/ultralytics/ultralytics.git
プログラムの実行
以下のコマンドで PC カメラでトラッキングを実行できます。
python .\track.py --source .\MOT16_eval\track_all.gif --yolo-weights yolov5su.pt --save-vid
yolov5s の最新モデルは yolov5su となったそうです。
上記を実行すると、以下の出力が得られます。
yolov8 の方が、非常に簡単にデモを実行することができました。
引数なしで実行できるプログラムに変更
track_custom.py として、以下のように変更しました。
import argparse
import cv2
import os
# limit the number of cpus used by high performance libraries
os.environ["OMP_NUM_THREADS"] = "1"
os.environ["OPENBLAS_NUM_THREADS"] = "1"
os.environ["MKL_NUM_THREADS"] = "1"
os.environ["VECLIB_MAXIMUM_THREADS"] = "1"
os.environ["NUMEXPR_NUM_THREADS"] = "1"
import sys
import platform
import numpy as np
from pathlib import Path
import torch
import torch.backends.cudnn as cudnn
FILE = Path(__file__).resolve()
ROOT = FILE.parents[0] # yolov5 strongsort root directory
WEIGHTS = ROOT / 'weights'
if str(ROOT) not in sys.path:
sys.path.append(str(ROOT)) # add ROOT to PATH
if str(ROOT / 'yolov8') not in sys.path:
sys.path.append(str(ROOT / 'yolov8')) # add yolov5 ROOT to PATH
if str(ROOT / 'trackers' / 'strongsort') not in sys.path:
sys.path.append(str(ROOT / 'trackers' / 'strongsort')) # add strong_sort ROOT to PATH
ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relative
import logging
from yolov8.ultralytics.nn.autobackend import AutoBackend
from yolov8.ultralytics.yolo.data.dataloaders.stream_loaders import LoadImages, LoadStreams
from yolov8.ultralytics.yolo.data.utils import IMG_FORMATS, VID_FORMATS
from yolov8.ultralytics.yolo.utils import DEFAULT_CFG, LOGGER, SETTINGS, callbacks, colorstr, ops
from yolov8.ultralytics.yolo.utils.checks import check_file, check_imgsz, check_imshow, print_args, check_requirements
from yolov8.ultralytics.yolo.utils.files import increment_path
from yolov8.ultralytics.yolo.utils.torch_utils import select_device
from yolov8.ultralytics.yolo.utils.ops import Profile, non_max_suppression, scale_boxes, process_mask, process_mask_native
from yolov8.ultralytics.yolo.utils.plotting import Annotator, colors
from trackers.multi_tracker_zoo import create_tracker
@torch.no_grad()
def run(
source='0',
yolo_weights=WEIGHTS / 'yolov5su.pt', # model.pt path(s),
reid_weights=WEIGHTS / 'osnet_x0_25_msmt17.pt', # model.pt path,
tracking_method='strongsort',
tracking_config=None,
imgsz=(640, 640), # inference size (height, width)
conf_thres=0.25, # confidence threshold
iou_thres=0.45, # NMS IOU threshold
max_det=1000, # maximum detections per image
device='', # cuda device, i.e. 0 or 0,1,2,3 or cpu
show_vid=False, # show results
classes=None, # filter by class: --class 0, or --class 0 2 3
agnostic_nms=False, # class-agnostic NMS
augment=False, # augmented inference
line_thickness=2, # bounding box thickness (pixels)
hide_labels=False, # hide labels
hide_conf=False, # hide confidences
hide_class=False, # hide IDs
half=False, # use FP16 half-precision inference
dnn=False, # use OpenCV DNN for ONNX inference
vid_stride=1, # video frame-rate stride
):
webcam, source = True, str(source)
# Load model
device = select_device(device)
model = AutoBackend(yolo_weights, device=device, dnn=dnn, fp16=half)
stride, names, pt = model.stride, model.names, model.pt
imgsz = check_imgsz(imgsz, stride=stride) # check image size
# Dataloader
bs = 1
if webcam:
show_vid = check_imshow(warn=True)
dataset = LoadStreams(
source,
imgsz=imgsz,
stride=stride,
auto=pt,
transforms=getattr(model.model, 'transforms', None),
vid_stride=vid_stride
)
bs = len(dataset)
else:
dataset = LoadImages(
source,
imgsz=imgsz,
stride=stride,
auto=pt,
transforms=getattr(model.model, 'transforms', None),
vid_stride=vid_stride
)
model.warmup(imgsz=(1 if pt or model.triton else bs, 3, *imgsz)) # warmup
# Create as many strong sort instances as there are video sources
tracker_list = []
tracking_config = ROOT / 'trackers' / tracking_method / 'configs' / (tracking_method + '.yaml')
for i in range(bs):
tracker = create_tracker(tracking_method, tracking_config, reid_weights, device, half)
tracker_list.append(tracker, )
if hasattr(tracker_list[i], 'model'):
if hasattr(tracker_list[i].model, 'warmup'):
tracker_list[i].model.warmup()
outputs = [None] * bs
# Run tracking
seen, windows, dt = 0, [], (Profile(), Profile(), Profile(), Profile())
curr_frames, prev_frames = [None] * bs, [None] * bs
for _, batch in enumerate(dataset):
path, im, im0s, vid_cap, s = batch
# visualize = increment_path(save_dir / Path(path[0]).stem, mkdir=True) if visualize else False
with dt[0]:
im = torch.from_numpy(im).to(device)
im = im.half() if half else im.float() # uint8 to fp16/32
im /= 255.0 # 0 - 255 to 0.0 - 1.0
if len(im.shape) == 3:
im = im[None] # expand for batch dim
# Inference
with dt[1]:
preds = model(im, augment=augment, visualize=False)
# Apply NMS
with dt[2]:
p = non_max_suppression(preds, conf_thres, iou_thres, classes, agnostic_nms, max_det=max_det)
# Process detections
for i, det in enumerate(p): # detections per image
seen += 1
if webcam: # bs >= 1
p, im0, _ = path[i], im0s[i].copy(), dataset.count
p = Path(p) # to Path
s += f'{i}: '
curr_frames[i] = im0
s += '%gx%g ' % im.shape[2:] # print string
annotator = Annotator(im0, line_width=line_thickness, example=str(names))
if hasattr(tracker_list[i], 'tracker') and hasattr(tracker_list[i].tracker, 'camera_update'):
if prev_frames[i] is not None and curr_frames[i] is not None: # camera motion compensation
tracker_list[i].tracker.camera_update(prev_frames[i], curr_frames[i])
if det is not None and len(det):
det[:, :4] = scale_boxes(im.shape[2:], det[:, :4], im0.shape).round() # rescale boxes to im0 size
# Print results
for c in det[:, 5].unique():
n = (det[:, 5] == c).sum() # detections per class
s += f"{n} {names[int(c)]}{'s' * (n > 1)}, " # add to string
# pass detections to strongsort
with dt[3]:
outputs[i] = tracker_list[i].update(det.cpu(), im0)
# draw boxes for visualization
if len(outputs[i]) > 0:
for j, (output) in enumerate(outputs[i]):
bbox = output[0:4]
id = output[4]
cls = output[5]
conf = output[6]
# if save_vid or save_crop or show_vid: # Add bbox/seg to image
c = int(cls) # integer class
id = int(id) # integer id
label = None if hide_labels else (f'{id} {names[c]}' if hide_conf else \
(f'{id} {conf:.2f}' if hide_class else f'{id} {names[c]} {conf:.2f}'))
color = colors(c, True)
annotator.box_label(bbox, label, color=color)
# Stream results
im0 = annotator.result()
if show_vid:
if platform.system() == 'Linux' and p not in windows:
windows.append(p)
cv2.namedWindow(str(p), cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO) # allow window resize (Linux)
cv2.resizeWindow(str(p), im0.shape[1], im0.shape[0])
cv2.imshow(str(p), im0)
if cv2.waitKey(1) == ord('q'): # 1 millisecond
exit()
prev_frames[i] = curr_frames[i]
# Print total time (preprocessing + inference + NMS + tracking)
LOGGER.info(f"{s}{'' if len(det) else '(no detections), '}{sum([dt.dt for dt in dt if hasattr(dt, 'dt')]) * 1E3:.1f}ms")
# Print results
t = tuple(x.t / seen * 1E3 for x in dt) # speeds per image
LOGGER.info(f'Speed: %.1fms pre-process, %.1fms inference, %.1fms NMS, %.1fms {tracking_method} update per image at shape {(1, 3, *imgsz)}' % t)
def main():
run()
if __name__ == "__main__":
main()
コードの説明は前回しましたので割愛します。
おわりに
今回は Yolov8_Tracking について説明しました。
Yolov8 で新しいモデル Yolov5su.pt が出ていたのは知りませんでした。
Yolov5s.pt と比較して、物体検知が途切れることなく検出できます。
Yolov8 は、学習及び検出も簡単なので、是非使用してみてください。
コメント