if文を利用して認識した観光地の地図(現在はriturin.pngだけ)を表示できるプログラムを作成しています。
if分の書き方を確認し、作成してみたのですが表示されません。
アドバイスお願いします。
python
1import cnn_model 2import keras 3import matplotlib.pyplot as plt 4import numpy as np 5from PIL import Image 6import matplotlib.pyplot as plt 7 8 9im_rows = 32 # 画像の縦ピクセルサイズ 10im_cols = 32 # 画像の横ピクセルサイズ 11im_color = 3 # 画像の色空間 12in_shape = (im_rows, im_cols, im_color) 13nb_classes = 8 14 15LABELS = ["栗林公園", "金刀比羅宮", "瀬戸大橋","丸亀城","伊勢神宮","スカイツリー","レインボーブリッジ","高松城"] 16CALORIES = [588, 118, 648] 17 18# 保存したCNNモデルを読み込む 19model = cnn_model.get_model(in_shape, nb_classes) 20model.load_weights('./image/photos-model.hdf5') 21 22def check_photo(path): 23 # 画像を読み込む 24 img = Image.open(path) 25 img = img.convert("RGB") # 色空間をRGBに 26 img = img.resize((im_cols, im_rows)) # サイズ変更 27 plt.imshow(img) 28 plt.show() 29 # データに変換 30 x = np.asarray(img) 31 x = x.reshape(-1, im_rows, im_cols, im_color) 32 x = x / 255 33 34 # 予測 35 pre = model.predict([x])[0] 36 idx = pre.argmax() 37 per = int(pre[idx] * 100) 38 return (idx, per) 39 40def check_photo_str(path): 41 idx, per = check_photo(path) 42 # 答えを表示 43 print("この写真は、", LABELS[idx]) 44 print("可能性は、", per, "%") 45 46if LABELS=="栗林公園": 47 im = Image.open("./riturin.png") 48 im_list = np.asarray(im) 49 plt.imshow(im_list) 50 plt.show() 51 52if __name__ == '__main__': 53 check_photo_str('riturin.jpg') 54 check_photo_str('kkk.jpg') 55 check_photo_str('brige.jpg') 56 check_photo_str('setooohasi.jpg') 57 check_photo_str('SetoBridge1.jpg')
判定は行えます。画像表示を行いたいのは、栗林公園と解答を得られた時に所在地の地図です。
if文を用いて書いたところはこの部分です。
python
1if LABELS=="栗林公園": 2 im = Image.open("./riturin.png") 3 im_list = np.asarray(im) 4 plt.imshow(im_list) 5 plt.show()
プログラムの修正を行いました。
修正箇所
python
1import cnn_model 2import keras 3import matplotlib.pyplot as plt 4import numpy as np 5from PIL import Image 6import matplotlib.pyplot as plt 7 8 9im_rows = 32 # 画像の縦ピクセルサイズ 10im_cols = 32 # 画像の横ピクセルサイズ 11im_color = 3 # 画像の色空間 12in_shape = (im_rows, im_cols, im_color) 13nb_classes = 8 14 15LABELS = ["栗林公園", "金刀比羅宮", "瀬戸大橋","丸亀城","伊勢神宮","スカイツリー","レインボーブリッジ","高松城"] 16CALORIES = [588, 118, 648] 17 18# 保存したCNNモデルを読み込む 19model = cnn_model.get_model(in_shape, nb_classes) 20model.load_weights('./image/photos-model.hdf5') 21 22def check_photo(path): 23 # 画像を読み込む 24 img = Image.open(path) 25 img = img.convert("RGB") # 色空間をRGBに 26 img = img.resize((im_cols, im_rows)) # サイズ変更 27 plt.imshow(img) 28 plt.show() 29 # データに変換 30 x = np.asarray(img) 31 x = x.reshape(-1, im_rows, im_cols, im_color) 32 x = x / 255 33 34 # 予測 35 pre = model.predict([x])[0] 36 idx = pre.argmax() 37 per = int(pre[idx] * 100) 38 return (idx, per) 39 40def check_photo_str(path): 41 idx, per = check_photo(path) 42 # 答えを表示 43 print("この写真は、", LABELS[idx]) 44 print("可能性は、", per, "%") 45 46if LABELS[idx]=="栗林公園": 47 im = Image.open("./riturin.png") 48 im_list = np.asarray(im) 49 plt.imshow(im_list) 50 plt.show() 51 52if __name__ == '__main__': 53 check_photo_str('riturin.jpg') 54 check_photo_str('kkk.jpg') 55 check_photo_str('brige.jpg') 56 check_photo_str('setooohasi.jpg') 57 check_photo_str('SetoBridge1.jpg')
python
1#エラー文 2runfile('/Users/name/my_photo.py') 3Traceback (most recent call last): 4 5 File "<ipython-input-6-738e553c0889>", line 1, in <module> 6 runfile('/Users/name/my_photo.py') 7 8 File "/Users/name/anaconda3/envs/python35/lib/python3.6/site-packages/spyder_kernels/customize/spydercustomize.py", line 668, in runfile 9 execfile(filename, namespace) 10 11 File "/Users/name/anaconda3/envs/python35/lib/python3.6/site-packages/spyder_kernels/customize/spydercustomize.py", line 108, in execfile 12 exec(compile(f.read(), filename, 'exec'), namespace) 13 14 File "/Users/nakayakenta/my_photo.py", line 54, in <module> 15 if LABELS[idx]=="栗林公園": 16 17NameError: name 'idx' is not defined
補足情報
python3.6.6
mac OS Mojava
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/05 23:10
2019/01/06 02:13
2019/01/06 03:36
2019/01/06 07:34
2019/01/06 11:58