title: Live image processing with OpenCV and Raspberry Pi
body: <p>recently I'm struggling with speeding up image processing in OpenCV running on my Raspberry Pi 3 platform. I did face recognition application, but it works very slowly. I read many subjects about threading, multiprocessing and so on, but still I'm confused about it. I was testing it with face detection only to make it simplier. Here is my code:</p>
  
  <p><strong>pivideostream.py - updating frames in thread</strong></p>
  
  <pre><code>from picamera.array import PiRGBArray
  from picamera import PiCamera
  from threading import Thread
  import cv2
  
  class PiVideoStream:
  
      def __init__(self, resolution=(640, 480), framerate=30):
          self.camera = PiCamera()
          self.camera.resolution = resolution
          self.camera.framerate = framerate
          self.rawCapture = PiRGBArray(self.camera, size=resolution)
          self.stream = self.camera.capture_continuous(self.rawCapture,format='bgr', use_video_port=True)
          self.image = None
          self.stopped = False
  
      def start(self):
          t = Thread(target=self.update)
          t.daemon = True
          t.start()
          return self
  
      def update(self):
          for frame in self.stream:
              self.image = frame.array
              self.rawCapture.truncate(0)
  
              if self.stopped:
                  self.stream.close()
                  self.rawCapture.close()
                  self.camera.close()
                  return  
  
      def read(self):
          return self.image
  
      def stop(self):
          self.stopped = True
  </code></pre>
  
  <p><strong>process_img_thread.py - main program</strong></p>
  
  <pre><code>from pivideostream import PiVideoStream
  import cv2
  from picamera.array import PiRGBArray
  from picamera import PiCamera
  import time
  
  
  def detect_in_thread():
      # Start updating frames in threaded manner
      face_cascade = cv2.CascadeClassifier('./haarcascades/haarcascade_frontalface_default.xml')
      eye_cascade = cv2.CascadeClassifier('./haarcascades/haarcascade_eye.xml')
      thread_stream = PiVideoStream()
      thread_stream.start()
      time.sleep(2)
  
      # Read frames
      while True:
  
          # Original image
          image = thread_stream.read()
  
          # Full image - face detection
          gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
          faces = face_cascade.detectMultiScale(gray,1.3,5)
          for (x,y,w,h) in faces:
              detected_face = cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),2)
  
              # Region of interest - eyes detection 
              roi_color = image[y:y+h,x:x+w]
              roi_gray = gray[y:y+h,x:x+w]
              eyes = eye_cascade.detectMultiScale(roi_gray,1.03,5,0,(40,40))
              for (ex,ey,ew,eh) in eyes:
                  cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,255),2)
  
          # Show computed image
          cv2.imshow('Threaded Camera OpenCV Preview',image)
  
          if cv2.waitKey(1) &amp; 0xFF == ord("q"):
              break
  
      # Close image window and thread
      cv2.destroyAllWindows()
      thread_stream.stop()
  
  
  if __name__ == "__main__":
      detect_in_thread()
  </code></pre>
  
  <p>When I display raw frames from camera it's working great, but when I only add something in main program in order to process image, video speed is around 1 FPS :(.
  Can someone help me?</p>