关于树莓派的人脸识别创造了LBP

时间:2015-02-14 14:33:41

标签: python-2.7 opencv raspberry-pi raspbian raspberry-pi2

我刚刚完成了一个工作程序,在ubuntu系统上使用python进行面部识别

但是当你想把工作转移到“Raspberry pi”时会出现这个错误

这是完整的错误: AttributeError:'module'对象没有属性'createLBPHFaceRecognizer'

解决方案是什么

谢谢

import cv2
import sys
import cv
import glob
import numpy as np
import os
labeltest=[]
Images=[]
Len=0

model = cv2.createLBPHFaceRecognizer(1,8,8,8,70.0)

Labels=[]
textsay=""

# *********** Read *****************\\
def read():
    arr={}
    with open("csv.ext") as f:
        for line in f:
            arr=line.split("%",2)

            labeltest.append(arr[1])
            Images.append(cv2.imread(arr[0],cv2.IMREAD_GRAYSCALE))
    label=range(0,len(labeltest))
    for i in range(0,len(labeltest)):
        label[i]=int(labeltest[i])

    print (label)
    model.train(np.asarray(Images),np.asarray(label))


    model.save("mezo.xml")


    model.load("mezo.xml")




# //*********** Read *****************


def writetofile(key):
    fo = open("csv.ext", "a+")
    fo.write(key)
    fo.write("\n")


def searchName(key):
    lines=tuple(open("Names.txt","r"))
    for i in range(0,len(lines)):
        test=lines[i].split("\n")
        print test[0]
        if str(key.lower())==str(test[0].lower()):
            return i
    return -1

def readName():
    lines=tuple(open("Names.txt","r"))
    for i in range(0,len(lines)):
        Labels.append(lines[i])
    print Labels

def AddName(key):
    fo = open("Names.txt", "a+")
    fo.write(key)
    fo.write("\n")
    readName()


# *********** Add *****************\\

def Add(faces,gray):
    count=Len+100
    for (x, y, w, h) in faces:
        filename = "/home/mohammad/Desktop/traning/%03d"%count +".pgm"
        f=gray[y:y+h,x:x+w]
        f=cv2.resize(f,(92,112),interpolation=cv2.INTER_LANCZOS4)
        newName=raw_input("Enter the Name : ")
        index=searchName(newName)
        if index==-1:
                index=len(Labels)
                AddName(newName)
        filenameIn = filename+"%"+str(index)
        writetofile(filenameIn)
        cv2.imwrite(filename,f)
        count+=1
    read()

# //*********** Add *****************

path={}
path=glob.glob("/home/mohammad/Desktop/traning/*.pgm")
Len=len(path)-1
cascPath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
count=0
video_capture = cv2.VideoCapture(0)
read();
readName()
while True:
    # Capture frame-by-frame
        ret, frame = video_capture.read()
        cv2.waitKey(10)
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        faces = faceCascade.detectMultiScale(
        frame,
        scaleFactor=1.1,
        minNeighbors=5,
        minSize=(30, 30),
        flags=cv2.cv.CV_HAAR_SCALE_IMAGE
    )
        # Draw a rectangle around the faces
        for (x, y, w, h) in faces:
                cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
                f=gray[y:y+h,x:x+w]
                f=cv2.resize(f,(92,112),interpolation=cv2.INTER_LANCZOS4)
                cv2.imwrite("11.pgm",f)
                label, confidence = model.predict(f)
                print"Threshold : ", model.getDouble("threshold")
                if label>-1:
                        if Labels[label] != textsay:
                            cmd = 'espeak "{0}" 2>/dev/null'.format(Labels[label])
                            os.system(cmd)
                        textsay=Labels[label]  
                        font = cv2.FONT_HERSHEY_SIMPLEX
                        cv2.putText(frame,Labels[label],(x,y-10), font, 1.0,(255,255,255))
                        print "\n"+str(Labels[label])+"   |    "+str(confidence)



  # Display the resulting frame
        cv2.imshow('Video', frame)
        k=cv2.waitKey(5)& 0xFF
        if  k==97 :
                Add(faces,gray)
        if k==27:
                exit()

0 个答案:

没有答案