我正在带有网络摄像头的树莓派上运行 OpenCV 进行面部识别,但它已停止工作,我的代码没有任何更改



我正在尝试在我的树莓派上运行面部识别服务,但它突然停止检测人脸

我为此使用了python-opencv,上次我测试它时一切正常。下面的代码用于在新面孔上训练系统`

import cv2
import os
import numpy as np
from PIL import Image
import sqlite3
recognizer = cv2.createLBPHFaceRecognizer()
detector= cv2.CascadeClassifier("haarcascade_frontalface_default.xml");
cam = cv2.VideoCapture(0)
con = sqlite3.connect('Users.db')
cur = con.cursor()

Id=raw_input('Enter your id: ')
name=raw_input('Enter your name: ')
sampleNum=0
while True:
    ret, img = cam.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = detector.detectMultiScale(gray, 1.3, 5)
    for (x,y,w,h) in faces:
        cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
        #incrementing sample number 
        sampleNum=sampleNum+1
        #saving the captured face in the dataset folder
        cv2.imwrite("dataSet/User."+Id +'.'+ str(sampleNum) + ".jpg", gray[y:y+h,x:x+w])
        cv2.imshow('frame',img)
    #wait for 100 miliseconds 
    if cv2.waitKey(100) & 0xFF == ord('q'):
        break
    # break if the sample number is more than 20
    elif sampleNum>30:
        break
cam.release()
cv2.destroyAllWindows()

def getImagesAndLabels(path):
    #get the path of all the files in the folder
    imagePaths=[os.path.join(path,f) for f in os.listdir(path)] 
    #create empth face list
    faceSamples=[]
    #create empty ID list
    Ids=[]
    #now looping through all the image paths and loading the Ids and the images
    for imagePath in imagePaths:
        #loading the image and converting it to gray scale
        pilImage=Image.open(imagePath).convert('L')
        #Now we are converting the PIL image into numpy array
        imageNp=np.array(pilImage,'uint8')

        #getting the Id from the image
        Id=int(os.path.split(imagePath)[-1].split(".")[1])
        # extract the face from the training image sample
        faces=detector.detectMultiScale(imageNp)
        #If a face is there then append that in the list as well as Id of it
        for (x,y,w,h) in faces:
            faceSamples.append(imageNp[y:y+h,x:x+w])
            Ids.append(Id)
    return faceSamples,Ids
os.system("sudo rm trainer/trainer.yml")
faces,Ids = getImagesAndLabels('dataSet')
recognizer.train(faces, np.array(Ids))
recognizer.save('trainer/trainer.yml')

'通常它会打开一个窗口并连续显示 30 张用户面部照片,但现在在询问名称后什么也没显示。我运行过其他OpenCV应用程序,它可以在静态图像中找到人脸。

我不得不替换haar_cascade_frontalface.xml文件

最新更新