Opencv人脸检测问题

时间:2017-10-16 22:19:31

标签: python opencv

我一直试图从图像中获取openCV 2.4.13 python27面部检测请帮忙。我得到的错误是:

Traceback (most recent call last):
  File "C:\Users\sam_o\Desktop\Alrehman\Attendance.py", line 36, in <module>
    profile = getProfile(id)
  File "C:\Users\sam_o\Desktop\Alrehman\Attendance.py", line 19, in getProfile
    profile = none
NameError: global name 'none' is not defined

我写的代码如下:

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

recognizer = cv2.createLBPHFaceRecognizer()

cascadePath = "Classifiers/face.xml"
faceCascade = cv2.CascadeClassifier(cascadePath);
path = 'EmployeeRecord'

def getProfile(id):
    conn=sqlite3.connect("Alrehman.db")
    cmd="SELECT * FROM Person 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.cv.InitFont(cv2.cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 0, 1, 1) #Creates a font
while True:
    ret, im =cam.read()
    gray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
    faces=faceCascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5, minSize=(100, 100), flags=cv2.CASCADE_SCALE_IMAGE)
    for(x,y,w,h) in faces:
        id, conf = recognizer.predict(gray[y:y+h,x:x+w])
        cv2.rectangle(im,(x,y),(x+w,y+h),(225,0,0),2)
        profile=getProfile(id)
        if(profile!=None):
            cv2.cv.PutText(cv2.cv.fromarray(im),str(profile[1]), (x,y+h+30),font, 255) #Draw the text
            cv2.cv.PutText(cv2.cv.fromarray(im),str(profile[2]), (x,y+h+60),font, 255) #Draw the text
        cv2.imshow('im',im)
        cv2.waitKey(10)

有谁知道我在这里做错了什么?

2 个答案:

答案 0 :(得分:0)

看起来您的相机没有拍摄照片或拍摄灰度照片。 (在这种情况下,错误代码215很常见,在其他opencv函数中也很常见)

您可以通过从计算机加载示例图像并将其传递给cv2.cvtColor()来进一步测试。

答案 1 :(得分:0)

我试过了这个代码对我有用.....

import cv2
import numpy as np
import os
import pickle
import sqlite3



faceDetect=cv2.CascadeClassifier('haarcascade_frontalface_default.xml');

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);

rec=cv2.createLBPHFaceRecognizer();
rec.load("recognizer\\trainner.yml");
id=0
font=cv2.cv.InitFont(cv2.cv.CV_FONT_HERSHEY_COMPLEX_SMALL,1,1,0,1)

while(True):
    ret,img=cam.read();
    gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    faces=faceDetect.detectMultiScale(gray,1.3,5);
    for(x,y,w,h) in faces:
        cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
        id,conf=rec.predict(gray[y:y+h,x:x+w])
        profile=getProfile(id)
        if(profile!=None):

            cv2.cv.PutText(cv2.cv.fromarray(img),str(profile[1]),(x,y+h+30),font,255);
            cv2.cv.PutText(cv2.cv.fromarray(img),str(profile[2]),(x,y+h+60),font,255);
            cv2.cv.PutText(cv2.cv.fromarray(img),str(profile[3]),(x,y+h+90),font,255);
           # cv2.cv.PutText(cv2.cv.fromarray(img),str(profile[4]),(x,y+h+30),font,255);
    cv2.imshow("Face",img);
    if(cv2.waitKey(1)==ord('q')):
        break;
cam.release();
cv2.destoryAllWindows()