Bacalhau is a platform that allows you to run virtually any program you want (from digital image processing to machine learning and AI) over data stored in IPFS/Filecoin.

For this tutorial I will show you how I used OpenCV with Bacalhau to compute over an image and get to know how “green” it is. We want to do this because we are assuming that the image belongs to a reforestation project and we want to determine the “health” of it. We will keep it simple and use an algorithm called color segmentation.

We will be following the code in this repository in GitHub. The basic steps that we will walk through are:

  1. Run the script locally.
  2. Make a Docker image.
  3. Upload files to IPFS.
  4. Use Bacalhau.

1. Run the script locally

The first step is to have our script working locally in our computer. For our example we are going to need numpy in addition to OpenCV. You can find instructions on how to install them in the repository.

In order to use color segmentation we need to transform the image into HSV format. HSV stands for hue, saturation and value, it is a way to represent an image that facilitates separate it by colors.

import cv2
import numpy as np

image = cv2.imread("../testImages/dron2.jpg")
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
hsv_image = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)

Next, we specify a range of green values in HSV format and create a “mask” using OpenCV .

#Select the range of colors in HSV
light_green = (50, 50, 50)
dark_green = (120, 255, 255)

# Segment image using inRange() function
mask = cv2.inRange(hsv_image, light_green, dark_green)

Finally, we do a bit-wise AND operation to obtain just the pixels where the image is green.

# Bitwise-AND mask and original image
result = cv2.bitwise_and(image, image, mask=mask)