在RaspberryPi上运行Python中的FaceRecognition代码时,没有得到名为“picamera”的模块

2024-05-19 13:32:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我在RaspberryPi上运行Python FaceRecognition代码,但是遇到了No module named 'picamera'错误。我尝试过很少的解决办法,但没有一个对我有效。有人能找出问题出在哪里吗?在

下面是我的Python代码:

import cv2
import numpy as np
from imutils.video.pivideostream import PiVideoStream
from imutils.video import FPS
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import argparse
import imutils

#Load pretrained cascade face detection
face_cascade = cv2.CascadeClassifier('lbpcascade_frontalface.xml')
#Load improved FPS video instead of the default Picamera
reader  = PiVideoStream().start()
time.sleep(0.2)
#Load the recognizer
rec = cv2.face.createLBPHFaceRecognizer()
#Load trained local binary pattern face data
rec.load("recognizer/trainingData.yml")
id=0
font = cv2.FONT_HERSHEY_SIMPLEX

#Face recognition function
def detectFace(faces,hog,img):
    for (x, y, w, h) in faces:
       cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
       result = cv2.face.MinDistancePredictCollector()
       rec.predict(hog[y:y+h,x:x+w],result, 0)
       id = result.getLabel()

       conf = result.getDist()
       if(conf<150):
               if(id==1):
                       id="Ibrahim_"+str(conf)
               elif(id==2):
                       id="Minh_"+str(conf)
               else:
                       id="Hyeon_"+str(conf)
       else:
               id="Unknow"
       cv2.putText(img,str(id),(x,y+h),font,1,(255,255,255),2,cv2.LINE_AA)      


while(True):
    #read each frame in the real-time video
    frame = reader.read()
    img=frame
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    #Use histogram equalizer for adjusting face in different light condition
    equ = cv2.equalizeHist(gray) 
    faces = face_cascade.detectMultiScale(equ, 1.05, 5,minSize=(10,10))
    #If the face is not frontal face then rotate the face by +30/-30 degree 
    if len(faces)==0:
        rows,cols = equ.shape
        M = cv2.getRotationMatrix2D((cols/2,rows/2),30,1)
        dst = cv2.warpAffine(equ,M,(cols,rows))
        faces = face_cascade.detectMultiScale(dst, 1.05, 5,minSize=(10,10))
        if len(faces)==0:
                rows,cols = equ.shape
                M = cv2.getRotationMatrix2D((cols/2,rows/2),-30,1)
                dst = cv2.warpAffine(equ,M,(cols,rows))
                faces = face_cascade.detectMultiScale(dst, 1.05, 5,minSize=(10,10))
                detectFace(faces,dst,img)

        else:
                detectFace(faces,dst,img)

    else:
        detectFace(faces,equ,img)

    cv2.imshow('Face', img)
    if(cv2.waitKey(1)==ord('q')):
        break;

cap.release()
cv2.destroyAllWindows()

下面是我在RaspberryPi终端上运行的结果和我得到的:

^{pr2}$

Tags: thefromimportidimgifconfcv2