python face detection raspberry pi with picamera


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):


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()


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 <a href="http://picamera.readthedocs.org/en/latest/api_camera.html/#picamera.camera.PiCamera.capture_continuous" rel="nofollow">link</a> 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


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


at the end of the first for loop.


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


  • Displaying unique transactions in Ruby on Rails
  • using GPIO to control raspberry picamera with push button
  • In a 5 day weather array how does one isolate a single time slot per day using swift?
  • simple html dom parser to return all td`s values?
  • To output a string without whitespce
  • cURL - works in browser, not in cURL
  • How to check payment method on a WooCommerce order by id?
  • How do I make powershell search a Word document for wildcards and return the word it found?
  • Android PICASSO - Image Not Loading into ImageView + Stops all Statements following from occuring
  • how to upload and download blackberry application from webserver?
  • How to make UI in blackberry including images in such a way that it works in different screen resolu
  • HTML5 Canvas DrawImage Stutter / Choppiness
  • Merge videos and images using ffmpeg
  • ActionScript: Adding multiple instances of the same swf file to the stage
  • Escape input data in SQL queries when using ODBC + Access
  • SetHidden not working [duplicate]
  • Feeding gstreamer fdsrc via STDIN only produces fraction of expected result
  • How to get label in cells in table to go on to next line instead of getting cut off screen? (xcode 8
  • RETROFIT how to parse this response
  • Maven Assembly Submodules Multimodules
  • Bash: sort find results using part of a filename [duplicate]
  • Apple submission requirements for iPhone apps
  • str_getcsv not parsing the data correctly
  • Calling generic function with 'params' from F# (Observable.StartWith)
  • How does `super` interacts with a class's `__mro__` attribute in multiple inheritance?
  • PID feedback & Position Controller with DC motor/encoder
  • Two totally different sites (on same server) getting mixed up in Google Search results
  • Error “Empty speech response”
  • Why has Haskell troubles resolving “overloaded” operators?
  • Get both date and time in milliseconds
  • How do I install these PHP extensions on Ubuntu?
  • Display Current Video in Windows Phone 8 using AudioVideoCaptureDevice?
  • Hatch area using pcolormesh in Basemap
  • How to get a list with description of all dba packages
  • Date difference with leap year
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Benchmarking RAM performance - UWP and C#
  • CSS Applying specific rule for a specific monitor resolution with only CSS is posible?
  • Angular 2 constructor injection vs direct access
  • IndexOutOfRangeException on multidimensional array despite using GetLength check