Yolov8 を M1 Mac で使う

Yolov8 を M1 Mac で使う

YOLOv8導入まとめ
を参考に

pip install ultralytics

を実行

物体検出

yolo predict model=yolov8n.pt source='https://ultralytics.com/images/bus.jpg'

というように
Source に画像ファイルを指定すればOKみたい

物体検出ではなくセグメンテーションしたい場合は、yolov8n.ptではなくyolov8n-seg.ptを使う

yolo predict model=yolov8n-seg.pt source='https://ultralytics.com/images/bus.jpg'

これもsourceで画像ファイルを指定する

コマンドを実行すると

runs/detect/predict/

に指定したファイル名と同じファイル名で検出結果が作成される
とあるが

Results saved to runs/detect/predict

でどこに保存されているかわからないため

更新日が最近のファイルを見つける(findコマンド)
を参考に

find . -mtime -1 -ls | top

で検索し

ls ./runs/detect/predict 

でファイルができているのを確認

Finder から開くと面倒なので

MacでターミナルからFinderを開くコマンド「open」
を参考に

open .

でカレントディレクトリを Finder で開いて確認

ちなみに
yolov8n.pt
yolov8n-seg.pt

Nはモデルのサイズで
x > l > m > s > n
の順に大きなモデルになっている

xが最も大きいモデルで、nが最も小さいモデル

大きいモデルは検出にパワーが必要になるものの、検出精度が高くなる

xサイズのモデルで検出なら

yolo predict model=yolov8x.pt source='https://ultralytics.com/images/bus.jpg'

M1Mac 16GB で22秒ほどで処理できた

ポーズ検出には
yolov8x.ptの代わりにyolov8x-pose.ptを使う

yolo predict model=yolov8x-pose.pt source='https://ultralytics.com/images/bus.jpg'

これで
首から上が緑、腕が青、体がパープル、足がオレンジ色で、それぞれポーズが検出される

大体20秒くらいで処理

yolov8n.pt、yolov8n-seg.pt、yolov8n-pose.ptなどについての情報は、YOLOv8 Modelsにある

なおGPUを活用するには
Google Colaboratory
を使う

ちなみに興味があったんで
maMacbookAir 16GBでどのくらいかかるか実験

yolo predict model=yolov8x-seg.pt source='https://youtu.be/Zgi9g1ksQHc'

を実験したけど

Ultralytics YOLOv8.0.132 🚀 Python-3.10.6 torch-2.0.1 CPU
YOLOv8x-seg summary (fused): 295 layers, 71797696 parameters, 0 gradients

ERROR: Unable to extract uploader id; please report this issue on https://yt-dl.org/bug . Make sure you are using the latest version; see  https://yt-dl.org/update  on how to update. Be sure to call youtube-dl with the --verbose flag and include its complete output.
Traceback (most recent call last):
  File "/Users/snowpool/.pyenv/versions/3.10.6/lib/python3.10/site-packages/youtube_dl/YoutubeDL.py", line 815, in wrapper
    return func(self, *args, **kwargs)
  File "/Users/snowpool/.pyenv/versions/3.10.6/lib/python3.10/site-packages/youtube_dl/YoutubeDL.py", line 836, in __extract_info
    ie_result = ie.extract(url)
  File "/Users/snowpool/.pyenv/versions/3.10.6/lib/python3.10/site-packages/youtube_dl/extractor/common.py", line 534, in extract
    ie_result = self._real_extract(url)
  File "/Users/snowpool/.pyenv/versions/3.10.6/lib/python3.10/site-packages/youtube_dl/extractor/youtube.py", line 1794, in _real_extract
    'uploader_id': self._search_regex(r'/(?:channel|user)/([^/?&#]+)', owner_profile_url, 'uploader id') if owner_profile_url else None,
  File "/Users/snowpool/.pyenv/versions/3.10.6/lib/python3.10/site-packages/youtube_dl/extractor/common.py", line 1012, in _search_regex
    raise RegexNotFoundError('Unable to extract %s' % _name)
youtube_dl.utils.RegexNotFoundError: Unable to extract uploader id; please report this issue on https://yt-dl.org/bug . Make sure you are using the latest version; see  https://yt-dl.org/update  on how to update. Be sure to call youtube-dl with the --verbose flag and include its complete output.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/snowpool/.pyenv/versions/3.10.6/lib/python3.10/site-packages/pafy/backend_youtube_dl.py", line 40, in _fetch_basic
    self._ydl_info = ydl.extract_info(self.videoid, download=False)
  File "/Users/snowpool/.pyenv/versions/3.10.6/lib/python3.10/site-packages/youtube_dl/YoutubeDL.py", line 808, in extract_info
    return self.__extract_info(url, ie, download, extra_info, process)
  File "/Users/snowpool/.pyenv/versions/3.10.6/lib/python3.10/site-packages/youtube_dl/YoutubeDL.py", line 824, in wrapper
    self.report_error(compat_str(e), e.format_traceback())
  File "/Users/snowpool/.pyenv/versions/3.10.6/lib/python3.10/site-packages/youtube_dl/YoutubeDL.py", line 628, in report_error
    self.trouble(error_message, tb)
  File "/Users/snowpool/.pyenv/versions/3.10.6/lib/python3.10/site-packages/youtube_dl/YoutubeDL.py", line 598, in trouble
    raise DownloadError(message, exc_info)
youtube_dl.utils.DownloadError: ERROR: Unable to extract uploader id; please report this issue on https://yt-dl.org/bug . Make sure you are using the latest version; see  https://yt-dl.org/update  on how to update. Be sure to call youtube-dl with the --verbose flag and include its complete output.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/snowpool/.pyenv/versions/3.10.6/bin/yolo", line 8, in <module>
    sys.exit(entrypoint())
  File "/Users/snowpool/.pyenv/versions/3.10.6/lib/python3.10/site-packages/ultralytics/yolo/cfg/__init__.py", line 407, in entrypoint
    getattr(model, mode)(**overrides)  # default args from model
  File "/Users/snowpool/.pyenv/versions/3.10.6/lib/python3.10/site-packages/torch/utils/_contextlib.py", line 115, in decorate_context
    return func(*args, **kwargs)
  File "/Users/snowpool/.pyenv/versions/3.10.6/lib/python3.10/site-packages/ultralytics/yolo/engine/model.py", line 255, in predict
    return self.predictor.predict_cli(source=source) if is_cli else self.predictor(source=source, stream=stream)
  File "/Users/snowpool/.pyenv/versions/3.10.6/lib/python3.10/site-packages/ultralytics/yolo/engine/predictor.py", line 195, in predict_cli
    for _ in gen:  # running CLI inference without accumulating any outputs (do not modify)
  File "/Users/snowpool/.pyenv/versions/3.10.6/lib/python3.10/site-packages/torch/utils/_contextlib.py", line 35, in generator_context
    response = gen.send(None)
  File "/Users/snowpool/.pyenv/versions/3.10.6/lib/python3.10/site-packages/ultralytics/yolo/engine/predictor.py", line 222, in stream_inference
    self.setup_source(source if source is not None else self.args.source)
  File "/Users/snowpool/.pyenv/versions/3.10.6/lib/python3.10/site-packages/ultralytics/yolo/engine/predictor.py", line 203, in setup_source
    self.dataset = load_inference_source(source=source, imgsz=self.imgsz, vid_stride=self.args.vid_stride)
  File "/Users/snowpool/.pyenv/versions/3.10.6/lib/python3.10/site-packages/ultralytics/yolo/data/build.py", line 159, in load_inference_source
    dataset = LoadStreams(source, imgsz=imgsz, vid_stride=vid_stride)
  File "/Users/snowpool/.pyenv/versions/3.10.6/lib/python3.10/site-packages/ultralytics/yolo/data/dataloaders/stream_loaders.py", line 48, in __init__
    s = get_best_youtube_url(s)
  File "/Users/snowpool/.pyenv/versions/3.10.6/lib/python3.10/site-packages/ultralytics/yolo/data/dataloaders/stream_loaders.py", line 386, in get_best_youtube_url
    return pafy.new(url).getbest(preftype='mp4').url
  File "/Users/snowpool/.pyenv/versions/3.10.6/lib/python3.10/site-packages/pafy/pafy.py", line 124, in new
    return Pafy(url, basic, gdata, size, callback, ydl_opts=ydl_opts)
  File "/Users/snowpool/.pyenv/versions/3.10.6/lib/python3.10/site-packages/pafy/backend_youtube_dl.py", line 31, in __init__
    super(YtdlPafy, self).__init__(*args, **kwargs)
  File "/Users/snowpool/.pyenv/versions/3.10.6/lib/python3.10/site-packages/pafy/backend_shared.py", line 97, in __init__
    self._fetch_basic()
  File "/Users/snowpool/.pyenv/versions/3.10.6/lib/python3.10/site-packages/pafy/backend_youtube_dl.py", line 43, in _fetch_basic
    raise IOError(str(e).replace('YouTube said', 'Youtube says'))
OSError: ERROR: Unable to extract uploader id; please report this issue on https://yt-dl.org/bug . Make sure you are using the latest version; see  https://yt-dl.org/update  on how to update. Be sure to call youtube-dl with the --verbose flag and include its complete output.

で終了してしまった

とりあえず静止画像はここまででできそうなので
【やってみた】YOLOv8の機能試す&Webカメラでリアルタイム推論
を参考に
リアルタイムでできるようにする