0
votes

i am working on a smile recognition using openCV, i wrote the code and reached the command line below:

def test_recognition(c1, c2):
    subplot(121)
    extracted_face1 = extract_face_features(gray1, face1[0], (c1, c2))
    imshow(extracted_face1, cmap='gray')
    print(predict_face_is_smiling(extracted_face1))
    subplot(122)
    extracted_face2 = extract_face_features(gray2, face2[0], (c1, c2))
    imshow(extracted_face2, cmap='gray')
    print(predict_face_is_smiling(extracted_face2))

then when i ran the code below:

interact(test_recognition,
         c1=(0.0, 0.3, 0.01),
         c2=(0.0, 0.3, 0.01))

it gave me this error:

ValueError Traceback (most recent call last) ~\Anaconda3\lib\site-packages\ipywidgets\widgets\interaction.py in update(self, *args) 249 value = widget.get_interact_value() 250 self.kwargs[widget._kwarg] = value --> 251 self.result = self.f(**self.kwargs) 252 show_inline_matplotlib_plots() 253 if self.auto_display and self.result is not None:

in test_recognition(c1, c2) 3 extracted_face1 = extract_face_features(gray1, face1[0], (c1, c2)) 4 imshow(extracted_face1, cmap='gray') ----> 5 print(predict_face_is_smiling(extracted_face1)) 6 subplot(122) 7 extracted_face2 = extract_face_features(gray2, face2[0], (c1, c2))

in predict_face_is_smiling(extracted_face) 1 def predict_face_is_smiling(extracted_face): ----> 2 return svc_1.predict(extracted_face.ravel())

~\Anaconda3\lib\site-packages\sklearn\svm\base.py in predict(self, X) 574 Class labels for samples in X. 575 """ --> 576 y = super(BaseSVC, self).predict(X) 577 return self.classes_.take(np.asarray(y, dtype=np.intp)) 578

~\Anaconda3\lib\site-packages\sklearn\svm\base.py in predict(self, X) 323 y_pred : array, shape (n_samples,) 324 """ --> 325 X = self._validate_for_predict(X) 326 predict = self._sparse_predict if self._sparse else self._dense_predict 327 return predict(X)

~\Anaconda3\lib\site-packages\sklearn\svm\base.py in _validate_for_predict(self, X) 456 457 X = check_array(X, accept_sparse='csr', dtype=np.float64, order="C", --> 458 accept_large_sparse=False) 459 if self._sparse and not sp.isspmatrix(X): 460 X = sp.csr_matrix(X)

~\Anaconda3\lib\site-packages\sklearn\utils\validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator) 550 "Reshape your data either using array.reshape(-1, 1) if " 551 "your data has a single feature or array.reshape(1, -1) " --> 552 "if it contains a single sample.".format(array)) 553 554 # in the future np.flexible dtypes will be handled like object dtypes

ValueError: Expected 2D array, got 1D array instead: array=[0.33913043 0.36086956 0.4173913 ... 0.52608699 0.56956524 0.53913045]. Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

Any hint about how to fix this?

Thanks in advance

1

1 Answers

0
votes

The TraceBack of the code literally contains the answer to your question.

Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

You have to create a Numpy NDArray instead of an Array and reshape it them to be accepted by the code.