python - OpenCV python上的人脸识别问题

标签 python python-3.x opencv

我非常感谢您的帮助,我在openCV上获得了认可。问题是我需要在cmd中显示可识别人员的姓名,例如,如果识别出具有Alex姓名的人,则应通过print(id)在cmd中显示该人的姓名,
如果(确定性> 20):
id =名称[id]
插入print(id),但它进入while循环并打印无限量,我需要它一次打印此人的名字,而不是在循环中,但这样才能继续识别。
这是代码

import cv2
import numpy as np
import os 


recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read('trainer/trainer.yml')
cascadePath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascadePath);
font = cv2.FONT_HERSHEY_SIMPLEX#iniciate id counter

id = 0# names related to ids: example ==> Marcelo: id=1,  etc

names = ['None', 'Alex', 'Trump', 'Obama', 'Z', 'W'] # Initialize and start realtime video capture

cam = cv2.VideoCapture(1)
cam.set(3, 1280) # set video widht
cam.set(4, 720) # set video height# Define min window size to be recognized as a face

minW = 0.1*cam.get(3)
minH = 0.1*cam.get(4)

while True:
    ret, img =cam.read()# Flip vertically
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

    faces = faceCascade.detectMultiScale( 
        gray,
        scaleFactor = 1.2,
        minNeighbors = 5,
        minSize = (int(minW), int(minH)),
       )
    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 is less them 100 ==> "0" : perfect match 
        if (confidence > 20):
            id = names[id]
            confidence = "  {0}%".format(round(100 - confidence))

        else:
            id = "Unknown person"
            confidence = "  {0}%".format(round(100 - confidence))

        cv2.putText(
                    img, 
                    str(id), 
                    (x+5,y-5), 
                    font, 
                    1, 
                    (255,255,255), 
                    2
                   )
        cv2.putText(
                    img, 
                    str(confidence), 
                    (x+5,y+h-5), 
                    font, 
                    1, 
                    (255,255,0), 
                    1
                   )  

    cv2.imshow('camera',img) 
    k = cv2.waitKey(10) & 0xff # Press 'ESC' for exiting video
    if k == 27:
        break# Do a bit of cleanup
print("\n [INFO] Exiting Program and cleanup stuff")
cam.release()
cv2.destroyAllWindows()

最佳答案

在开始创建

previous_id = None

并循环使用
if id != previous_id:
   print(id)
   previous_id = id

如果您只想打印一次id,即使在其他id之后又打印一次,请使用list

在开始创建
all_previous_id = []

并循环使用
if id not in all_previous_id:
   print(id)
   all_previous_id.append(id)

关于python - OpenCV python上的人脸识别问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61796968/

相关文章:

c - 如何在不停止 C 中的父进程的情况下 fork 子进程

c++ - “CvLoadImage”未在此范围内声明

python - 如何在 OpenCV 中读取 .tif 图像

python - 覆盖 python-social-auth 内置 url?

python 请求登录后得到响应代码 500

python - 语法错误 : keyword can't be an expression (views. py)

python - 使用 Pandas 将唯一数字转换为 md5 哈希

python - 如何使用 "getMouse() & getMouseNow()"以及哪个文件包含这些函数?

使用大表循环的python性能问题

python-3.x - 使用 python 将列添加到 csv 文件上的大型数据集的最佳方法是什么?