0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

YOLOv8で画像認識元のパスをローカルから読み込ませたいがエラーが出る Google colaboratory

Posted at

Google colaboratoryでYOLOを使おうとしたら

%cd {HOME}
# This source will be the image I want to output
!yolo task=detect mode=predict model=yolov8n.pt conf=0.25 source='xxxxxxx(ここはGoogle Driveの画像を読み込ませたくてシェア用パスを入れてました。ここが原因)'

をやった時

エラー文章

/content
Downloading https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8n.pt to 'yolov8n.pt'...
100% 6.23M/6.23M [00:00<00:00, 81.2MB/s]
Ultralytics YOLOv8.0.196 🚀 Python-3.10.12 torch-2.1.0+cu121 CUDA:0 (Tesla T4, 15102MiB)
YOLOv8n summary (fused): 168 layers, 3151904 parameters, 0 gradients, 8.7 GFLOPs

[ERROR:0@6.232] global cap.cpp:164 open VIDEOIO(CV_IMAGES): raised OpenCV exception:

..
.
.
.
Failed to open https://drive.google.com/file/d/11wsHOXQKVSS-FQoh-ZOeP0LAcCumq-rZ/view?usp=sharing

がでた

原因

Google Colabからローカルのファイルを直接的に読み込むことはできません。Colabはクラウドベースの環境であり、ローカルマシン上のファイルには直接アクセスできない

とのこと。
つまり、Google drive内のものを直接読めるようにしないといけなくてその準備が足りなかったことになります。

解決方法

Google Driveのマウント:

Google ColabでGoogle Driveをマウントすることができます。Google Driveにファイルをアップロードしておき、Colabからアクセスできるようにすることができます。

直接アップロード:

Colabのセッションにファイルをアップロードすることができます。Colabの左側のサイドバーにある「ファイル」タブから、ファイルをアップロードできます。

例えば、Google Driveをマウントした場合、以下のようにしてローカルファイルをColab上で扱うことができます。

from google.colab import drive
drive.mount('/content/drive')

### ファイルのパスを指定して読み込み
file_path = '/content/drive/My Drive/path_to_your_file.txt'
with open(file_path, 'r') as file:
    content = file.read()

memo

Google Driveを結局自分は使いましたがGoogleDriveはファイルパスを自分で手打ちしなければいけないのであまり階層深すぎたり複雑な名前にしていると大変です。気を付けましょう。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?