Set up the Raspberry Pi Camera.

Requirements

  • Raspi w/ camera (Lookout has been tested with PiCamera module v1 and v2)
  • Python3, Numpy, OpenCV

These steps were performed on a Raspi3 with a clean DietPi image “DietPi_v150_RPi-armv6-(Jessie)” - the image should work for any model Raspi.

Python3, Numpy, OpenCV Installation

  1. Install Python3 and system dependencies for OpenCV
    sudo apt-get install python3 python3-pip python3.4-dev libpython3-dev python3-numpy
    sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
    sudo apt-get install libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
    
  2. Install OpenCV 3.3.0 - steps copied (slightly modified) from this SO post. This step may take 30+ minutes (mostly for OpenCV compile).
    git clone https://github.com/opencv/opencv
    cd opencv
    git checkout 3.3.0
    mkdir build
    cd build
    cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
    make -j4
    sudo make install
    
  3. Check cv2 import in python shell to validate installtion:
    python3
    >>> import cv2
    >>> cv2.__version__
    '3.3.0'
    >>>
    

Configure Lookout Camera Thing

The Lookout “camera thing” is a python module that interacts with AWS as an “IoT Thing”. Since MQTT messages are limited in size, the module also puts images directly to AWS S3 for analysis.

  1. Configure AWS credentials. These credentials are only used for boto3 to put images in S3 - best practice is to create an IAM user with put permission to your S3 bucket (instead of using your admin credentials).
    mkdir ~/.aws
    vi ~/.aws/credentials
    
    • Enter the following values:
      [default]
      aws_access_key_id = YOUR_ACCESS_ID
      aws_secret_access_key = YOUR_SECRET_ACCESS_KEY
      
  2. Install python module dependencies
    sudo pip3 install AWSIoTPythonSDK
    sudo pip3 install picamera
    sudo pip3 install boto3
    
  3. Enable the raspi camera module.

    • For Raspian-based distros use raspi-config to enable the camera module and reboot.

    • For DietPi, vi /DietPi/config.txt, set start_x=1, then reboot.

  4. Get the lookout code
    git clone https://github.com/2412labs/lookout.git
    cd lookout/device/camera
    sudo python3 setup.py install
    
  5. Set up an IoT thing in AWS.
    1. In the AWS console navigate to services -> internet of things -> AWS IoT
    2. Create a thing, give it a name you will remember
    3. Create a thing policy with the following values (from the IoT console, secure -> policies -> create):
      {
      "Version": "2012-10-17",
      "Statement": [
          {
              "Effect": "Allow",
              "Action": [
                  "iot:*"
              ],
              "Resource": [
                  "*"
              ]
          }
      ]
      }
      
    4. Create a certificate (from the IoT console, secure -> certificates -> create -> “One-click certificate creation”)
    5. Save thing cert values (pem, private key, public key) - this is a copy paste from UI, and the ONLY time they will be displayed (if you don’t save here, you will need to regenerate new certs).
    6. Click Activate to enable the certs
    7. Click “Attach to policy” and select the policy you created.
  6. Copy the certs from prior step to lookout/device/camera/example/certs/, you will need to enter the cert names in the conf.json in the next step.
  7. Create and update the conf.json file:
    cd lookout/device/camera/example
    cp conf_example.json conf.json
    vi conf.json
    
    • Update values to match your AWS deployment (you can find your IoT host on in the IoT section of your AWS console):
      {
        "iotConfig": {
            "region": "us-east-1",
            "thingName": "lo-iot-cam1",
            "iotHost": "YOUR_IOT_HOST.iot.us-east-1.amazonaws.com",
            "iotPort": 8883,
            "rootCert": "certs/root.pem",
            "thingCert": "certs/cert.pem",
            "thingPrivateKey": "certs/private.key"
        },
        "s3Bucket": "lookout-stack-s3bucket-YOUR_BUCKET_ID",
        "deviceTopic": "lo/lo-iot-cam1",
        "eventTopic": "lo/event"
      }
      
  8. Run python3 -u cameraThingExample.py -c conf.json

If everything is set up correctly you should see some diagnostic log messages.

Run Lookout Camera Thing as a Service

The example folder contains a shell script to install the camera thing as a service that will start when the pi boots. Make sure you test the script with the previous commands before running as a service!

sudo apt-get install runit
cd lookout/device/camera/example
./installAsService.sh

The script copies contents of the example folder to /usr/local/camerathing and creates a systemd service. If you need to change any settings in conf.json after installation (or update thing certs), be sure to edit the files in /usr/local/camerathing.

The runit package contains the svlogd logging tool, which manages log size, rotation, deletion, etc. The shell script run by the service uses svlogd to send stdout and stderr to /var/log/camerathing/. If you want to use a different logging scheme you can edit the run.sh file.

Related Posts