Skip to content

Instantly share code, notes, and snippets.

@waveform80
Created June 28, 2015 17:42
Show Gist options
  • Save waveform80/22dea34379d5a7171ce4 to your computer and use it in GitHub Desktop.
Save waveform80/22dea34379d5a7171ce4 to your computer and use it in GitHub Desktop.
A quick demo of using PiRGBAnalysis for (semi) rapid analysis of RGB data
from __future__ import division
import picamera
import picamera.array
import numpy as np
class MyAnalysis(picamera.array.PiRGBAnalysis):
def __init__(self, camera):
super(MyAnalysis, self).__init__(camera)
self.frame_num = 0
def analyse(self, a):
r = int(np.mean(a[..., 0]))
g = int(np.mean(a[..., 1]))
b = int(np.mean(a[..., 2]))
c = (r << 16) | (g << 8) | b
print('Average color: #%06x' % c)
self.frame_num += 1
with picamera.PiCamera() as camera:
camera.resolution = (320, 240)
camera.framerate = 30
output = MyAnalysis(camera)
camera.start_recording(output, 'rgb')
camera.wait_recording(10)
camera.stop_recording()
print('FPS: %.2f' % (output.frame_num / 10))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment