Ошибка при написании кода для распознавания лиц с использованием классификатора haarcascade - PullRequest
0 голосов
/ 20 марта 2020
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()

После запуска программы написано, что переменная face_section не определена. Пожалуйста, помогите

1 Ответ

0 голосов
/ 23 марта 2020

У вас есть более одного face_section. Если они вам нужны вне вашего для l oop, вы можете сделать это следующим образом:

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

А затем снаружи напечатайте каждое лицо по порядку (или сделайте все, что вам нужно сделать):

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

Я написал много кода для распознавания и распознавания лиц, который может оказаться полезным, посмотрите .

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...