3.5 Workload Applications
3.5.3 Flower Counter
The Flower Counter application processes images captured by cameras positioned at various locations in a farmland by using various image processing techniques to count the approximate number of Canola flowers in the image.
The sequential version of the application reads in multiple images from the local file system or from a database lookup. An object of CanolaTimelapseImage class is created for each image read. This class consists of many methods corresponding to different processing, including readImage for reading the image data, andshowDetectedBlobs for highlighting the detected flowers. Images are read and converted into RGB using Python Imaging Library (PIL). A pre-processing step is done on the input images to filter those that are likely to impact flower estimation results negatively, for example images captured at night and images
captured on a rainy day. To filter the images, sklearn3 implementation of K-Means clustering algorithm is used. K-means is a widely used clustering algorithm for knowledge discovery and data mining. It aims to partition input data objects into k clusters by calculating the nearest mean cluster to which an object belongs.
The filtering stage starts by defining four coordinates representing a square boundary specifying the region of interest within the image where the flowers would be counted. The next stage computes histograms of each individual input image (pixel) data in both greyscale and CIELAB colour space. Each pixel in the input image is grouped into one of 256 bins. All the input images are passed into a method that computes histogram shifts for each image with respect to the average histograms of Lab colour space. This computation involves selecting the first image histogram as a reference and computing the discrete cross-correlation between the reference image histogram and the histograms of all other input images. Furthermore, a correlation reference is computed by cross-correlating the reference histogram and the average histograms of all the input images. The correlation value of each input image plus the correlation reference computed is returned as the histogram shift of that image. Images are flagged as good or corrupted based on the value of their greyscale histogram value.
The images that are flagged as not corrupted are passed to the K-Means clustering stage. This stage groups all the input images into 2 clusters (namely cluster 0 and cluster 1) based on the percentage of yellow pixels within the image plot boundary. The images in cluster 0 are the most suitable for the flower counting algorithm and thus used in the next stages of the flower counting pipeline. The selected images are sorted from high to low based on the percentage of yellow pixels. Thus, the image with the highest number of yellow pixels is the first on the list. This sorted list of images is passed to the flower counting method which runs a pipeline of various image processing techniques to estimate the number of Canola flowers in each input image. The processing pipeline includes converting the images from RGB to CIELAB colour space, computing sigmoid mapping, and finally detecting blobs within the defined image region using scikit-image Determinant of Hessian (DoH) blob detection algorithm. For each blob found, the blob detection method returns its coordinates and the standard deviation of the Gaussian kernel that detected the blob. The number of flowers were estimated by counting the number of the returned blobs. The output of the Flower Counting application is a text file containing image names and estimated number of Canola flowers per image. This text file is written to a local file system for analysis.
Due to the large number of images, and the various processing involved in this application, it takes a large amount of time to complete. However, unlike the Image Registration application which is embarrassingly parallel, the stages in the Flower Counter can be categorised into two classes: 1) Stages that process a single image independent of other images such as the final flower counting stage, and 2) Stages that require information from all other input images such asK-Means clustering stage.
To distribute the processing of the Flower Counter application on multiple computing nodes, I re-write
the sequential version of this application, which uses pure Python, into a distributed Spark application usingpyspark’s transformations and actions. Similar to the sequential version, the distributed version of the application uses the same set of image processing pipelines. The main difference is that the Spark version of the application reads input images in a .jpg format from HDFS. Spark’sbinaryFilesis used to create an RDD of all the images for a single Flower Counter job. Each image file is read as a single record of a key-value pair, where the key is the path of the file, and the value is the content. A map transformation is used to create a new RDD of the images converted from binary files into Python’s numpy array of image data.
Spark provides many functions that can process RDD partitions in parallel on multiple executors. One of such function is theaggregate transformation. It aggregates the elements of each RDD partition, and then the results for all the partitions, using a givencombine function. This transformation was used to calculate the average histogram of pixels over all input images in parallel. Other Spark transformations and actions including map, filter, zipWithIndex, sortBy, coalesce and saveAsTextFile were used for executing various processing in parallel. For the K-Means clustering stage, Spark’s machine learning library MLlib provides an efficient implementation of the K-Means clustering algorithm that includes a parallelized variant of k- means++. This implementation ofK-Means performs processing in parallel; therefore, it was used to replace thesklearn implementation in the Spark version of the Flower Counter application. The output of the final stage (that is the flower counting stage) is reduced into 1 RDD partition usingcoalesce, and saved to HDFS as a text file containing the image file name and the estimate of Canola flowers count per image. This final stage involves shuffling output data over the network.