How to process Instagram images with Spark 1.1.0 and OpenCV (with Java)
by Ruben TousInstallation: You will need
Installation: You will need
- Apache Spark 1.1.0
- OpenCV 2.4.9 or 3.0.0 (you can follow this guide)
- Apache Maven
Preparing the data
We will have thousands of .jpg files, along with their corresponding .txt files (with json metadata). On the one hand, it is not convenient to store all the files within the same directory. On the other hand, it is not convenient to access the files one by one from Spark. So, we will need to perform some previous steps:
- When capturing the images it is better to save them using a hierarchy of directories (e.g. with max 1000 files for directory). If you already have the images within a big directory you can try directly performing the next steps or you can split the directory with this script
- Pack all the .jpg files from a directory (500 files) into a tarball. Pack all the .txt files (500 files) into a different tarball
- Transform each tarball in a Hadoop SequenceFile with this tool by Stuart Sierra
Building the code
Create the following directory structure (let's call the root YOUR_APP_PATH):
Edit the pom.xml file:
This pom.xml uses the maven-assembly-plugin to pack all the dependent jars (except for the Spark-related jars) within a single .jar. This will make easier the submission of the application to Spark.
Now edit Main.java. This class 1) reads the images from the SequenceFile into a key-value RDD (key will be the filename) and uses OpenCV to determine the width of the images, 2) reads the metadata from the SequenceFile into another key-value RDD, 3) joins both RDD into a single RDD where the value is a tuple (image width, username)
Before building it is necessary to import the OpenCV .jar (opencv-249.jar or opencv-300.jar) into the local Maven respository. Type the following:
In order to build, simply type:
Execute on Spark 1.1.0
Type the following from
The previous command tells Spark where to find the native OpenCV library. However, if you copy the library in /usr/lib/ it would work without the --driver-library-path directive.