私はscikit-learnを使用して顔認識を実行しています。私は、提供された例hereを試して、うまくいきます。私は今、顔の認識をテストするために画像からなる自分のカスタムデータを使用しようとしています。私はhereから顔データベースを使用しています。私は今、これらの画像をトレーニングセットとして私のプログラムにロードすることに固執しています。私はすべての画像をロードするには、以下のコードを使用しています。この後Python scikit-learnはカスタムトレーニングデータを提供します
# get the training data
def read_images(path, sz=None):
"""Reads the images in a given folder, resizes images on the fly if size is given.
Args:
path: Path to a folder with subfolders representing the subjects (persons).
sz: A tuple with the size Resizes
Returns:
A list [X,y]
X: The images, which is a Python list of numpy arrays.
y: The corresponding labels (the unique number of the subject, person) in a Python list.
"""
c = 0
X, y = [], []
for dirname, dirnames, filenames in os.walk(path):
for subdirname in dirnames:
subject_path = os.path.join(dirname, subdirname)
for filename in os.listdir(subject_path):
try:
im = Image.open(os.path.join(subject_path, filename))
im = im.convert("L")
# resize to given size (if given)
if (sz is not None):
im = im.resize(sz, Image.ANTIALIAS)
X.append(np.asarray(im, dtype=np.uint8))
y.append(c)
except IOError, (errno, strerror):
print("I/O error({0}): {1}".format(errno, strerror))
except:
print("Unexpected error:", sys.exc_info()[0])
raise
c = c+1
return [X, y]
if __name__ == '__main__':
if len(sys.argv) < 3:
print('No image supplied.')
sys.exit()
# Now read in the image data. This must be a valid path!
[X, y] = read_images(sys.argv[1])
print(len(X), len(y))
X_train = np.vstack(X)
print(X_train)
y_train = np.array(y)
image_path = sys.argv[2]
image = np.array(cv2.imread(image_path), dtype=np.uint8)
if np.ndim(image) == 3:
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = cv2.equalizeHist(image)
# create a CLAHE object (Arguments are optional).
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
image = clahe.apply(image)
# detect face in this image
detector = FaceDetector()
X_test = []
for i, d in enumerate(detector.detect(image)):
x, y, w, h = d.left(), d.top(), d.right() - \
d.left(), d.bottom() - d.top()
a = image[y:(y+h), x:(x+w)]
b = cv2.resize(a, (130, 130), interpolation=cv2.INTER_CUBIC)
X_test.append(np.asarray(b, dtype=np.uint8))
X_test = np.vstack(X_test)
##########################################################################
# Compute a PCA (eigenfaces) on the face dataset (treated as unlabeled
# dataset): unsupervised feature extraction/dimensionality reduction
n_components = 150
print('Extracting the top {} eigenfaces from faces'.format(n_components))
t0 = time()
pca = RandomizedPCA(n_components=n_components, whiten=True).fit(X_train)
print("done in %0.3fs" % (time() - t0))
print("Projecting the input data on the eigenfaces orthonormal basis")
t0 = time()
X_train_pca = pca.transform(X_train)
**X_test_pca = pca.transform(X_test)**
print("done in %0.3fs" % (time() - t0))
##########################################################################
# Train a SVM classification model
print("Fitting the classifier to the training set")
t0 = time()
param_grid = {'C': [1e3, 5e3, 1e4, 5e4, 1e5],
'gamma': [0.0001, 0.0005, 0.001, 0.005, 0.01, 0.1], }
clf = GridSearchCV(SVC(kernel='rbf', class_weight='balanced'), param_grid)
clf = clf.fit(X_train_pca, y_train)
print("done in %0.3fs" % (time() - t0))
print("Best estimator found by grid search:")
print(clf.best_estimator_)
##########################################################################
# Quantitative evaluation of the model quality on the test set
print("Predicting people's names on the test set")
t0 = time()
y_pred = clf.predict(X_test_pca)
print("done in %0.3fs" % (time() - t0))
を、私はまた、上記のサンプルに対して実行する一つのテスト画像を取得する:エラーが発生します ラインX_test_pca = pca.transformです( X_test)
にエラーがある:私が正しくフォーマットされていない私のデータとは何かを持っている疑いがある
ValueError: operands could not be broadcast together with shapes (130,130) (92,)
。
エラーが発生している行を教えてください。私はこの問題の原因となるものは何も見えません。 –
私はすべてのコードサンプルを – Denny
@Dennyに含めました。** trainingDataSET(** '.fit(X_train_pca、y_train)' **)と 'testingDataSet'(パイプラインをさらに使用する'.fit(X_test_pca)')は正確に** ** '.shape' ** - sとcolourdepths(そうでなければ**' cv2' ** - 前処理 '...を持つように) – user3666197