PiCam: Difference between revisions

From XPUB & Lens-Based wiki
Line 86: Line 86:
== Sending the data from python to puredata via OSC ==
== Sending the data from python to puredata via OSC ==
Using [https://trac.v2.nl/wiki/pyOSC Mr. Stock's classic OSC.py] module. You can simply download it and place in the same folder as your python script (no need to pip if you don't wanna).
Using [https://trac.v2.nl/wiki/pyOSC Mr. Stock's classic OSC.py] module. You can simply download it and place in the same folder as your python script (no need to pip if you don't wanna).
== UNCLASSIFIED NOTES ==
remember to put on udp on your pdsend (otherwise it's trying tcp and says connection refused ... annoying)
  pdsend 8001 localhost udp

Revision as of 16:27, 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: connect-camera.jpg

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)

Sending the data from python to puredata via OSC

Using Mr. Stock's classic OSC.py module. You can simply download it and place in the same folder as your python script (no need to pip if you don't wanna).


UNCLASSIFIED NOTES

remember to put on udp on your pdsend (otherwise it's trying tcp and says connection refused ... annoying)

 pdsend 8001 localhost udp