質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

1回答

764閲覧

物体検知SSDでの IndexErrorの対応

yokosawa123

総合スコア1

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2022/01/19 05:01

編集2022/01/19 05:03

前提・実現したいこと

私は、大学の卒業研究で物体検知(SSD)を用いてリンゴの周りにある葉を検知するプログラムを作成しています。
エラー分に該当する部分は、コードの一番下の方にあります。

発生している問題・エラーメッセージ

Traceback (most recent call last): File "葉検出プログラム.py", line 110, in <module> y_A = int(img_name_split3[1]) IndexError: list index out of range

該当のソースコード

python

1import glob 2import numpy as np 3from PIL import Image 4from keras.preprocessing import image 5from ssd import SSD300 6from ssd_training import MultiboxLoss 7from ssd_utils import BBoxUtility 8from scipy.misc import imread 9from keras.applications.imagenet_utils import preprocess_input 10import keras 11from ssd_utils import BBoxUtility 12import matplotlib.pyplot as plt 13import cv2 14import pickle 15import os 16 17#path_prefix = './VOCdevkit/VOC2007/JPEGImages/' 18 19#img_get = glob.glob('C:\\Users\\vive\\Desktop\\18ms611\\exp1\\exp15/Image/*.jpg') 20 21img_num = 'IMG_1070' 22alpha = 1.3 23size_of_img = 3024*4032 24csvfile = open('C:\\Users\\ユーザー名\\Desktop\\引継ぎ用\\葉検出システム\\葉検出に使うデータ\\葉検出結果\\'+img_num+'.csv','w') 25 26input_shape = (300, 300, 3) # 入力のサイズ 27NUM_CLASSES = 2 # 1クラス + その他 = 2クラス 28priors = pickle.load(open('C:\\Users\\ユーザー名\\Desktop\\引継ぎ用\\葉検出システム\\葉検出に使うデータ/prior_boxes_ssd300.pkl', 'rb')) # 入力画像のサイズを指定 29bbox_util = BBoxUtility(NUM_CLASSES,priors) 30model = SSD300(input_shape, num_classes=NUM_CLASSES) 31 32menseki_list = [] 33beta_list = [] 34 35model.load_weights('C:\\Users\\ユーザー名\\Desktop\\引継ぎ用\\葉検出システム\\葉検出に使うデータ//weights_SSD300.hdf5', by_name=True) 36 37for beta in np.arange(0.01,1.06,0.05): 38 39 All_img = Image.open('C:\\Users\\ユーザー名\\Desktop\\引継ぎ用\\検出結果\\Figure_1.jpg') 40 #All_img = All_img.rotate(-90,expand=True) 41 All_img = np.array(All_img) 42 43 All_img2 = Image.open('C:\\Users\\ユーザー名\\Desktop\\引継ぎ用\\検出結果\\Figure_1.jpg') 44 #All_img2 = All_img2.rotate(-90,expand=True) 45 All_img2 = np.array(All_img2) 46 47 All_img3 = Image.open('C:\\Users\\ユーザー名\\Desktop\\引継ぎ用\\検出結果\\Figure_1.jpg') 48 #All_img3 = All_img3.rotate(-90,expand=True) 49 All_img3 = np.array(All_img3) 50 51 img_get = glob.glob('C:\\Users\\ユーザー名\\Desktop\\引継ぎ用\\検出結果\\Figure_1.jpg') 52 #for x in range(0,7,1): 53 54 #if os.path.exists('C:\\Users\\user\\Desktop\\18ms611\\result20200517\\'+img_num+'_a'+str(alpha)+'_b'+str(beta))==0: 55 #os.makedirs('C:\\Users\\user\\Desktop\\18ms611\\result20200517\\'+img_num+'_a'+str(alpha)+'_b'+str(beta)) 56 57 apple_one_array = np.zeros((All_img.shape[0],All_img.shape[1])) 58 leef_point = [] 59 j=0 60 for jjj in range(len(img_get)): 61 inputs = [] 62 images = [] 63 64 img_path=img_get[jjj] 65 img = image.load_img(img_path, target_size=(300, 300)) 66 img = image.img_to_array(img) 67 68 img2 = image.load_img(img_path) 69 img2 = image.img_to_array(img2) 70 71 #img2=all_img.rotate(-90,expand=True) 72 images.append(imread(img_path)) 73 inputs.append(img.copy()) 74 inputs = preprocess_input(np.array(inputs)) 75 76 preds = model.predict(inputs, batch_size=1, verbose=1) 77 results = bbox_util.detection_out(preds) 78 79 for i, img in enumerate(images): 80 # Parse the outputs. 81 det_label = results[i][:, 0] 82 det_conf = results[i][:, 1] 83 det_xmin = results[i][:, 2] 84 det_ymin = results[i][:, 3] 85 det_xmax = results[i][:, 4] 86 det_ymax = results[i][:, 5] 87 88 # Get detections with confidence higher than 0.6. 89 top_indices = [i for i, conf in enumerate(det_conf) if conf >= 0.6] 90 91 top_conf = det_conf[top_indices] 92 top_label_indices = det_label[top_indices].tolist() 93 top_xmin = det_xmin[top_indices] 94 top_ymin = det_ymin[top_indices] 95 top_xmax = det_xmax[top_indices] 96 top_ymax = det_ymax[top_indices] 97 98 colors = plt.cm.hsv(np.linspace(0, 1, NUM_CLASSES)).tolist() 99 100 #plt.imshow(img / 255.) 101 currentAxis = plt.gca() 102 img_name_split1=img_get[jjj].split('x') 103 img_name_split2=img_name_split1[-1].split('.jpg') 104 img_name_split3=img_name_split2[0].split('y') 105 y_A = int(img_name_split3[1])   ****ここがエラーの該当する部分です。**** 106 x_A = int(img_name_split3[0]) 107 apple_one_array[y_A:y_A+img.shape[0],x_A:x_A+img.shape[1]] = apple_one_array[y_A:y_A+img.shape[0],x_A:x_A+img.shape[1]] + 1 108 109 110 111

試したこと

[]の中の数字を変えてみましたが、うまくいきませんでした。

補足情報(FW/ツールのバージョンなど)

何の範囲がおかしくてこのようなエラーが出ているかもわかりません。
知識のある方ご教授よろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

img_name_split3の要素数が1個以下になっていると提示エラーが発生します。
img_name_split1img_name_split3に格納される値(ファイル名)およびそれぞれの処理内容が意図したものになっているかを確認しましょう。

投稿2022/01/19 05:59

can110

総合スコア38256

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yokosawa123

2022/01/21 03:44

回答ありがとうございます。img_name_split1~img_name_split3について調べてみます。 エラーの解決にたどり着けなかった場合また質問させていただきます。 よろしくお願いいたします。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問