首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >没有模块名称'sklearn.forest.ensemble‘

没有模块名称'sklearn.forest.ensemble‘
EN

Stack Overflow用户
提问于 2021-01-17 15:01:18
回答 3查看 12.9K关注 0票数 7

我正在使用此代码来检测face_spoofing

代码语言:javascript
运行
复制
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()

但是我得到了一个错误

代码语言:javascript
运行
复制
I am using the version 0.24.0 for scikit and am on python 3.8 to use tensorflowTraceback (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),但是我得到了error C++ build tools required。我不知道如何安装这些工具,因为我在虚拟环境中,它们已经安装在我的笔记本电脑上了。

请建议我能做些什么

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-01-17 21:33:50

sklearn.ensemble.forest已重命名为sklearn.ensemble._forest in 437ca05 on Oct 16, 2019。您需要安装较旧的sklearn。请试用2019年7月30日发布的0.21.3版本:

代码语言:javascript
运行
复制
pip install -U scikit-learn==0.21.3

请注意,作者提供了Python3.7的轮子。对于3.8或3.9,您将需要compile from sources

票数 13
EN

Stack Overflow用户

发布于 2021-07-23 02:07:46

上面的答案是正确的,sklearn.ensemble.forest已重命名为sklearn.ensemble._forest

这个问题仍然存在于更多依赖sklearn的库中,因此我想提供一个额外的解决方案,普遍适用于大多数这些包。

在您的例子中,您的库名称为face_detector,但是当您在版本控制方面遇到此问题时,您可以将其替换为任何库名称(以及其他库)。

  1. 找到该库的目录:

导入print(face_detector.\_\_file__) face_detector

在任何文本编辑器中打开

  1. 文件,在本例中,库文件的名称将为face_detector.py

  1. -注释旧的导入并替换为新的导入。

注释掉旧版本sklearn的import,并添加新的import语句from sklearn.ensemble.forest import ForestClassifier,ForestRegressor from sklearn.ensemble._forest import ForestClassifier,ForestRegressor

  1. 安全和享受,您刚刚修复了一个依赖问题!这个解决方案适用于大多数库,甚至比安装不同版本的sklearn所需的工作量更少。如果它不工作,您仍然可以按照另一个答案中的建议安装和旧版本。

注意:可以很容易地修改此解决方案,以跟踪和修复sklearn以外的其他库依赖项的依赖项问题。只要函数本身没有改变输入和输出参数,修复重命名问题是修复损坏的依赖关系的一种简单方法。

票数 2
EN

Stack Overflow用户

发布于 2021-04-26 23:18:33

也许你的模型太旧了。使用:

代码语言:javascript
运行
复制
pip install scikit-learn==0.22

安装旧版本的sklearn。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65758102

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档