Problem in Face Recognition Project

THIA CODE IS TAUGHT IN GENERATING SELFIE TRAINING DATA USING WEBCAM. IT JUST OPENS THE WEBCAM AND THAT’S IT. IT EVEN DOESN’T SHOW THE RECTANGLE AROUND DETECTED FACE. PLEASE LOOK INTO IT.

import cv2
import numpy as np
cap = cv2.VideoCapture(0)

face_cascade = cv2.CascadeClassifier(‘haarcascade_frontalface_alt.xml’)
skip = 0
face_data = []
dataset_path = ‘./Data/’
filename = input('Enter the name of the person: ')
while True:
ret, frame = cap.read()
if ret == False:
continue
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(frame, 1.3, 5)
faces = sorted(faces, key=lambda f:f[2]*f[3])

#Pick the largest face (it is at last position in the sorted faces)
for face in faces[-1:0]:
    x,y,w,h = face
    cv2.rectangle(frame, (x,y), (x+w,y+h), (0,255,255) ,2)

    #Extract (crop out the required face) : Region of interest
    offset = 10     #padding around the cropped face
    face_section = frame[y-offset:y+h+offset, x-offset:x+w+offset]
    face_section = cv2.resize(face_section,(100,100))
    skip+=1
    if skip%10 == 0:
        face_data.append(face_section)
        print(len(face_data))

cv2.imshow("Frame", frame)
#cv2.imshow("Face section,", face_section)

key_pressed = cv2.waitKey(1) & 0xFF
if key_pressed == ord('q'):
    break

#Convert face list array into a numpy array
face_data = np.asarray(face_data)
face_data = face_data.reshape((face_data.shape[0],-1))
print(face_data.shape)

#Save this data into file system

np.save(dataset_path + filename + ‘.npy’, face_data)
print('Data successfully saved at ’ + dataset_path + filename + ‘.npy’)

cap.release()
cv2.destroyAllWindows()

Hello @vinay86048, can you pls give me your whole code? I will check that and let you know if that is working or not. Pls make its indentation right and then give it to me or just post it on github or gdrive then share the link of the code.

Please find the python file at this link:

https://drive.google.com/file/d/1js3AdVtYPkMcoDxKLSwZymwnXUL4yLA4/view?usp=sharing

see the code


import cv2
import numpy as np
cap = cv2.VideoCapture(0)

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
skip = 0
face_data = []
dataset_path = './Data/'
filename = input('Enter the name of the person: ')
while True:
    ret, frame = cap.read()
    if ret == False:
        continue
    gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(frame, 1.3, 5)
    faces = sorted(faces, key=lambda f:f[2]*f[3])

    #Pick the largest face (it is at last position in the sorted faces)
    for face in faces[-1:]:
        x,y,w,h = face
        cv2.rectangle(frame, (x,y), (x+w,y+h), (0,255,255) ,2)
    
        #Extract (crop out the required face) : Region of interest
        offset = 10     #padding around the cropped face
        face_section = frame[y-offset:y+h+offset, x-offset:x+w+offset]
        face_section = cv2.resize(face_section,(100,100))
        skip+=1
        if skip%10 == 0:
            face_data.append(face_section)
            print(len(face_data))
    
        cv2.imshow("Frame", frame)
        cv2.imshow("Face section,", face_section)

    key_pressed = cv2.waitKey(1) & 0xFF
    if key_pressed == ord('q'):
        break

#Convert face list array into a numpy array
face_data = np.asarray(face_data)
face_data = face_data.reshape((face_data.shape[0],-1))
print(face_data.shape)

#Save this data into file system

np.save(dataset_path + filename + '.npy', face_data)
print('Data successfully saved at ' + dataset_path + filename + '.npy')

cap.release()
cv2.destroyAllWindows()

Here the mistakes were you were running loop
for face in faces[-1:0]:
but we need to do
for face in faces[-1:]:
and these two lines will come inside the for loop but you were taken them outside the loop.
cv2.imshow(“Frame”, frame)
cv2.imshow(“Face section,”, face_section)

In case it is clear to you pls mark it as resolve. Otherwise pls ask if anything seems confusing to you

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.