我如何在If语句内执行一次Python指令

问题描述 投票:-1回答:1

我正在更新Python / OpenCV人脸识别程序,因此当它识别出一个人时,该人的姓名和时间将存储在Google表格中。请检查此代码:

    for x, y, w, h in faces:
        cv2.rectangle(img, (x,y), (x+w,y+h), (0,255,0), 2)
        id, confidence = recognizer.predict(gray[y:y+h,x:x+w])

        if confidence > 0:

            id = names[id]
            confidence = "  {0}%".format(round(confidence))

            sheet.append_row([str(id), strftime("%Y-%m-%d %H:%M:%S", gmtime())])

        else:
            id = "Inconnue"
            confidence = "  {0}%".format(round(confidence))

所以,我只想执行一次sheet.append_row([str(id), strftime("%Y-%m-%d %H:%M:%S", gmtime())])。当我执行代码时,当我面对镜头时,它会继续将我的名字发送到工作表。

python opencv
1个回答
0
投票

您可以维护一组已经插入的名称,即:


existing_entries = {}   # set of existing entries
for x,y,w,h in faces:
    cv2.rectangle(img, (x, y), (x + w, y + h), (0,255,0), 2)
    id, confidence = recognizer.predict(gray[y:y+h,x:x+w])

    if confidence > 0:


        id = names['id']
        if id not in existing_entries:
           confidence = "  {0}%".format(round(confidence))

           sheet.append_row([str(id), strftime("%Y-%m-%d %H:%M:%S", gmtime())])
           existing_entries.add(id)   # add recognized entry to existing entries 

    else:
        id = "Inconnue"
        confidence = "  {0}%".format(round(confidence))
© www.soinside.com 2019 - 2024. All rights reserved.