0

I have a program to detect masks in Realtime using the KNN classifier. First, I doing capture human face using mask and not mask in a folder. Then each folder is converted to .NPY file, , like the following program code: model.ipynb

The .npy file is processed in the training program as follows code: train_and_test.ipynb

But when testing, there is an error like this:

(165, 165, 3)
(100, 100, 3)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-29-0f61e7a9f47a> in <module>
     14             print(face.shape)
     15             #face=face.reshape(1,-1)
---> 16             face=pca.transform(face)
     17             face=std.transform(face)
     18             prediction=knn.predict(face)

c:\users\acer\appdata\local\programs\python\python39\lib\site-packages\sklearn\decomposition\_base.py in transform(self, X)
    124         check_is_fitted(self)
    125 
--> 126         X = self._validate_data(X, dtype=[np.float64, np.float32], reset=False)
    127         if self.mean_ is not None:
    128             X = X - self.mean_

c:\users\acer\appdata\local\programs\python\python39\lib\site-packages\sklearn\base.py in _validate_data(self, X, y, reset, validate_separately, **check_params)
    419             out = X
    420         elif isinstance(y, str) and y == 'no_validation':
--> 421             X = check_array(X, **check_params)
    422             out = X
    423         else:

c:\users\acer\appdata\local\programs\python\python39\lib\site-packages\sklearn\utils\validation.py in inner_f(*args, **kwargs)
     61             extra_args = len(args) - len(all_args)
     62             if extra_args <= 0:
---> 63                 return f(*args, **kwargs)
     64 
     65             # extra_args > 0

c:\users\acer\appdata\local\programs\python\python39\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, estimator)
    657                     "into decimal numbers with dtype='numeric'") from e
    658         if not allow_nd and array.ndim >= 3:
--> 659             raise ValueError("Found array with dim %d. %s expected <= 2."
    660                              % (array.ndim, estimator_name))
    661 

ValueError: Found array with dim 3. Estimator expected <= 2.

How to solve the error problem? and what the error mean? or you can update the program to the link

Thanks

  • it probably wants a grayscale version of the image. Try turning it into grayscale using `pillow` with code such as something over here: https://stackoverflow.com/questions/12201577/how-can-i-convert-an-rgb-image-into-grayscale-in-python – hypadr1v3 Jun 02 '21 at 04:41
  • @hypadr1v3 I have changed all images in the database to grayscale, but the error is still same. It seems the problem is due to the different types of array dimensions in the dataset file (.npy). In my dataset I have a 2-dimensional array value while for PCA it takes 3 dimensions, but I don't understand how to change the dimensions of the array – Monica Kristy Jun 02 '21 at 13:02
  • try `np.reshape` – hypadr1v3 Jun 03 '21 at 04:17

0 Answers0