LBPを画像から特徴量を抽出してSVMで学習をさせたく、WEBで調べてコードを作ってはみたんですが実行をしてもエラーが起きてしまいます。いろいろ試してはいるのですが初めてみるエラーなのでやりかたが分かりません。
教えていただけると嬉しいです
# 特徴量抽出器 # 特徴量抽出器 def create_images_array(load_img_paths): imgs=[] #LBP特徴量パラメータ points = 8 radius = 1 win_size = (64,64) #画像群の配列を生成 for load_img_path in tqdm(load_img_paths): #画像をロードし、グレースケール変換 #色反転、64*64にリサイズ、1次元配列に変換 img = cv2.imread(load_img_path) gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) gray = cv2.resize(gray,win_size) lbp = feature.local_binary_pattern(gray, points, radius, method='uniform') imgs.append(lbp) return np.array(imgs, np.float32) def main(): t1 = time.time() # 学習用の画像ファイルの格納先 LOAD_TRAIN_IMG1S_PATH = './preprocess_images/case3/train/T1-FE/*' LOAD_TRAIN_IMG2S_PATH = './preprocess_images/case3/train/T2-FE/*' LOAD_TRAIN_IMG3S_PATH = './preprocess_images/case3/train/T3-FE/*' LOAD_TRAIN_IMG4S_PATH = './preprocess_images/case3/train/T4-FE/*' LOAD_TRAIN_IMG5S_PATH = './preprocess_images/case3/train/T5-FE/*' LOAD_TRAIN_IMG6S_PATH = './preprocess_images/case3/train/T6-FE/*' LOAD_TRAIN_IMG7S_PATH = './preprocess_images/case3/train/T7-FE/*' LOAD_TRAIN_IMG8S_PATH = './preprocess_images/case3/train/T8-FE/*' LOAD_TRAIN_IMG9S_PATH = './preprocess_images/case3/train/T1-W/*' LOAD_TRAIN_IMG10S_PATH = './preprocess_images/case3/train/T2-W/*' LOAD_TRAIN_IMG11S_PATH = './preprocess_images/case3/train/T3-W/*' LOAD_TRAIN_IMG12S_PATH = './preprocess_images/case3/train/T4-W/*' LOAD_TRAIN_IMG13S_PATH = './preprocess_images/case3/train/T5-W/*' LOAD_TRAIN_IMG14S_PATH = './preprocess_images/case3/train/T6-W/*' LOAD_TRAIN_IMG15S_PATH = './preprocess_images/case3/train/T7-W/*' LOAD_TRAIN_IMG16S_PATH = './preprocess_images/case3/train/T8-W/*' # 作成した学習モデルの保存先 SAVE_TRAINED_DATA_PATH = './casestudy/case3/svm_trained_data.xml' # 学習用の画像ファイルのパスを取得 load_img1_paths = glob.glob(LOAD_TRAIN_IMG1S_PATH) load_img2_paths = glob.glob(LOAD_TRAIN_IMG2S_PATH) load_img3_paths = glob.glob(LOAD_TRAIN_IMG3S_PATH) load_img4_paths = glob.glob(LOAD_TRAIN_IMG4S_PATH) load_img5_paths = glob.glob(LOAD_TRAIN_IMG5S_PATH) load_img6_paths = glob.glob(LOAD_TRAIN_IMG6S_PATH) load_img7_paths = glob.glob(LOAD_TRAIN_IMG7S_PATH) load_img8_paths = glob.glob(LOAD_TRAIN_IMG8S_PATH) load_img9_paths = glob.glob(LOAD_TRAIN_IMG9S_PATH) load_img10_paths = glob.glob(LOAD_TRAIN_IMG10S_PATH) load_img11_paths = glob.glob(LOAD_TRAIN_IMG11S_PATH) load_img12_paths = glob.glob(LOAD_TRAIN_IMG12S_PATH) load_img13_paths = glob.glob(LOAD_TRAIN_IMG13S_PATH) load_img14_paths = glob.glob(LOAD_TRAIN_IMG14S_PATH) load_img15_paths = glob.glob(LOAD_TRAIN_IMG15S_PATH) load_img16_paths = glob.glob(LOAD_TRAIN_IMG16S_PATH) # 学習用の画像ファイルをロードし特徴量抽出 imgs1 = create_images_array(load_img1_paths) imgs2 = create_images_array(load_img2_paths) imgs3 = create_images_array(load_img3_paths) imgs4 = create_images_array(load_img4_paths) imgs5 = create_images_array(load_img5_paths) imgs6 = create_images_array(load_img6_paths) imgs7 = create_images_array(load_img7_paths) imgs8 = create_images_array(load_img8_paths) imgs9 = create_images_array(load_img9_paths) imgs10 = create_images_array(load_img10_paths) imgs11 = create_images_array(load_img11_paths) imgs12 = create_images_array(load_img12_paths) imgs13 = create_images_array(load_img13_paths) imgs14 = create_images_array(load_img14_paths) imgs15 = create_images_array(load_img15_paths) imgs16 = create_images_array(load_img16_paths) imgs = np.r_[imgs1, imgs2, imgs3, imgs4, imgs5, imgs6, imgs7, imgs8, imgs9, imgs10, imgs11, imgs12, imgs13, imgs14, imgs15, imgs16] # 正解ラベルを生成imgs ''' #1パターン目:T1-FE,T2-FE,・・・T1-W,T2-W,・・・全部判別 labels1 = np.full(len(load_img1_paths), 1, np.int32) labels2 = np.full(len(load_img2_paths), 2, np.int32) labels3 = np.full(len(load_img3_paths), 3, np.int32) labels4 = np.full(len(load_img4_paths), 4, np.int32) labels5 = np.full(len(load_img5_paths), 5, np.int32) labels6 = np.full(len(load_img6_paths), 6, np.int32) labels7 = np.full(len(load_img7_paths), 7, np.int32) labels8 = np.full(len(load_img8_paths), 8, np.int32) labels9 = np.full(len(load_img9_paths), 9, np.int32) labels10 = np.full(len(load_img10_paths), 10, np.int32) labels11 = np.full(len(load_img11_paths), 11, np.int32) labels12 = np.full(len(load_img12_paths), 12, np.int32) labels13 = np.full(len(load_img13_paths), 13, np.int32) labels14 = np.full(len(load_img14_paths), 14, np.int32) labels15 = np.full(len(load_img15_paths), 15, np.int32) labels16 = np.full(len(load_img16_paths), 16, np.int32) labels = np.array([np.r_[labels1, labels2, labels3, labels4, labels5, labels6, labels7, labels8, labels9, labels10, labels11, labels12, labels13, labels14, labels15, labels16]]) # SVMで学習モデルの作成(カーネル:LINEAR 線形, gamma:1, C:1) svm = cv2.ml.SVM_create() svm.setType(cv2.ml.SVM_C_SVC) svm.setKernel(cv2.ml.SVM_LINEAR) svm.setGamma(1) svm.setC(1) svm.setTermCriteria((cv2.TERM_CRITERIA_COUNT, 100, 1.e-06)) svm.train(imgs, cv2.ml.ROW_SAMPLE, labels) # 学習結果を保存 svm.save(SAVE_TRAINED_DATA_PATH) if __name__ == '__main__': main()
#エラーコード
svm.train(imgs, cv2.ml.ROW_SAMPLE, labels) error: OpenCV(3.4.2) C:\projects\opencv-python\opencv\modules\ml\src\data.cpp:259: error: (-215:Assertion failed) samples.type() == 5 || samples.type() == 4 in function 'cv::ml::TrainDataImpl::setData'
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/12/07 04:15