UnboundLocalError:赋值前引用的局部变量“匹配”

2024-09-25 00:28:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Python3.8做一个面部识别程序。当我试图执行我的程序时,我遇到了错误代码UnboundLocalError:赋值前引用的局部变量“matches”。据我所知,错误代码意味着在声明变量之前已经为变量赋值,但我找不到问题所在

下面是我的代码:

def test(): cap=cv2.视频捕获(0)

while True:
    _, img = cap.read()

    imgS = cv2.resize(img,(0,0),None,0.25,0.25)

    imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)

    facesCurFrame = face_recognition.face_locations(imgS)
    encodeCurFrame = face_recognition.face_encodings(imgS,facesCurFrame)

    for encodeFace,faceLoc in zip(encodeCurFrame, facesCurFrame):
        matches =face_recognition.compare_faces(encodeListKnown,encodeFace)
        faceDis = face_recognition.face_distance(encodeListKnown,encodeFace)
        print(faceDis)


        matchIndex = np.argmin(faceDis)
        print('matchIndex', matchIndex)

    if matches[matchIndex]:

        name = testNames[matchIndex].upper()
        print(name)
        y1, x2, y2, x1 = faceLoc
        y1, x2, y2, x1 = y1 * 4, x2 * 4, y2 * 4, x1 * 4
        cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
        cv2.rectangle(img, (x1, y2 - 35), (x2, y2), (0, 0, 0), cv2.FILLED)
        cv2.putText(img, name, (x1 + 6, y2 - 6), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255), 2)
        markAttendance(name)
        cv2.imshow('Webcam', img)
        winsound.Beep(freq, duration)
        MessageBox.showinfo("Notice", "Welcome " + name);



    if cv2.waitKey(1) == 27:
        break


cap.release()

Tags: nameimgcv2facecapx1x2matches