I am using this code to detect face_spoofing
import numpy as np
import cv2
import joblib
from face_detector import get_face_detector, find_faces
def calc_hist(img):
"""
To calculate histogram of an RGB image
Parameters
----------
img : Array of uint8
Image whose histogram is to be calculated
Returns
-------
histogram : np.array
The required histogram
"""
histogram = [0] * 3
for j in range(3):
histr = cv2.calcHist([img], [j], None, [256], [0, 256])
histr *= 255.0 / histr.max()
histogram[j] = histr
return np.array(histogram)
face_model = get_face_detector()
clf = joblib.load(0)
cap = cv2.VideoCapture("videos/face_spoofing.mp4")
sample_number = 1
count = 0
measures = np.zeros(sample_number, dtype=np.float)
while True:
ret, img = cap.read()
faces = find_faces(img, face_model)
measures[count%sample_number]=0
height, width = img.shape[:2]
for x, y, x1, y1 in faces:
roi = img[y:y1, x:x1]
point = (0,0)
img_ycrcb = cv2.cvtColor(roi, cv2.COLOR_BGR2YCR_CB)
img_luv = cv2.cvtColor(roi, cv2.COLOR_BGR2LUV)
ycrcb_hist = calc_hist(img_ycrcb)
luv_hist = calc_hist(img_luv)
feature_vector = np.append(ycrcb_hist.ravel(), luv_hist.ravel())
feature_vector = feature_vector.reshape(1, len(feature_vector))
prediction = clf.predict_proba(feature_vector)
prob = prediction[0][1]
measures[count % sample_number] = prob
cv2.rectangle(img, (x, y), (x1, y1), (255, 0, 0), 2)
point = (x, y-5)
# print (measures, np.mean(measures))
if 0 not in measures:
text = "True"
if np.mean(measures) >= 0.7:
text = "False"
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img=img, text=text, org=point, fontFace=font, fontScale=0.9, color=(0, 0, 255),
thickness=2, lineType=cv2.LINE_AA)
else:
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img=img, text=text, org=point, fontFace=font, fontScale=0.9,
color=(0, 255, 0), thickness=2, lineType=cv2.LINE_AA)
count+=1
cv2.imshow('img_rgb', img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
I am using the version 0.24.0
for scikit
and I am on Python 3.8 to use tensorflow
But I get the error:
Traceback (most recent call last): File "C:/Users/heman/PycharmProjects/ProctorAI/face_spoofing.py", line 29, in <module> clf = joblib.load('models/face_spoofing.pkl') File "C:\Users\heman\PycharmProjects\ProctorAI\venv\lib\site-packages\joblib\numpy_pickle.py", line 585, in load obj = _unpickle(fobj, filename, mmap_mode) File "C:\Users\heman\PycharmProjects\ProctorAI\venv\lib\site-packages\joblib\numpy_pickle.py", line 504, in _unpickle obj = unpickler.load() File "C:\Users\heman\AppData\Local\Programs\Python\Python38\lib\pickle.py", line 1212, in load dispatch[key[0]](self) File "C:\Users\heman\AppData\Local\Programs\Python\Python38\lib\pickle.py", line 1528, in load_global klass = self.find_class(module, name) File "C:\Users\heman\AppData\Local\Programs\Python\Python38\lib\pickle.py", line 1579, in find_class __import__(module, level=0) ModuleNotFoundError: No module named 'sklearn.ensemble.forest' Process finished with exit code 1
I think I need to use the previous version of scikit
(0.19.1
) but I get the error: C++ build tools required. I don't know how to install these tools as I am in a virtual environment, they are already installed in my laptop.
The Python "ModuleNotFoundError: No module named 'sklearn'" occurs when we forget to install the scikit-learn module before importing it or install it in an incorrect environment. To solve the error, install the module by running the pip install scikit-learn command.
Use sklearn. __version__ to display the installed version of scikit-learn. Call sklearn. __version__ to return the current version of scikit-learn .
sklearn.ensemble.forest
was renamed to sklearn.ensemble._forest
in 437ca05 on Oct 16, 2019. You need to install an older sklearn
. Try version 0.21.3 released on Jul 30, 2019:
pip install -U scikit-learn==0.21.3
Please be warned that the authors provided wheels up to Python 3.7. For 3.8 or 3.9 you will need to compile from sources.
The answer above is correct,
sklearn.ensemble.forest
is renamed to sklearn.ensemble._forest
This problem persist with more libraries that depend on sklearn, therefore I want to provide an additional solution that universally for most of these packages.
In your case your library is called face_detector
, but you can replace it with any library name when you encounter this issue with versioning of scikit-learn (as well as with other libraries).
Locate the directory of the library:
import face_detector
print(face_detector.\_\_file__)
Open the file in any text editor, in your case the name of the library file would be face_detector.py
Out-comment the old import and replace with the new import.
Comment out the import for the old versions of sklearn and add the new import statement
# from sklearn.ensemble.forest import ForestClassifier, ForestRegressor
from sklearn.ensemble._forest import ForestClassifier, ForestRegressor
Safe and enjoy, you just fixed a dependency issue! This solution will work for most libraries and is even less work than installing a different version of sklearn. In case it does not work, you can still install and older version as suggested in the other answer.
Note: This solution can be easily modified to trace and fix dependency issues for other library dependencies than sklearn. As long as the function itself did not change in input and output parameters, fixing renaming issues is an easy way to fix broken dependencies.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With