关于人脸识别opencv的module ‘cv2.cv2’ has no attribute …

Pyhton的人脸识别代码如下:

#!usr/bin/env python

#coding:utf‐8

import sys

sys.path.append(‘C:\\Users\\Heyden\\AppData\\Local\\Programs\\Python\\Python37-32\\Lib\\site-packages\\’)

import cv2

import os

import numpy as np

from PIL import Image

# recognizer = cv2.createLBPHFaceRecognizer()

detector = cv2.CascadeClassifier(“haarcascade_frontalface_default.xml”)

recognizer = cv2.face.LBPHFaceRecognizer_create()

def get_images_and_labels(path):

    image_paths = [os.path.join(path, f) for f in os.listdir(path)]

    face_samples = []

    ids = []

    for image_path in image_paths:

        image = Image.open(image_path).convert(‘L’)

        image_np = np.array(image, ‘uint8’)

        if os.path.split(image_path)[-1].split(“.”)[-1] != ‘jpg’:

            continue

        image_id = int(os.path.split(image_path)[-1].split(“.”)[1])

        faces = detector.detectMultiScale(image_np)

        for (x, y, w, h) in faces:

            face_samples.append(image_np[y:y + h, x:x + w])

            ids.append(image_id)

    return face_samples, ids

faces, Ids = get_images_and_labels(‘C:\\Users\\Heyden\\PycharmProjects\\face\\data_set’)

recognizer.train(faces, np.array(Ids))

recognizer.save(‘C:\\Users\\Heyden\\PycharmProjects\\face\\trainner\\trainner.yml’)

执行代码报错:

Traceback (most recent call last):

  File “C:\Users\Heyden\PycharmProjects\face\trainner2.py”, line 13, in <module>

    recognizer = cv2.face.LBPHFaceRecognizer_create()

AttributeError: module ‘cv2.cv2’ has no attribute ‘face’

Process finished with exit code 1

(划线的部分即问题所在)

问题分析:主要原因是OpenCV的扩展模块Opencv_contrib没有找到的原因

解决方法:cmd命令行下  ——  pip install opencv_contrib_python 

验证:代码测试通过,实现人脸识别数据集的训练。

你或许想:《去原作者写文章的地方

「点点赞赏,手留余香」

    还没有人赞赏,快来当第一个赞赏的人吧!
0 条回复 A 作者 M 管理员
    所有的伟大,都源于一个勇敢的开始!
欢迎您,新朋友,感谢参与互动!欢迎您 {{author}},您在本站有{{commentsCount}}条评论