title: RaspberryPi camera stream processing with Python 2.7
body: <p>I found following code <a href="https://picamera.readthedocs.org/en/release-1.10/recipes2.html" rel="nofollow">here</a> for catching a video stream from the RaspberryPi camera.</p>
  
  <p>Now I want to process the video stream (e.g. finding BLOB or display the video).</p>
  
  <p>For this purpose I need to catch the content of the video as array, e.g. with the name <code>frame</code> in this example:</p>
  
  <pre><code>cv2.imshow('stream', frame)
  </code></pre>
  
  <p>I tried it by converting it into an array like</p>
  
  <pre><code>frame = self.stream.array
  </code></pre>
  
  <p>but somewhat I am not getting it to work.</p>
  
  <p>The output tells me that I cannot create an array with the stream:</p>
  
  <pre><code>File "Raspberry.py", line 35, in run
      frame = self.stream.array   #creating an array for the stream
  AttributeError: '_io.BytesIO' object has no attribute 'array'
  </code></pre>
  
  <p>Any ideas how to solve it?</p>
  
  <p>Code:</p>
  
  <pre><code>import io
  import time
  import threading
  import picamera
  
  # Create a pool of image processors
  done = False
  lock = threading.Lock()
  pool = []
  
  class ImageProcessor(threading.Thread):
      def __init__(self):
          super(ImageProcessor, self).__init__()
          self.stream = io.BytesIO()
          self.event = threading.Event()
          self.terminated = False
          self.start()
  
      def run(self):
          # This method runs in a separate thread
          global done
          while not self.terminated:
              # Wait for an image to be written to the stream
              if self.event.wait(1):
                  try:
                      self.stream.seek(0)
  
                      frame = self.stream.array   #creating an array for the stream                   
  
                      cv2.imshow('stream', frame) #should show the livestream
  
  
  
                      # Read the image and do some processing on it
                      #Image.open(self.stream)
                      #...
                      #...
                      # Set done to True if you want the script to terminate
                      # at some point
                      #done=True
                  finally:
                      # Reset the stream and event
                      self.stream.seek(0)
                      self.stream.truncate()
                      self.event.clear()
                      # Return ourselves to the pool
                      with lock:
                          pool.append(self)
  
  def streams():
      while not done:
          with lock:
              if pool:
                  processor = pool.pop()
              else:
                  processor = None
          if processor:
              yield processor.stream
              processor.event.set()
          else:
              # When the pool is starved, wait a while for it to refill
              time.sleep(0.1)
  
  with picamera.PiCamera() as camera:
      pool = [ImageProcessor() for i in range(4)]
      camera.resolution = (640, 480)
      camera.framerate = 30
      camera.start_preview()
      time.sleep(2)
      camera.capture_sequence(streams(), use_video_port=True)
  
  # Shut down the processors in an orderly fashion
  while pool:
      with lock:
          processor = pool.pop()
      processor.terminated = True
      processor.join()
  </code></pre>