上面字面上定义的符号怎么可能没有被定义呢? [已关闭]

问题描述 投票:0回答:1
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)

在代码中我收到以下错误:

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

当上面字面上定义了

label
时,它怎么可能没有被定义呢?而且几分钟前代码运行得非常好。

python opencv nameerror
1个回答
-1
投票

    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 是在 for 循环内部定义的,可能没有

faces_rect
,所以 label 最终没有被定义。

添加打印语句并检查。

也许您想缩进代码的最后一部分?


    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")
© www.soinside.com 2019 - 2024. All rights reserved.