简体   繁体   English

使用 haarcascade 分类器编写人脸识别代码时出错

[英]Error while writing code for face recognition using haarcascade classifier

import cv2
import numpy as np

#Init camera

cap = cv2.VideoCapture(0)

#Face Detection using haarcascade File

face_cascade = cv2.CascadeClassifier('Anaconda3\Lib\site-packages\cv2\data\haarcascade_frontalface_alt.xml')

skip = 0

face_data = []
#dataset_path = ('./Face Recognition Data')

while True:
    ret,frame = cap.read()
    if ret == False:
        continue

    faces = face_cascade.detectMultiScale(frame,1.3,5)

    #The next line of code is written to only store the largest face in the window frame
    faces = sorted(faces,key = lambda  f: f[2]*f[3])

    #start sorting from the last face since the last face is the largest in terms of area(w*h)
    for face in faces[-1:] :
        x,y,w,h = face
        cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,255),2)

        #extract the required face or the region of the interest
        #Refers to adding an extra 10 pixels on all the sides of the required extracted face
        offset = 10
        #By default face slicing is done in (y,x) manner
        face_section = frame[y-offset:y+h+offset,x-offset:x+w+offset]
        face_section = cv2.resize(face_section,(100,100))

        if skip%10==0 : #Store every 10th frame
            face_data.append(face_section)
            print(len(face_data)) #number of faces captured so far

    cv2.imshow("Video Frame",frame)
    cv2.imshow("Face section frame",face_section)
    key_pressed = cv2.waitKey(1) & 0xFF
    if key_pressed == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

After running the program, it says that face_section variable is not defined.运行程序后,它说没有定义 face_section 变量。 Please help请帮忙

You have more than one face_section .你有不止一个face_section If you need them outside of your for loop you may do like this:如果您在 for 循环之外需要它们,您可以这样做:

face_section_list = [] # Define a new empty list!
#start sorting from the last face since the last face is the largest in terms of area(w*h)
for face in faces[-1:] :
    x,y,w,h = face
    cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,255),2)

    #extract the required face or the region of the interest
    #Refers to adding an extra 10 pixels on all the sides of the required extracted face
    offset = 10
    #By default face slicing is done in (y,x) manner
    face_section = frame[y-offset:y+h+offset,x-offset:x+w+offset]
    face_section = cv2.resize(face_section,(100,100))
    face_section_list.append(face_section) # Append EVERY face!

    if skip%10==0 : #Store every 10th frame
        face_data.append(face_section)
        print(len(face_data)) #number of faces captured so far

And then outside, print every face in order (or do whatever you need to do):然后在外面,按顺序打印每张脸(或做任何你需要做的事情):

for im in face_section_list:
     cv2.imshow("Face section frame",im)
     cv2.waitKey(0) # Zero means "wait until a key is pressed"

I've wrote a lot of code for face detection and recognition that you may find helpful, have a look .我写了很多用于人脸检测和识别的代码,您可能会发现它们有帮助,请查看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 OpenCV 人脸识别灰度转换错误,同时训练我的分类器 - OpenCV Face Recognition grayscale conversion error while training my classifier 使用opencv进行人脸识别时出现属性错误 - Attribute error while using opencv for face recognition 安装 Face_recognition Library 时出错 - Error while installing Face_recognition Library 在python中安装人脸识别时出错 - Error while installing face-recognition in python 创建训练数据时人脸识别出错 - Error in face recognition while creating training data 我在 pycharm 中执行面部识别代码时遇到错误,也尝试在终端上执行它 - i am getting an error in my face recognition code while excuting it in pycharm, have also tried executing it on terminal 安装 face_recognition 时出错 错误文本:收集 face_recognition - An error occurred while installing face_recognition Error text: Collecting face_recognition 为什么在 python 中使用 face_recognition 库时出现列表索引超出范围错误? - Why am I getting a list index out of range error while using face_recognition library in python? 创建人脸识别代码时出错。 错误是“ TypeError: Expected Ptr<cv::UMat> 对于参数 &#39;src&#39; ” 什么是解决方案 - I got Error while creating face recognition code. The Error is “ TypeError: Expected Ptr<cv::UMat> for argument 'src' ” what is solution 使用 MTCNN 进行人脸识别 - Face Recognition using MTCNN
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM