没有模块名称 'sklearn.ensemble.forest'
No module name 'sklearn.ensemble.forest'
我正在使用此代码检测 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()
我正在为 scikit
使用 0.24.0
版本,我在 Python 3.8 上使用 tensorflow
但是我得到错误:
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
我想我需要使用以前版本的 scikit
(0.19.1
) 但我收到错误:需要 C++ 构建工具。由于我在虚拟环境中,我不知道如何安装这些工具,它们已经安装在我的笔记本电脑中。
sklearn.ensemble.forest
已重命名为 sklearn.ensemble._forest
in 437ca05 on Oct 16, 2019。您需要安装较旧的 sklearn
。试用 2019 年 7 月 30 日发布的 0.21.3 版本:
pip install -U scikit-learn==0.21.3
请注意,作者提供的轮子最高可达 Python 3.7。对于 3.8 或 3.9,您需要 compile from sources.
可能是你的型号太老了。使用:
pip install scikit-learn==0.22
安装旧版本的sklearn。
以上回答正确,
sklearn.ensemble.forest
重命名为 sklearn.ensemble._forest
这个问题在更多依赖于 sklearn 的库中仍然存在,因此我想提供一个额外的解决方案,普遍适用于大多数这些包。
在您的情况下,您的库名为 face_detector
,但当您遇到 scikit-learn(以及其他库)的版本控制问题时,您可以将其替换为任何库名称。
找到库的目录:
import face_detector
print(face_detector.\_\_file__)
在任何文本编辑器中打开文件,在您的情况下,库文件的名称将是 face_detector.py
取消注释旧导入并替换为新导入。
注释掉旧版本 sklearn 的导入并添加新的导入语句
# from sklearn.ensemble.forest import ForestClassifier, ForestRegressor
from sklearn.ensemble._forest import ForestClassifier, ForestRegressor
祝您安全愉快,您刚刚解决了一个依赖性问题!
该解决方案适用于大多数图书馆,甚至比安装不同版本的 sklearn 更有效。如果它不起作用,您仍然可以按照其他答案中的建议安装旧版本。
注意:可以轻松修改此解决方案以跟踪和修复除 sklearn 之外的其他库依赖项的依赖项问题。只要函数本身没有更改输入和输出参数,修复重命名问题就是修复损坏的依赖关系的简单方法。
我正在使用此代码检测 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()
我正在为 scikit
使用 0.24.0
版本,我在 Python 3.8 上使用 tensorflow
但是我得到错误:
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
我想我需要使用以前版本的 scikit
(0.19.1
) 但我收到错误:需要 C++ 构建工具。由于我在虚拟环境中,我不知道如何安装这些工具,它们已经安装在我的笔记本电脑中。
sklearn.ensemble.forest
已重命名为 sklearn.ensemble._forest
in 437ca05 on Oct 16, 2019。您需要安装较旧的 sklearn
。试用 2019 年 7 月 30 日发布的 0.21.3 版本:
pip install -U scikit-learn==0.21.3
请注意,作者提供的轮子最高可达 Python 3.7。对于 3.8 或 3.9,您需要 compile from sources.
可能是你的型号太老了。使用:
pip install scikit-learn==0.22
安装旧版本的sklearn。
以上回答正确,
sklearn.ensemble.forest
重命名为 sklearn.ensemble._forest
这个问题在更多依赖于 sklearn 的库中仍然存在,因此我想提供一个额外的解决方案,普遍适用于大多数这些包。
在您的情况下,您的库名为 face_detector
,但当您遇到 scikit-learn(以及其他库)的版本控制问题时,您可以将其替换为任何库名称。
找到库的目录:
import face_detector print(face_detector.\_\_file__)
在任何文本编辑器中打开文件,在您的情况下,库文件的名称将是
face_detector.py
取消注释旧导入并替换为新导入。
注释掉旧版本 sklearn 的导入并添加新的导入语句
# from sklearn.ensemble.forest import ForestClassifier, ForestRegressor from sklearn.ensemble._forest import ForestClassifier, ForestRegressor
祝您安全愉快,您刚刚解决了一个依赖性问题! 该解决方案适用于大多数图书馆,甚至比安装不同版本的 sklearn 更有效。如果它不起作用,您仍然可以按照其他答案中的建议安装旧版本。
注意:可以轻松修改此解决方案以跟踪和修复除 sklearn 之外的其他库依赖项的依赖项问题。只要函数本身没有更改输入和输出参数,修复重命名问题就是修复损坏的依赖关系的简单方法。