###前提・実現したいこと
Pythonで掲題のエラーが発生してしまい困っております。皆様のお知恵を拝借したく。何卒宜しくお願いします。
###発生している問題・エラーメッセージ
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-274-1ab25a298417> in <module>() ----> 1 mask_L,mask_R = initial_mask(lane_line_gb,window_size=40) 2 3 plt.subplot(1,2,1) 4 plt.imshow(mask_L,cmap='gray') 5 plt.axis('off') <ipython-input-273-f5ded2734466> in initial_mask(img, window_size) 12 mov_filtsize = img_size[1]/50. 13 mean_lane = np.mean(img[img_size[1]//2:,:],axis=0) ---> 14 mean_lane = moving_average(mean_lane,mov_filtsize) 15 16 #find peak indexes from histgram <ipython-input-273-f5ded2734466> in moving_average(a, n) 4 # Moving average 5 ret = np.cumsum(a, dtype=float) ----> 6 ret[n:] = ret[n:] - ret[:-n] 7 return ret[n - 1:] / n 8 TypeError: slice indices must be integers or None or have an __index__ method
###該当のソースコード(Python3.5)
Python3.5
1from scipy.signal import find_peaks_cwt 2 3def moving_average(a, n=3): 4 # Moving average 5 ret = np.cumsum(a, dtype=float) 6 ret[n:] = ret[n:] - ret[:-n] 7 return ret[n - 1:] // n 8 9def initial_mask(img,window_size): 10 img = gaussian_blur(img,5) 11 img_size = np.shape(img) 12 mov_filtsize = img_size[1]/50. 13 mean_lane = np.mean(img[img_size[1]//2:,:],axis=0) 14 mean_lane = moving_average(mean_lane,mov_filtsize) 15 16 #find peak indexes from histgram 17 indexes = find_peaks_cwt(mean_lane,[100],max_distances=[800]) 18 19 val_ind = np.array([mean_lane[indexes[i]] for i in range(len(indexes)) ]) 20 ind_sorted = np.argsort(-val_ind) 21 22 ind_peakR = indexes[ind_sorted[0]] 23 ind_peakL = indexes[ind_sorted[1]] 24 25 # exchange indexes if under condition 26 if ind_peakR<ind_peakL: 27 28 ind_peakR,ind_peakL = ind_peakL,ind_peakR 29 30 # take windows from each index 31 ind_min_L = ind_peakL-window_size 32 ind_max_L = ind_peakL+window_size 33 34 ind_min_R = ind_peakR-window_size 35 ind_max_R = ind_peakR+window_size 36 37 mask_L = np.zeros_like(img) 38 mask_R = np.zeros_like(img) 39 40 ind_peakR_prev = ind_peakR 41 ind_peakL_prev = ind_peakL 42 43 # Split image into 11 parts and compute histogram on each part 44 45 for i in range(11): 46 img_y1 = img_size[0]-img_size[0]*i/11 47 img_y2 = img_size[0]-img_size[0]*(i+1)/11 48 49 mean_lane_y = np.mean(img[img_y2:img_y1,:],axis=0) 50 mean_lane_y = moving_average(mean_lane_y,mov_filtsize) 51 indexes = find_peaks_cwt(mean_lane_y,[100], max_distances=[800]) 52 53 # if the indexes are more than 2 (both side peak L,R) 54 if len(indexes)>1.5: 55 val_ind = np.array([mean_lane[indexes[i]] for i in range(len(indexes)) ]) 56 ind_sorted = np.argsort(-val_ind) 57 58 ind_peakR = indexes[ind_sorted[0]] 59 ind_peakL = indexes[ind_sorted[1]] 60 if ind_peakR<ind_peakL: 61 ind_peakR,ind_peakL = ind_peakL,ind_peakR 62 63 else: 64 # if one peak is found 65 if len(indexes)==1: 66 #found right lane 67 if (np.abs(indexes[0]-ind_peakR_prev)<np.abs(indexes[0]-ind_peakL_prev)): 68 ind_peakR = indexes[0] 69 ind_peakL = ind_peakL_prev 70 # found left lane 71 else: 72 ind_peakL = indexes[0] 73 ind_peakR = ind_peakR_prev 74 # If no pixels are found, use previous ones. 75 else: 76 ind_peakL = ind_peakL_prev 77 ind_peakR = ind_peakR_prev 78 79 80 # If new center is more than 80pixels away, use previous 81 # Outlier rejection 82 if np.abs(ind_peakL-ind_peakL_prev)>=80: 83 ind_peakL = ind_peakL_prev 84 85 if np.abs(ind_peakR-ind_peakR_prev)>=80: 86 ind_peakR = ind_peakR_prev 87 88 89 90 mask_L[img_y2:img_y1,ind_peakL-window_size:ind_peakL+window_size] = 1. 91 mask_R[img_y2:img_y1,ind_peakR-window_size:ind_peakR+window_size] = 1. 92 93 ind_peakL_prev = ind_peakL 94 ind_peakR_prev = ind_peakR 95 96 return mask_L,mask_R 97 98
Python3.5
1mask_L,mask_R = initial_mask(lane_line_gb,window_size=40) 2 3plt.subplot(1,2,1) 4plt.imshow(mask_L,cmap='gray') 5plt.axis('off') 6plt.title('Left lane') 7plt.subplot(1,2,2) 8plt.imshow(mask_R,cmap='gray') 9plt.axis('off') 10plt.title('Right lane'); 11 12
###試したこと
1.Stackoverflowを参考にしようとしましたが、解決方法が分かりませんでした。
URL:https://stackoverflow.com/questions/20733156/slice-indices-must-be-integers-or-none-or-have-index-method
###補足情報(言語/FW/ツール等のバージョンなど)
Python 3.5.2
-以上-
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。