python - 代码的未知部分,无法在人脸识别python程序中检测未知面孔[opencv]

标签 python python-3.x opencv computer-vision face-recognition

请帮我。我正在运行一个面部识别检测器python程序,该程序将显示sqllite studio数据库中的数据,并且我编写了将未知面孔显示为未知的代码...

import cv2,os
import sqlite3
import numpy as np
from PIL import Image 
import pickle

recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read('trainer/trainer.yml')
cascadePath = "Classifiers/face.xml"
faceCascade = cv2.CascadeClassifier(cascadePath);
path = 'dataSet'

def getProfile(Id):
   conn=sqlite3.connect("facebase.db")
   cmd="SELECT * FROM people WHERE ID="+str(Id)
   cursor=conn.execute(cmd)
   profile=None
   for row in cursor:
       profile=row
   conn.close()
   return profile 


cam = cv2.VideoCapture(0)
font = cv2.FONT_HERSHEY_SIMPLEX #Creates a font
while True:
   ret, im =cam.read()
   gray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
   faces=faceCascade.detectMultiScale(gray, 1.2,5)
   for(x,y,w,h) in faces:
       cv2.rectangle(im,(x,y),(x+w,y+h),(225,0,0),2)
       Id, conf = recognizer.predict(gray[y:y+h,x:x+w])
       profile=getProfile(Id)
       if(profile!=None):
         cv2.putText(im,str(profile[0]), (x,y+10), font, 2, (255,255,255),3)
         cv2.putText(im,str(profile[1]), (x,y+40), font, 2, (255,255,255),3)

       else:
         Id="Unknown"
         cv2.rectangle(im, (x-22,y-90), (x+w+22, y-22), (0,255,0), -1)
         cv2.putText(im, str(Id), (x,y-40), font, 2, (255,255,255), 3)
cv2.imshow('im',im) 
if cv2.waitKey(10) & 0xFF==ord('q'):
    break
cam.release()
cv2.destroyAllWindows()

问题是代码的未知部分正在运行。
例如,如果检测到未知面孔而不是显示未知面孔,它将显示数据库中的随机名称。
       if(profile!=None):
         cv2.putText(im,str(profile[0]), (x,y+10), font, 2, (255,255,255),3)
         cv2.putText(im,str(profile[1]), (x,y+40), font, 2, (255,255,255),3)

       else:
         Id="Unknown"
         cv2.rectangle(im, (x-22,y-90), (x+w+22, y-22), (0,255,0), -1)
         cv2.putText(im, str(Id), (x,y-40), font, 2, (255,255,255), 3)

我正在使用python 3.4和opencv 3.4有人可以帮助我吗?我是python的新手。

谢谢....

最佳答案

我解决了这个问题,在大多数情况下conf会测量预测的准确性,因此conf会小于50,未知数会大于50,因此可以通过添加if(conf)来解决。

    if(conf<60):
            profile=getProfile(Id)
    else:
            Id=0
            profile=getProfile(Id)
    if(profile!=None):
        cv2.rectangle(im, (x-22,y-90), (x+w+80, y-22), (0,255,0), -1)
        cv2.putText(im,str(profile[0]), (x,y-40), font, 2, (255,255,255), 3)
        cv2.putText(im,str(profile[1]), (x+50,y-40), font, 2(255,255,255),3)

代替
   if(profile!=None):
     cv2.putText(im,str(profile[0]), (x,y+10), font, 2, (255,255,255),3)
     cv2.putText(im,str(profile[1]), (x,y+40), font, 2, (255,255,255),3)

   else:
     Id="Unknown"
     cv2.rectangle(im, (x-22,y-90), (x+w+22, y-22), (0,255,0), -1)
     cv2.putText(im, str(Id), (x,y-40), font, 2, (255,255,255), 3)

关于python - 代码的未知部分,无法在人脸识别python程序中检测未知面孔[opencv],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48740349/

相关文章:

python - Django 没有序列化自然键值

python - 如何使用 Python 和 Regex 搜索特定字符串

python - Django SplitDateTime 小部件抛出 'list' 对象没有属性 'strip'

python - OpenCV python 层不适用于 caffe/digits 框架

javascript - 使用 BS4 python 单击并抓取 aspx 页面

python - 使用 Python Regex 查找 2 个标签之间的短语

python - 在 Microsoft Azure WebJob 中指定 Python 版本?

python - 使用opencv和python清理图像背景后如何正确提取字母?

c++ - 使用 OpenCV 跟踪手

python - 为什么这个Tkinter程序卡住了?