Raspberry Pi - Camera Interface with Python
Take a picture with a button press
import time
import picamera
import RPi.GPIO as GPIO
import numpy as np
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11,GPIO.IN)
# 8Mpx (3280,2464)
# 5Mpx (2592,1944)
h = 2592
v = 1944
a = np.zeros((v,h,3), dtype=np.uint8)
a[v/2, :, :] = 0xff
a[:, h/2, :] = 0xff
#set left or right camera
def take_pic():
with picamera.PiCamera() as camera:
camera.resolution = (h, v)
camera.vflip = True
camera.iso = 800
#o = camera.add_overlay(np.getbuffer(a), layer=3, alpha=64)
#camera.start_preview() #0720243637
time.sleep(2)
#camera.capture('foo.jpg', resize=(400,240))
ofile = 'cam%s_%d.jpg' % ('r',time.time())
camera.capture(ofile)
#camera.remove_overlay(o)
prev = GPIO.input(11)
while True:
curr = GPIO.input(11) # take a reading
if ((not prev) and curr):
print("button pressed")
take_pic()
#take pic
prev = curr
time.sleep(0.05)
Add an overlay to a live camera image
import time
import picamera
import numpy as np
h = 800 #2592
v = 480 #1944
a = np.zeros((v,h,3), dtype=np.uint8)
a[v/2, :, :] = 0xff
a[:, h/2, :] = 0xff
with picamera.PiCamera() as camera:
camera.resolution = (h,v)
camera.vflip = Ture
camera.framerate = 24
camera.start_preview()
o = camera.add_overlay(np.getbuffer(a), layer=3, alpha=64)
try:
while True:
time.sleep(1)
finally:
camera.remove_overlay(o)