简体   繁体   English

在 RaspberryPi 上用 Python 运行 FaceRecognition 代码时,未获取名为“picamera”的模块

[英]Getting No module named 'picamera' while running FaceRecognition code in Python on RaspberryPi

I am running my Python FaceRecognition code on RaspberryPi but I am getting No module named 'picamera' error.我在 RaspberryPi 上运行我的 Python FaceRecognition 代码,但我收到No module named 'picamera'错误。 I have tried few solutions but none worked for me.我尝试了一些解决方案,但没有一个对我有用。 Can someone please figure out what is the problem?有人可以弄清楚是什么问题吗?

Below is my Python Code:下面是我的 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()

Below is what I run on RaspberryPi terminal and what I get:以下是我在 RaspberryPi 终端上运行的内容以及我得到的内容:

(cv) pi@raspberrypi:~/Downloads/Raspberry_pi_face_recognition-master $ python FaceRecognizer.py
Traceback (most recent call last):
  File "FaceRecognizer.py", line 3, in <module>
    from imutils.video.pivideostream import PiVideoStream
  File "/home/pi/.virtualenvs/cv/lib/python3.5/site-packages/imutils/video/pivideostream.py", line 2, in <module>
    from picamera.array import PiRGBArray
ImportError: No module named 'picamera'

Just Try it for python3试试python3吧

 sudo apt install python3-picamera
 sudo -H pip3 install --upgrade picamera[array]

In code file write this steps:在代码文件中编写以下步骤:

from picamera import PiCamera
camera = PiCamera()

and must be ensure that camera = PiCamera() line is not be in loop.并且必须确保 camera = PiCamera() 行没有循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM