Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What's wrong here? I don't get it

import cv2 as cv
import datetime
import numpy as np

def rescaleFrame(frame,scale=0.9):
    width=int(frame.shape[1]*scale)
    height=int(frame.shape[0]*scale)
    dimension=(width,height)
    return cv.resize(frame,dimension,interpolation=cv.INTER_AREA)

haar_cascade=cv.CascadeClassifier(r'C:\Users\Parth\Desktop\Project-III\faces.xml')
people = ['Parth Parlikar', 'Pradnya Parlikar','Rajiv Parlikar']
face_recognizer=cv.face.LBPHFaceRecognizer_create()
face_recognizer.read(r'C:\Users\Parth\Desktop\Project-III\face_trained.yml')

file = open(r'C:\Users\Parth\Desktop\face.txt', 'a')

capture = cv.VideoCapture(1)
while True:
    isTrue,frame=capture.read()
    haar_cascade=cv.CascadeClassifier('faces.xml')
    faces_rect=haar_cascade.detectMultiScale(frame, scaleFactor=1.1, minNeighbors=8)
    gray=cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
    #print(f"Number of faces found = {len(faces_rect)}")
    cnt = len(faces_rect)

    for (x,y,w,h) in faces_rect:
        faces_roi=gray[y:y+h, x:x+h]
        label, confidence = face_recognizer.predict(faces_roi)
        print(f'Label={people[label]} with a confidence of {confidence}')
        for i in range (0,cnt):
          cv.putText(frame, str(people[label]),(i*400,40), cv.FONT_HERSHEY_COMPLEX, 1.0, (0,255,0), thickness=2)
        cv.rectangle(frame,(x,y), (x+w, y+h), (0,255,0), thickness=2)
        cv.imshow('video',frame)

    file.write(people[label])
    file.write(str(confidence))
    file.write(str(datetime.datetime.now()))
    file.write("\n")



    if cv.waitKey(20) & 0xFF==ord('d'):
        break

cv.imshow('Detected faces', frame)

file.close()


cv.waitKey(0)

In the code i get the following error:

Exception has occurred: NameError
name 'label' is not defined
  File "C:\Users\Parth\Desktop\Project-III\face_recog_video_PROTOTYPE.py", line 41, in <module>
    file.write(people[label])
                      ^^^^^
NameError: name 'label' is not defined

How can label be not defined when its literally defined above? Also the code was running perfectly fine few minutes ago.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :


    for (x,y,w,h) in faces_rect:
        faces_roi=gray[y:y+h, x:x+h]
        label, confidence = face_recognizer.predict(faces_roi)
        print(f'Label={people[label]} with a confidence of {confidence}')
        for i in range (0,cnt):
          cv.putText(frame, str(people[label]),(i*400,40), cv.FONT_HERSHEY_COMPLEX, 1.0, (0,255,0), thickness=2)
        cv.rectangle(frame,(x,y), (x+w, y+h), (0,255,0), thickness=2)
        cv.imshow('video',frame)

    file.write(people[label])
    file.write(str(confidence))
    file.write(str(datetime.datetime.now()))
    file.write("\n")

label is defined inside the for loop, it could be that there are no faces_rect and label ends up not being defined.

Add print statements and check.

Maybe you wanted to indent the last part of the code?


    for (x,y,w,h) in faces_rect:
        faces_roi=gray[y:y+h, x:x+h]
        label, confidence = face_recognizer.predict(faces_roi)
        print(f'Label={people[label]} with a confidence of {confidence}')
        for i in range (0,cnt):
          cv.putText(frame, str(people[label]),(i*400,40), cv.FONT_HERSHEY_COMPLEX, 1.0, (0,255,0), thickness=2)
        cv.rectangle(frame,(x,y), (x+w, y+h), (0,255,0), thickness=2)
        cv.imshow('video',frame)

        file.write(people[label])
        file.write(str(confidence))
        file.write(str(datetime.datetime.now()))
        file.write("\n")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading