Google Driveのフォルダ構成
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

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

ColaboratoryへのGoogle Driveのマウント成功
from google.colab import drive
drive.mount('/content/drive')
Mounted at /content/drive


Colaboratoryからディレクトリにアクセス
- treeコマンドがない
- ColaboratoryのOSを確認:結果は、Debian
!tree
/bin/bash: tree: command not found

- apt-get installでtreeコマンドを入れる
!sudo apt-get install tree
- 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
- 最下層のディレクトリにある画像ファイルを開く
- 各コマンドが使えない
!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.



- コマンドでなく、IPythonの機能で開く
- 開けた
from IPython.display import Image,display_jpeg
display_jpeg(Image('drive/MyDrive/google_colaboratory_share_folder/nozomi/nozomi_1.jpg'))

今度は、Macbookのローカルのフォルダにあるファイルを、Colaboratory側に持ってくる
- ファイル選択画面
from google.colab import files
uploaded = files.upload()

- ローカルPCのフォルダ構成

- このファイルをアップロードする


- show1.pngのファイル名(と拡張子)でアップロードされた
show1.png
show1.png(image/png) - 1501098 bytes, last modified: 2021/8/11 - 100% done
Saving show1.png to show1.png

- ファイルを開ける
from IPython.display import Image,display_png
display_png(Image('show1.png'))
Colaboratory側で生成されたファイルが、Google Drive側で閲覧できるかを確認
- touchコマンドで、空のファイルを生成
- cdは、!ではなく%に続けて打つ

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

画像ファイルを加工してみる
%cd drive/MyDrive/google_colaboratory_share_folder/nozomi
!pwd
!ls
Google Driveにも加工された画像ファイルが格納されている
画像ファイルを加工してみる
%cd drive/MyDrive/google_colaboratory_share_folder/nozomi
!pwd
!ls

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






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