Analyze an image from Raspberry Pi on Bluemix

In this Node-RED flow on Raspberry Pi, the application performs these tasks:

    • Receives the command sent from Node-RED on Bluemix
    • Gets a picture from the Raspberry Pi file system and optionally takes a picture with the RaspCam and saves it
    • Sends this encoded picture and its name to Bluemix for cognitive analysis by using the Visual Recognition service

Before you use the camera or Internet pictures, import a flow from GitHub:

  1. Go to this GitHub page and click Raw.
  2. Copy all the raw code to your clipboard.
  3. In the flow editor on the Raspberry Pi, create a new tab.
  4. Click the Menu icon > Import > Clipboard.
  5. In Step 4, Add a Cloudant database node, you will install the Cloudant nodes that are a part of the import, if you did not already do this task described in Lab 1.

Decide how you want to get images to analyze:

Download pictures from the Internet

Follow these instructions if you aren’t using the RaspCam.

  1. Find a link to an image (PNG or JPEG) on the Internet that is less than 2 MB. For example, copy this link:
  2. Download the image and save it with the name example.jpg on the Raspberry Pi in the following directory:
    Important: Do not change the file name. The image must be named example.jpg.
  3. Because you’re not using a camera, change the code in the take a pic function node in the flow. Double-click the node and paste in this code under Function:
    var pictureFilename = "/home/pi/pictures/example.jpg";
    var currTime = new Date().getTime();

    // Providing the above generated file name to the next nodes
    return {payload: pictureFilename,
    filename: pictureFilename,
    filedate: currTime};

  4. Double-click the json function node on the Raspberry Pi and change the file type after “value” to this: “data:image/jpg;base64,” var picId = msg.filename;
    var picDate = msg.filedate;
    var encoded = msg.payload;
    var len = encoded.length;

    data = {
    "value": "data:image/jpg;base64," + msg.payload,
    "pic_date" : picDate

    return [{payload:JSON.stringify(data)}, {payload: picId}];

Take a picture on the Raspberry Pi with a RaspCam camera

Follow these instructions if you are using the RaspCam.

Connecting the camera:

  1. Disconnect the Raspberry Pi from the power source.
  2. Connect the camera to the provided slot.
  3. Reconnect the Raspberry Pi to the power source.
  4. Set up the system to initialize the camera in the operating system.
    When system reinitializes, you should be able to install the camera and take pictures. See the Raspberry Pi documentation for information about initializing the camera.
  5. Expose the system variable so that you can call it from the Node-RED on Raspberry Pi. Go to the terminal and enter the following commands:
    cd ~/.node-red
    npm install raspicam
    pico ~/.node-red/settings.js

    If you can’t find the settings.js file in the ~/.node-red folder, look for this file in /usr/lib/node_modules/node-red/ and then copy it to the ~/.node-red folder.
  6. Update the functionGlobalContext settings.js file with the following code:
    functionGlobalContext: {

    When this variable is set, you can reference it from Node-RED:
    var camera = new opts );
  7. Update the flow to add the function to take a picture. Double-click the take a pic function node on the Raspberry Pi and add the function to execute the following code:
    var encoding = "png";
    var currTime = new Date().getTime();
    // Using the current timestamp to ensure the uniquness of the picture
    var pictureFilename = "/home/pi/pictures/" + currTime + "." + encoding;
    var opts = {
    mode: "photo",
    encoding: encoding,
    quality: 50,
    width: 600,
    height: 400,
    output: pictureFilename,
    timeout: 1};
    // RaspiCam to create a camera object.
    var camera = new opts );

    // Take a pic with RaspiCam
    var process_id = camera.start( opts );

    // Providing the above generated file name to the next nodes in a payload.
    return {payload: pictureFilename,
    filename: pictureFilename,
    filedate: currTime};