2

I am a newbie with python and opencv i am trying to build a face detection project with raspberry pi. i am getting this error and here is my code

Traceback (most recent call last):

 File "/home/pi/Desktop/picamera-code/FaceDetection1.0", line 19, in <module>
for frame in 
    camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):

Code:

import numpy as np
import cv2
from picamera.array import PiRGBArray
from picamera import PiCamera
import time


camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 32
rawCapture = PiRGBArray(camera, size=(640, 480))

time.sleep(0.1)



face_cascade =  cv2.CascadeClassifier('/home/pi/Downloads/haarcascade_frontalface_default.xml')

for frame in camera.capture_continuous(rawCapture, format="bgr",  use_video_port=True):

    img=np.asarray(frame.array)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    for (x,y,w,h) in faces:
        img = cv2.Rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = img[y:y+h, x:x+w]


cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Shudy
  • 7,806
  • 19
  • 63
  • 98
Mark Mercieca
  • 23
  • 1
  • 3

3 Answers3

5

The problem is in your camera.capture_continuos. First value, output, cannot be just an array as it records with an infinite iteration as the docs says. Instead of this you should put an output file. If you want an stream to capture this you can use the io.Bytes as well.

In this link it explains you examples on how tu use the frame and where should you redirect the output.

You can do something like what suggest on the API docs. Take the stream and truncate it to get the image that you are currently getting:

import io
import time
import picamera
with picamera.PiCamera() as camera:
    stream = io.BytesIO()
    for foo in camera.capture_continuous(stream, format='jpeg'):
    # YOURS:  for frame in camera.capture_continuous(stream, format="bgr",  use_video_port=True):
        # Truncate the stream to the current position (in case
        # prior iterations output a longer image)
        stream.truncate()
        stream.seek(0)
        if process(stream):
            break
Ricardo Burillo
  • 1,246
  • 11
  • 14
3

The correct answer is that you need to truncate the stream at the end of the loop. Add

rawCapture.truncate(0) 

at the end of the first for loop.

josedlujan
  • 5,357
  • 2
  • 27
  • 49
thomas
  • 31
  • 1
0

if you change the part in line 11 640, 420 to 160, 120 it should work

  • 1
    Welcome to Stack Overflow! I recommend you [take the tour](http://stackoverflow.com/tour). When giving an answer it is preferable to give some explanation as to WHY your answer is the one. – Stephen Rauch Jan 27 '17 at 01:27