PiCam: Difference between revisions
Line 43: | Line 43: | ||
This [http://www.pyimagesearch.com/2015/03/30/accessing-the-raspberry-pi-camera-with-opencv-and-python/ tutorial on pyimagesearch.com] is very useful. | This [http://www.pyimagesearch.com/2015/03/30/accessing-the-raspberry-pi-camera-with-opencv-and-python/ tutorial on pyimagesearch.com] is very useful. | ||
Adapting the opt_flow.py opencv sample to do motion detection on the pi. | |||
<source lang="python"> | |||
from picamera.array import PiRGBArray | |||
from picamera import PiCamera | |||
import time | |||
import cv2 | |||
import numpy as np | |||
# initialize the camera and grab a reference to the raw camera capture | |||
camera = PiCamera() | |||
# framesize = (640, 480) | |||
framesize = (160, 120) | |||
camera.resolution = framesize | |||
camera.framerate = 32 | |||
rawCapture = PiRGBArray(camera, size=framesize) | |||
# allow the camera to warmup | |||
time.sleep(0.25) | |||
prevgray = None | |||
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True): | |||
img = frame.array | |||
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | |||
if prevgray != None: | |||
flow = cv2.calcOpticalFlowFarneback(prevgray, gray, 0.5, 3, 15, 3, 5, 1.2, 0) | |||
fx = flow[:,:,0] | |||
fy = flow[:,:,1] | |||
mag = np.sqrt(fx*fx+fy+fy) | |||
mag = np.nan_to_num(mag) | |||
print mag.max() | |||
# print mag[0][0], mag[10][10] | |||
prevgray = gray | |||
# clear the stream in preparation for the next frame | |||
rawCapture.truncate(0) | |||
</source> |
Revision as of 13:47, 15 March 2017
connecting the camera
gently lift the black clip by pushing upward. insert the ribbon cable of the camera firmly down. the blue side goes towards the headphone jack! push the clip down again.
See:
setup
warning: this info is in progress -- may not (yet) represent a useful thing
- ok this link in the end was totally misleading -- in fact you (maybe?) just need to add some stuff to /boot/config.txt
(the audio thing is unrelated to the camera, but may be useful -- I think it forces audio to the mini jack?! ie not to HDMI)
/boot/config.txt
# Enable audio (loads snd_bcm2835) dtparam=audio=on start_x=1 gpu_mem=128
test the camera
raspistill -o test.jpg
connecting via python
See this tutorial on raspberrypi.com. The bird box tutorial is also quite interesting relating to how the camera responds to infrared, and the fact that the "fixed focus" camera can be adapted to manually focus at various lengths.
from picamera import PiCamera
from time import sleep
camera = PiCamera()
camera.rotation=270
camera.start_preview()
sleep(10)
camera.stop_preview()
using python + opencv
This tutorial on pyimagesearch.com is very useful.
Adapting the opt_flow.py opencv sample to do motion detection on the pi.
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
import numpy as np
# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
# framesize = (640, 480)
framesize = (160, 120)
camera.resolution = framesize
camera.framerate = 32
rawCapture = PiRGBArray(camera, size=framesize)
# allow the camera to warmup
time.sleep(0.25)
prevgray = None
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
img = frame.array
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
if prevgray != None:
flow = cv2.calcOpticalFlowFarneback(prevgray, gray, 0.5, 3, 15, 3, 5, 1.2, 0)
fx = flow[:,:,0]
fy = flow[:,:,1]
mag = np.sqrt(fx*fx+fy+fy)
mag = np.nan_to_num(mag)
print mag.max()
# print mag[0][0], mag[10][10]
prevgray = gray
# clear the stream in preparation for the next frame
rawCapture.truncate(0)