【问题标题】:Unknown part of the code,not detecting unknown faces in the face recognition python program[opencv]未知部分代码,在人脸识别python程序中未检测到未知人脸[opencv]
【发布时间】:2018-07-22 06:16:28
【问题描述】:

请帮助我。我正在运行一个人脸识别检测器 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新手。

谢谢……

【问题讨论】:

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


    【解决方案1】:

    我解决了这个问题,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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-22
      • 1970-01-01
      • 2017-08-29
      • 2020-07-29
      • 2013-09-24
      • 2019-03-04
      • 2021-10-30
      • 1970-01-01
      相关资源
      最近更新 更多