質問編集履歴

1

プログラムの修正

2019/01/06 03:36

投稿

fdd
fdd

スコア28

test CHANGED
File without changes
test CHANGED
@@ -142,7 +142,165 @@
142
142
 
143
143
  ```
144
144
 
145
-
145
+ プログラムの修正を行いました。
146
+
147
+ **修正箇所**
148
+
149
+ ```python
150
+
151
+ import cnn_model
152
+
153
+ import keras
154
+
155
+ import matplotlib.pyplot as plt
156
+
157
+ import numpy as np
158
+
159
+ from PIL import Image
160
+
161
+ import matplotlib.pyplot as plt
162
+
163
+
164
+
165
+
166
+
167
+ im_rows = 32 # 画像の縦ピクセルサイズ
168
+
169
+ im_cols = 32 # 画像の横ピクセルサイズ
170
+
171
+ im_color = 3 # 画像の色空間
172
+
173
+ in_shape = (im_rows, im_cols, im_color)
174
+
175
+ nb_classes = 8
176
+
177
+
178
+
179
+ LABELS = ["栗林公園", "金刀比羅宮", "瀬戸大橋","丸亀城","伊勢神宮","スカイツリー","レインボーブリッジ","高松城"]
180
+
181
+ CALORIES = [588, 118, 648]
182
+
183
+
184
+
185
+ # 保存したCNNモデルを読み込む
186
+
187
+ model = cnn_model.get_model(in_shape, nb_classes)
188
+
189
+ model.load_weights('./image/photos-model.hdf5')
190
+
191
+
192
+
193
+ def check_photo(path):
194
+
195
+ # 画像を読み込む
196
+
197
+ img = Image.open(path)
198
+
199
+ img = img.convert("RGB") # 色空間をRGBに
200
+
201
+ img = img.resize((im_cols, im_rows)) # サイズ変更
202
+
203
+ plt.imshow(img)
204
+
205
+ plt.show()
206
+
207
+ # データに変換
208
+
209
+ x = np.asarray(img)
210
+
211
+ x = x.reshape(-1, im_rows, im_cols, im_color)
212
+
213
+ x = x / 255
214
+
215
+
216
+
217
+ # 予測
218
+
219
+ pre = model.predict([x])[0]
220
+
221
+ idx = pre.argmax()
222
+
223
+ per = int(pre[idx] * 100)
224
+
225
+ return (idx, per)
226
+
227
+
228
+
229
+ def check_photo_str(path):
230
+
231
+ idx, per = check_photo(path)
232
+
233
+ # 答えを表示
234
+
235
+ print("この写真は、", LABELS[idx])
236
+
237
+ print("可能性は、", per, "%")
238
+
239
+
240
+
241
+ if LABELS[idx]=="栗林公園":
242
+
243
+ im = Image.open("./riturin.png")
244
+
245
+ im_list = np.asarray(im)
246
+
247
+ plt.imshow(im_list)
248
+
249
+ plt.show()
250
+
251
+
252
+
253
+ if __name__ == '__main__':
254
+
255
+ check_photo_str('riturin.jpg')
256
+
257
+ check_photo_str('kkk.jpg')
258
+
259
+ check_photo_str('brige.jpg')
260
+
261
+ check_photo_str('setooohasi.jpg')
262
+
263
+ check_photo_str('SetoBridge1.jpg')
264
+
265
+ ```
266
+
267
+ ```python
268
+
269
+ #エラー文
270
+
271
+ runfile('/Users/name/my_photo.py')
272
+
273
+ Traceback (most recent call last):
274
+
275
+
276
+
277
+ File "<ipython-input-6-738e553c0889>", line 1, in <module>
278
+
279
+ runfile('/Users/name/my_photo.py')
280
+
281
+
282
+
283
+ File "/Users/name/anaconda3/envs/python35/lib/python3.6/site-packages/spyder_kernels/customize/spydercustomize.py", line 668, in runfile
284
+
285
+ execfile(filename, namespace)
286
+
287
+
288
+
289
+ File "/Users/name/anaconda3/envs/python35/lib/python3.6/site-packages/spyder_kernels/customize/spydercustomize.py", line 108, in execfile
290
+
291
+ exec(compile(f.read(), filename, 'exec'), namespace)
292
+
293
+
294
+
295
+ File "/Users/nakayakenta/my_photo.py", line 54, in <module>
296
+
297
+ if LABELS[idx]=="栗林公園":
298
+
299
+
300
+
301
+ NameError: name 'idx' is not defined
302
+
303
+ ```
146
304
 
147
305
  補足情報
148
306