Skip to content

Commit 82e1387

Browse files
committed
Created using Colaboratory
1 parent ce03ee9 commit 82e1387

1 file changed

Lines changed: 158 additions & 0 deletions

File tree

GoogleDrive_Tutorial.ipynb

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"colab": {
6+
"name": "GoogleDrive_Tutorial.ipynb",
7+
"provenance": [],
8+
"collapsed_sections": [],
9+
"include_colab_link": true
10+
},
11+
"kernelspec": {
12+
"name": "python3",
13+
"display_name": "Python 3"
14+
}
15+
},
16+
"cells": [
17+
{
18+
"cell_type": "markdown",
19+
"metadata": {
20+
"id": "view-in-github",
21+
"colab_type": "text"
22+
},
23+
"source": [
24+
"<a href=\"https://colab.research.google.com/github/SteveLC/ThinkJavaCode/blob/master/GoogleDrive_Tutorial.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
25+
]
26+
},
27+
{
28+
"cell_type": "markdown",
29+
"metadata": {
30+
"id": "ca2CpPPUvO-h",
31+
"colab_type": "text"
32+
},
33+
"source": [
34+
"# **Google Drive 檔案存取教學**\n",
35+
"\n",
36+
"作者:黃健祐 Chien-Yu Huang\n",
37+
"\n",
38+
"若有任何問題,歡迎來信至助教信箱 ntu-ml-2020spring-ta@googlegroups.com"
39+
]
40+
},
41+
{
42+
"cell_type": "markdown",
43+
"metadata": {
44+
"id": "EAM_tPQAELh0",
45+
"colab_type": "text"
46+
},
47+
"source": [
48+
"Colab 可以透過兩種方式存取 Google Drive 的檔案:\n",
49+
"\n",
50+
"**1. 透過檔案共用連結**\n",
51+
"\n",
52+
" 一般來說,Google Drive 的檔案連結會是類似下面的結構:\n",
53+
"\n",
54+
" https://drive.google.com/open?id=1duQU7xqXRsOSPYeOR0zLiSA8g_LCFzoV\n",
55+
" \n",
56+
" 其中 \"open?id=\" 後面的那一串亂碼稱為 **file_id**\n",
57+
"\n",
58+
" 在 colab 中,我們可以直接用 **file_id** 下載檔案,優點是這個過程不需要再手動輸入任何訊息,但缺點是下載下來的檔案在程式執行完畢後就會消失,也就是每一次都必須重新下載,使得執行時間增加。\n",
59+
"\n",
60+
"\n",
61+
"\n",
62+
"\n"
63+
]
64+
},
65+
{
66+
"cell_type": "code",
67+
"metadata": {
68+
"id": "XztYEj0oD7J3",
69+
"colab_type": "code",
70+
"outputId": "f1865640-0d20-4f6e-bb92-9dd047ac3ccc",
71+
"colab": {
72+
"base_uri": "https://localhost:8080/",
73+
"height": 107
74+
}
75+
},
76+
"source": [
77+
"# 下載 file_id 為 \"1duQU7xqXRsOSPYeOR0zLiSA8g_LCFzoV\" 的檔案,並將它命名為 Minori.jpg\n",
78+
"!gdown --id '1duQU7xqXRsOSPYeOR0zLiSA8g_LCFzoV' --output Minori.jpg\n",
79+
"# 列出目前目錄下所有的檔案\n",
80+
"!ls"
81+
],
82+
"execution_count": 0,
83+
"outputs": [
84+
{
85+
"output_type": "stream",
86+
"text": [
87+
"Downloading...\n",
88+
"From: https://drive.google.com/uc?id=1duQU7xqXRsOSPYeOR0zLiSA8g_LCFzoV\n",
89+
"To: /content/Minori.jpg\n",
90+
"\r 0% 0.00/219k [00:00<?, ?B/s]\r100% 219k/219k [00:00<00:00, 82.5MB/s]\n",
91+
"Minori.jpg sample_data\n"
92+
],
93+
"name": "stdout"
94+
}
95+
]
96+
},
97+
{
98+
"cell_type": "markdown",
99+
"metadata": {
100+
"id": "a4O8VLuGETPQ",
101+
"colab_type": "text"
102+
},
103+
"source": [
104+
"**2. 掛載自己的 Google Drive**\n",
105+
"\n",
106+
" 透過 Google 提供的套件,我們可以讓 Colab 上的程式直接讀取自己的雲端硬碟。\n",
107+
" 這個方法的好處是只要檔案存在於自己的雲端硬碟,就隨時都可以直接存取;相對地,缺點就是使用者得手動將檔案加入,並且在程式運行時要輸入連結 Google Drive 所需要的授權碼。\n",
108+
"\n"
109+
]
110+
},
111+
{
112+
"cell_type": "code",
113+
"metadata": {
114+
"id": "CufnDQVQGkyH",
115+
"colab_type": "code",
116+
"outputId": "1abe1097-9be4-44eb-d937-ed8ea4eae166",
117+
"colab": {
118+
"base_uri": "https://localhost:8080/",
119+
"height": 127
120+
}
121+
},
122+
"source": [
123+
"# OpenCV 套件,和 Google Drive 無關\n",
124+
"import cv2\n",
125+
"# import Google Drive 套件\n",
126+
"from google.colab import drive\n",
127+
"# 將自己的雲端硬碟掛載上去\n",
128+
"drive.mount('/content/gdrive')\n",
129+
"# 透過 gdrive/My Drive/... 來存取檔案\n",
130+
"img = cv2.imread('gdrive/My Drive/Minori.jpg')"
131+
],
132+
"execution_count": 0,
133+
"outputs": [
134+
{
135+
"output_type": "stream",
136+
"text": [
137+
"Go to this URL in a browser: https://accounts.google.com/o/oauth2/auth?client_id=947318989803-6bn6qk8qdgf4n4g3pfee6491hc0brc4i.apps.googleusercontent.com&redirect_uri=urn%3aietf%3awg%3aoauth%3a2.0%3aoob&response_type=code&scope=email%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdocs.test%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive.photos.readonly%20https%3a%2f%2fwww.googleapis.com%2fauth%2fpeopleapi.readonly\n",
138+
"\n",
139+
"Enter your authorization code:\n",
140+
"··········\n",
141+
"Mounted at /content/gdrive\n"
142+
],
143+
"name": "stdout"
144+
}
145+
]
146+
},
147+
{
148+
"cell_type": "markdown",
149+
"metadata": {
150+
"id": "D0URgikZXl5I",
151+
"colab_type": "text"
152+
},
153+
"source": [
154+
"助教們提供的 sample code 是使用第一種方式來取得檔案,如果同學在實作作業時有需要,可以自行將 code 換成第二種方式。"
155+
]
156+
}
157+
]
158+
}

0 commit comments

Comments
 (0)