gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 16:33:49 25 4
gpt4 key购买 nike

我非常感谢您的帮助,我在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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com