首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于OpenCV- AttributeError的图像处理:模块“cv2”没有属性“face”

基于OpenCV- AttributeError的图像处理:模块“cv2”没有属性“face”
EN

Stack Overflow用户
提问于 2022-11-23 09:54:58
回答 2查看 46关注 0票数 0

我试着按照"trying.py“运行,但是没有错误。怎么修呢?

trying.py

代码语言:javascript
复制
import cv2, os
import numpy as np
from PIL import Image

# Create Local Binary Patterns Histograms for face recognization
recognizer = cv2.face.LBPHFaceRecognizer_create()

# Using prebuilt frontal face training model, for face detection
detector = cv2.CascadeClassifier("haarcascade_frontalface_default.xml");


# Create method to get the images and label data
def getImagesAndLabels(path):
    # Get all file path
    imagePaths = [os.path.join(path, f) for f in os.listdir(path)]

    # Initialize empty face sample
    faceSamples = []

    # Initialize empty id
    ids = []

    # Loop all the file path
    for imagePath in imagePaths:

        # Get the image and convert it to grayscale
        PIL_img = Image.open(imagePath).convert('L')

        # PIL image to numpy array
        img_numpy = np.array(PIL_img, 'uint8')

        # Get the image id
        id = int(os.path.split(imagePath)[-1].split(".")[1])
        print(id)

        # Get the face from the training images
        faces = detector.detectMultiScale(img_numpy)

        # Loop for each face, append to their respective ID
        for (x, y, w, h) in faces:
            # Add the image to face samples
            faceSamples.append(img_numpy[y:y + h, x:x + w])

            # Add the ID to IDs
            ids.append(id)

    # Pass the face array and IDs array
    return faceSamples, ids


# Get the faces and IDs
faces, ids = getImagesAndLabels('dataset')

# Train the model using the faces and IDs
recognizer.train(faces, np.array(ids))

# Save the model into trainer.yml
recognizer.save('trainer/trainer.yml')

错误:

"C:\Users\HP\PycharmProjects\face_identificiation\trying.py",跟踪(最近一次调用):

第12行,在识别器= cv2.face.LBPHFaceRecognizer_create() AttributeError:模块'cv2‘中没有属性“face”

EN

回答 2

Stack Overflow用户

发布于 2022-11-23 10:42:19

很可能,您安装了错误的OpenCV版本。

导入face将失败(我使用的是v4.6.0),因为模块没有包含在OpenCv的“正常”安装中。

尝试运行pip list并检查OpenCv版本。我的猜测是,您安装了普通的OpenCv,它将为您提供如下的条目:

opencv-python 4.6.0.66

如果是这样的话,您应该先用pip uninstall opencv-python卸载OpenCv,然后再使用pip install opencv-contrib-python安装带有contrib包的OpenCv。

然后,使用pip list的条目应该包含

opencv-contrib-python 4.6.0.66

这样,您的代码就可以工作了!

编辑:我应该补充一下,您在代码中重写了关键字id,并且应该将变量重命名为其他东西,例如id_doc之类的。

票数 0
EN

Stack Overflow用户

发布于 2022-11-23 10:45:23

尝试以下命令:

代码语言:javascript
复制
pip install opencv-contrib-python --upgrade

如果它不起作用,可以使用以下命令卸载opencv-:

代码语言:javascript
复制
pip uninstall opencv-contrib-python

再安装一次。

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

https://stackoverflow.com/questions/74544839

复制
相关文章

相似问题

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