Skip to content

Instantly share code, notes, and snippets.

@kamathln
Last active March 22, 2016 05:38
picamera to Numpy : File like helper class to read an image from picamera into numpy ndarray , usefull for image processing, especially with opencv
# Copyright (c) 2016 Laxmianarayan G Kamath A [email protected]
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#Usage:
# import rpi2numpy
# rn = rpi2numpy.Rpi2Numpy()
# .. code here .. probably start a loop
# i = rn.read()
# .. process it
# rn.capture.close()
# capture = picamera.PiCamera()
# width, height = capture.resolution
# rpif = Rpi2NumpyFile(width,height)
# capture.capture(rpif, format='bgr'
# image = rpif.get_image()
#Points to note if you are trying to understand this code:
# % The function "capture" from "picamera" module expects a *file-like* object which has the "write" method to dump it's data into
# % It will dump the format expected by opencv when you as for capture in 'bgr' format.
# % numpy can read data from string using the "fromstring" function into its array.
import picamera
import time
import numpy
class Rpi2NumpyFile(object):
def __init__(self,width,height,format='bgr',videoport=True,depth=3):
self.image = None
self.data = None
self.width = width
self.height = height
self.format = format
self.depth = depth
self.videoport = videoport
def open(self):
return True
def write(self,data):
self.data = data
def get_image(self):
assert self.data is not None, "No Image read yet"
return numpy.frombuffer(self.data,dtype=numpy.uint8).reshape(self.height, self.width, self.depth)
def close(self):
return True
class Rpi2Numpy(object):
def __init__(self,*cameraParams, **cameraKeyParams):
self.capture = picamera.PiCamera(*cameraParams, **cameraKeyParams)
width,height = self.capture.resolution
self.r2n = Rpi2NumpyFile(width,height)
def read(self):
self.capture.capture(self.r2n,format=self.r2n.format, use_video_port = self.r2n.videoport)
return self.r2n.get_image()
if __name__ == '__main__':
rn = Rpi2Numpy(resolution=(1024,768))
rn.capture.video_denoise = False
rn.capture.video_stabilization = False
rn.capture.image_denoise = False
rn.capture.exposure_mode ='off'
numframes = 100
t1 = time.time()
for i in xrange(0,numframes):
j = rn.read()
t2 = time.time()
t3 = time.time()
j = rn.read()
t4 = time.time()
print "Num of Frames: %d" %(numframes)
print "Time Taken %f seconds" % (t2 - t1)
print "FPS: %f " % (numframes/ (t2 - t1))
print "Time Taken for single shot %f seconds" % (t4 - t3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment