前提・実現したいこと
python初心者です。
scikit-learnを用いて自分の顔を学習させ、動画から人の顔を認識し、自分の顔の写真のみディレクトリに保存するということを行いないたいのですが、以下のエラーコードがでてしまい、処理が行えません。
発生している問題・エラーメッセージ
File "C:/Users/hoge/PycharmProjects/resarch372/20191029/self/顔-個人認証.py", line 48, in <module> pred_y = clf.predict([img_data]) File "C:\Users\hoge\PycharmProjects\resarch372\venv\lib\site-packages\sklearn\ensemble\forest.py", line 545, in predict proba = self.predict_proba(X) 顔の座標= 15 167 169 169 File "C:\Users\hoge\PycharmProjects\resarch372\venv\lib\site-packages\sklearn\ensemble\forest.py", line 588, in predict_proba X = self._validate_X_predict(X) File "C:\Users\hoge\PycharmProjects\resarch372\venv\lib\site-packages\sklearn\ensemble\forest.py", line 359, in _validate_X_predict return self.estimators_[0]._validate_X_predict(X, check_input=True) File "C:\Users\hoge\PycharmProjects\resarch372\venv\lib\site-packages\sklearn\tree\tree.py", line 402, in _validate_X_predict % (self.n_features_, n_features)) ValueError: Number of features of the model must match the input. Model n_features is 10000 and input n_features is 30000
該当のソースコード
python
1#顔の学習 2import cv2 3import os, glob 4from PIL import Image 5from sklearn.model_selection import train_test_split 6from sklearn import datasets,metrics 7from sklearn.ensemble import RandomForestClassifier 8from sklearn.metrics import accuracy_score 9from sklearn.externals import joblib 10 11img_size = (100, 100) 12 13path = os.path.dirname(os.path.abspath(__file__)) 14path_ore = path + '/ore' 15path_hoka = path + '/hoka' 16x = [] 17y = [] 18 19def read_dir(path, label): 20 files = glob.glob(path + "/*.jpg") 21 for f in files: 22 img = cv2.imread(f) 23 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 24 gray = cv2.GaussianBlur(gray, (9, 9), 0) 25 img_b = cv2.threshold(gray, 100, 255, cv2.THRESH_BINARY)[1] 26 img_b = cv2.resize(img_b, img_size) 27 img_data = img_b.reshape(-1, ) 28 x.append(img_data) 29 y.append(label) 30 31read_dir(path_hoka, 0) 32read_dir(path_ore, 1) 33 34x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2) 35 36clf = RandomForestClassifier() 37clf.fit(x_train,y_train) 38 39y_pred = clf.predict(x_test) 40print(accuracy_score(y_test, y_pred)) 41 42joblib.dump(clf, 'ore.pkl') 43 44 45#動画からの検出 46 47import cv2, os, copy 48from sklearn.externals import joblib 49 50clf = joblib.load("ore.pkl") 51output_dir = "./ore-kojinn" 52img_last = None 53img_c = None 54count = 0 55frame_count = 0 56no = 0 57 58cascade_file = ('haarcascade_frontalface_alt.xml') 59cascade = cv2.CascadeClassifier(cascade_file) 60 61 62if not os.path.isdir(output_dir): 63 os.mkdir(output_dir) 64 65cap = cv2.VideoCapture("nakayosi.mp4") 66while True: 67 ore,frame = cap.read() 68 if not ore: 69 break 70 frame = cv2.resize(frame,(600,400)) 71 frame2 = copy.copy(frame) 72 frame_count +=1 73 74 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 75 gray = cv2.GaussianBlur(gray, (15,15), 0) 76 img_b = cv2.threshold(gray, 127,255,cv2.THRESH_BINARY)[1] 77 78 # 顔 79 if img_c is None: 80 img_c = img_b 81 continue 82 83 face_list = cascade.detectMultiScale(frame, minSize=(100, 100)) 84 85 for (x, y, w, h) in face_list: 86 87 print("顔の座標=", x, y, w, h) 88 red = (0, 0, 255) 89 cv2.rectangle(frame, (x, y), (x + w, y + h), red, thickness=2) 90 img_c = frame[y:y + h, x:x + w] 91 img_d = cv2.resize(img_c, (100, 100)) 92 img_data = img_d.reshape(-1,) 93 94 pred_y = clf.predict([img_data]) 95 96 if pred_y[0] == 1: 97 outfile = output_dir + "/" + str(no) + ".jpg" 98 cv2.imwrite(outfile, img_c) 99 no += 1 100 101 cv2.imshow("douga", frame2) 102 103 104 105 img_c = img_b 106 107 if cv2.waitKey(1) == 13: 108 break 109cap.release() 110cv2.destroyAllWindows() 111print("ok", no,"/",frame_count)
試したこと
学習データの次元数が問題だと考えたので、resizeの関数をいろいろ変えてみたりしたのですが、同じようなエラーばかり出てしまい、どのようにすればよいかわかりません...
補足情報(FW/ツールのバージョンなど)
python3.7.5
エラーメッセージは全文掲載いただいた方が回答がつきやすくなるかと思います。
回答1件
あなたの回答
tips
プレビュー