質問編集履歴
1
エラーが出るまでの全てのプログラムを書きました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -2,6 +2,176 @@
|
|
2
2
|
|
3
3
|
```python
|
4
4
|
|
5
|
+
!pip install icrawler
|
6
|
+
|
7
|
+
```
|
8
|
+
|
9
|
+
```python
|
10
|
+
|
11
|
+
# from icrawler.builtin import GoogleImageCrawler
|
12
|
+
|
13
|
+
# もつ煮の画像を100枚取得
|
14
|
+
|
15
|
+
# crawler = GoogleImageCrawler(storage={"root_dir": "moosuni"})
|
16
|
+
|
17
|
+
# crawler.crawl(keyword="もつ煮", max_num=100)
|
18
|
+
|
19
|
+
from icrawler.builtin import BingImageCrawler
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
# もつ煮の画像を100枚取得
|
24
|
+
|
25
|
+
crawler = BingImageCrawler(storage={"root_dir": "motsuni"})
|
26
|
+
|
27
|
+
crawler.crawl(keyword="もつ煮", max_num=100)
|
28
|
+
|
29
|
+
```
|
30
|
+
|
31
|
+
```python
|
32
|
+
|
33
|
+
# ナポリタンの画像を100枚取得
|
34
|
+
|
35
|
+
# crawler = GoogleImageCrawler(storage={"root_dir": "naporitan"})
|
36
|
+
|
37
|
+
# crawler.crawl(keyword="ナポリタン", max_num=100)
|
38
|
+
|
39
|
+
from icrawler.builtin import BingImageCrawler
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
# ナポリタンの画像を100枚取得
|
44
|
+
|
45
|
+
crawler = BingImageCrawler(storage={"root_dir": "naporitan"})
|
46
|
+
|
47
|
+
crawler.crawl(keyword="ナポリタン", max_num=100)
|
48
|
+
|
49
|
+
```
|
50
|
+
|
51
|
+
```python
|
52
|
+
|
53
|
+
# ラーメンの画像を100枚取得
|
54
|
+
|
55
|
+
# crawler = GoogleImageCrawler(storage={"root_dir": "ra-men"})
|
56
|
+
|
57
|
+
# crawler.crawl(keyword="ラーメン", max_num=100)
|
58
|
+
|
59
|
+
from icrawler.builtin import BingImageCrawler
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
# ラーメンの画像を100枚取得
|
64
|
+
|
65
|
+
crawler = BingImageCrawler(storage={"root_dir": "ra-men"})
|
66
|
+
|
67
|
+
crawler.crawl(keyword="醤油ラーメン", max_num=100)
|
68
|
+
|
69
|
+
```
|
70
|
+
|
71
|
+
```python
|
72
|
+
|
73
|
+
from PIL import Image
|
74
|
+
|
75
|
+
import os, glob
|
76
|
+
|
77
|
+
import numpy as np
|
78
|
+
|
79
|
+
from PIL import ImageFile
|
80
|
+
|
81
|
+
# IOError: image file is truncated (0 bytes not processed)回避のため
|
82
|
+
|
83
|
+
ImageFile.LOAD_TRUNCATED_IMAGES = True
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
classes = ["motsuni", "naporitan", "ra-men"]
|
88
|
+
|
89
|
+
num_classes = len(classes)
|
90
|
+
|
91
|
+
image_size = 64
|
92
|
+
|
93
|
+
num_testdata = 25
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
X_train = []
|
98
|
+
|
99
|
+
X_test = []
|
100
|
+
|
101
|
+
y_train = []
|
102
|
+
|
103
|
+
y_test = []
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
for index, classlabel in enumerate(classes):
|
108
|
+
|
109
|
+
photos_dir = "./" + classlabel
|
110
|
+
|
111
|
+
files = glob.glob(photos_dir + "/*.jpg")
|
112
|
+
|
113
|
+
for i, file in enumerate(files):
|
114
|
+
|
115
|
+
image = Image.open(file)
|
116
|
+
|
117
|
+
image = image.convert("RGB")
|
118
|
+
|
119
|
+
image = image.resize((image_size, image_size))
|
120
|
+
|
121
|
+
data = np.asarray(image)
|
122
|
+
|
123
|
+
if i < num_testdata:
|
124
|
+
|
125
|
+
X_test.append(data)
|
126
|
+
|
127
|
+
y_test.append(index)
|
128
|
+
|
129
|
+
else:
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
for angle in range(-20, 20, 5):
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
img_r = image.rotate(angle)
|
138
|
+
|
139
|
+
data = np.asarray(img_r)
|
140
|
+
|
141
|
+
X_train.append(data)
|
142
|
+
|
143
|
+
y_train.append(index)
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
img_trains = img_r.transpose(Image.FLIP_LEFT_RIGHT)
|
148
|
+
|
149
|
+
data = np.asarray(img_trains)
|
150
|
+
|
151
|
+
X_train.append(data)
|
152
|
+
|
153
|
+
y_train.append(index)
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
X_train = np.array(X_train)
|
158
|
+
|
159
|
+
X_test = np.array(X_test)
|
160
|
+
|
161
|
+
y_train = np.array(y_train)
|
162
|
+
|
163
|
+
y_test = np.array(y_test)
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
xy = (X_train, X_test, y_train, y_test)
|
168
|
+
|
169
|
+
np.save("./ryouri.npy", xy)
|
170
|
+
|
171
|
+
```
|
172
|
+
|
173
|
+
```python
|
174
|
+
|
5
175
|
from keras.models import Sequential
|
6
176
|
|
7
177
|
from keras.layers import Conv2D, MaxPooling2D
|
@@ -16,7 +186,7 @@
|
|
16
186
|
|
17
187
|
|
18
188
|
|
19
|
-
classes = ["motsuni", "naporitan", "
|
189
|
+
classes = ["motsuni", "naporitan", "ra-men"]
|
20
190
|
|
21
191
|
num_classes = len(classes)
|
22
192
|
|
@@ -206,4 +376,8 @@
|
|
206
376
|
|
207
377
|
|
208
378
|
|
379
|
+
使う画像は、上のコードでウェブから拾ってきています。
|
380
|
+
|
381
|
+
|
382
|
+
|
209
383
|
3つ以上のファイルを機械学習するためにはどのようにすればいいか、教えていただけると嬉しいです。
|