2
2

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 3 years have passed since last update.

ColaboratoryとGoogle Drive連携のやり方総点検! 〜 マウントからファイルのアップロードと出力まで

Last updated at Posted at 2021-08-19

Google Driveのフォルダ構成

a

c

Colaboratoryを起動

エラー

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


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-7-c3c60ac8b3ff> in <module>()
      1 from google.colab import drive
----> 2 drive.mount('/content/drive/My Drive/google_colaboratory_share_folder')

/usr/local/lib/python3.7/dist-packages/google/colab/drive.py in mount(mountpoint, force_remount, timeout_ms, use_metadata_server)
    105 
    106   if ' ' in mountpoint:
--> 107     raise ValueError('Mountpoint must not contain a space.')
    108 
    109   mountpoint = _os.path.expanduser(mountpoint)

ValueError: Mountpoint must not contain a space.

エラー

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

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-9-95b8b3a68d3c> in <module>()
      1 from google.colab import drive
----> 2 drive.mount('/content/drive/MyDrive/google_colaboratory_share_folder')

/usr/local/lib/python3.7/dist-packages/google/colab/drive.py in mount(mountpoint, force_remount, timeout_ms, use_metadata_server)
    168     normed = _os.path.normpath(mountpoint)
    169     if '/' in normed and not _os.path.exists(_os.path.dirname(normed)):
--> 170       raise ValueError('Mountpoint must be in a directory that exists')
    171   except:
    172     d.kill(_signal.SIGKILL)

ValueError: Mountpoint must be in a directory that exists
スクリーンショット 2021-08-19 22 54 25

OK

from google.colab import drive
drive.mount('/content/drive')
スクリーンショット 2021-08-19 22 49 08

スクリーンショット 2021-08-19 22 51 39

スクリーンショット 2021-08-19 22 52 35

ColaboratoryへのGoogle Driveのマウント成功

from google.colab import drive
drive.mount('/content/drive')
Mounted at /content/drive
スクリーンショット 2021-08-19 22 04 51 スクリーンショット 2021-08-19 22 55 26

Colaboratoryからディレクトリにアクセス

  • treeコマンドがない
  • ColaboratoryのOSを確認:結果は、Debian
!tree
/bin/bash: tree: command not found
スクリーンショット 2021-08-19 22 56 13
  • apt-get installでtreeコマンドを入れる
!sudo apt-get install tree

スクリーンショット 2021-08-19 23 00 54

  • treeでディレクトリのツリー構成を確認
!tree
.
├── drive
│   └── MyDrive
│       └── google_colaboratory_share_folder
│           └── nozomi
│               ├── nozomi_1.jpg
│               ├── nozomi_2.jpg
│               ├── nozomi_3.jpg
│               └── nozomi_4.jpg
└── sample_data
    ├── anscombe.json
    ├── california_housing_test.csv
    ├── california_housing_train.csv
    ├── mnist_test.csv
    ├── mnist_train_small.csv
    └── README.md

5 directories, 10 files

スクリーンショット 2021-08-19 23 01 52

  • 最下層のディレクトリにある画像ファイルを開く
  • 各コマンドが使えない
!pwd
/content

!xdg-open .drive/MyDrive/google_colaboratory_share_folder/nozomi/nozomi_1.jpg
xdg-open: file '.drive/MyDrive/google_colaboratory_share_folder/nozomi/nozomi_1.jpg' does not exist

!sudo apt-get install open
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package open

!ls
drive  sample_data

!ls drive
MyDrive

!ls .drive
ls: cannot access '.drive': No such file or directory

!ls drive/MyDrive
google_colaboratory_share_folder

!ls drive/MyDrive/google_colaboratory_share_folder
nozomi

!ls drive/MyDrive/google_colaboratory_share_folder/nozomi
nozomi_1.jpg  nozomi_2.jpg  nozomi_3.jpg  nozomi_4.jpg

!ls drive/MyDrive/google_colaboratory_share_folder/nozomi/nozomi_1.jpg
drive/MyDrive/google_colaboratory_share_folder/nozomi/nozomi_1.jpg

!xdg-open drive/MyDrive/google_colaboratory_share_folder/nozomi/nozomi_1.jpg 
# https://matsuoshi.hatenablog.com/entry/2019/02/13/122347

Error: no "view" mailcap rules found for type "image/jpeg"
/usr/bin/xdg-open: 851: /usr/bin/xdg-open: www-browser: not found
/usr/bin/xdg-open: 851: /usr/bin/xdg-open: links2: not found
/usr/bin/xdg-open: 851: /usr/bin/xdg-open: elinks: not found
/usr/bin/xdg-open: 851: /usr/bin/xdg-open: links: not found
/usr/bin/xdg-open: 851: /usr/bin/xdg-open: lynx: not found
/usr/bin/xdg-open: 851: /usr/bin/xdg-open: w3m: not found
xdg-open: no method available for opening 'drive/MyDrive/google_colaboratory_share_folder/nozomi/nozomi_1.jpg'

!gnome-open drive/MyDrive/google_colaboratory_share_folder/nozomi/nozomi_1.jpg 
/bin/bash: gnome-open: command not found

!mimeopen drive/MyDrive/google_colaboratory_share_folder/nozomi/nozomi_1.jpg 
/bin/bash: mimeopen: command not found

!display drive/MyDrive/google_colaboratory_share_folder/nozomi/nozomi_1.jpg 
/bin/bash: display: command not found

!sudo apt-get -y install imagemagick
# https://ch.nicovideo.jp/matarou/blomaga/ar933895
# https://pcl.solima.net/archives/716 

!display drive/MyDrive/google_colaboratory_share_folder/nozomi/nozomi_1.jpg 
display-im6.q16: unable to open X server `' @ error/display.c/DisplayImageCommand/432.
スクリーンショット 2021-08-19 23 03 34 スクリーンショット 2021-08-19 23 03 49

スクリーンショット 2021-08-19 23 04 06

スクリーンショット 2021-08-19 23 04 18
  • コマンドでなく、IPythonの機能で開く
  • 開けた
from IPython.display import Image,display_jpeg
display_jpeg(Image('drive/MyDrive/google_colaboratory_share_folder/nozomi/nozomi_1.jpg'))
スクリーンショット 2021-08-19 23 06 36

今度は、Macbookのローカルのフォルダにあるファイルを、Colaboratory側に持ってくる

  • ファイル選択画面
from google.colab import files
uploaded = files.upload()
スクリーンショット 2021-08-19 22 29 36
  • ローカルPCのフォルダ構成
スクリーンショット 2021-08-20 0 37 22
  • このファイルをアップロードする
スクリーンショット 2021-08-20 0 38 23 スクリーンショット 2021-08-20 0 39 05
  • show1.pngのファイル名(と拡張子)でアップロードされた
show1.png
show1.png(image/png) - 1501098 bytes, last modified: 2021/8/11 - 100% done
Saving show1.png to show1.png
スクリーンショット 2021-08-19 22 30 38
  • ファイルを開ける
from IPython.display import Image,display_png
display_png(Image('show1.png'))

スクリーンショット 2021-08-19 23 11 56

Colaboratory側で生成されたファイルが、Google Drive側で閲覧できるかを確認

  • touchコマンドで、空のファイルを生成
  • cdは、!ではなく%に続けて打つ
スクリーンショット 2021-08-19 23 13 25

Google Drive側でも、テキストファイルの存在を確認できる

スクリーンショット 2021-08-19 22 45 40

画像ファイルを加工してみる

%cd drive/MyDrive/google_colaboratory_share_folder/nozomi
!pwd
!ls

Google Driveにも加工された画像ファイルが格納されている

画像ファイルを加工してみる

%cd drive/MyDrive/google_colaboratory_share_folder/nozomi
!pwd
!ls
スクリーンショット 2021-08-20 0 03 01
import numpy as np
import cv2
img = cv2.imread("nozomi_2.jpg")
print(img)

resize_img = cv2.resize(img,(img.shape[1] // 10, img.shape[0] // 10))
cv2.imwrite("resized_nozomi_1.jpg", resize_img)
!ls

from IPython.display import Image,display_png
display_png(Image('resized_nozomi_1.png'))

mosaic_img = cv2.resize(resize_img,(img.shape[1], img.shape[0]))
cv2.imwrite("mosaic_nozomi_1.jpg", mosaic_img)

!ls | grep mosaic
!ls

from IPython.display import Image,display_jpeg
display_jpeg(Image('mosaic_nozomi_1.jpg'))

cvt_img2 = cv2.bitwise_not(img)
cv2.imwrite("cvt_nozomi_1.jpg", cvt_img2)
from IPython.display import Image,display_jpeg
display_jpeg(Image('cvt_nozomi_1.jpg'))

!ls
スクリーンショット 2021-08-20 0 05 21 スクリーンショット 2021-08-20 0 05 29 スクリーンショット 2021-08-20 0 05 46 スクリーンショット 2021-08-20 0 06 01 スクリーンショット 2021-08-20 0 06 16

スクリーンショット 2021-08-20 0 06 26

スクリーンショット 2021-08-20 0 06 46

Google Driveにも加工された画像ファイルが格納されている

スクリーンショット 2021-08-20 0 13 20
2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?