Extracting image metadata

Up too this point, we have loaded and pre-processed raw images, but we have no idea about the image metadata that we need to make our CNNs learn. Thus, it's time to load the CSV files that contain metadata about each image.

I wrote a method to read such metadata in CSV format, called readMetadata(), which is used later on by two other methods called readBusinessLabels and readBusinessToImageLabels. These three methods are defined in the CSVImageMetadataReader.scala script. Here's the signature of the readMetadata() method:

def readMetadata(csv: String, rows: List[Int]=List(-1)): List[List[String]] = {
val src = Source.fromFile(csv)

def reading(csv: String): List[List[String]]= {
src.getLines.map(x => x.split(",").toList)
.toList
}
try {
if(rows==List(-1)) reading(csv)
else rows.map(reading(csv))
}
finally {
src.close
}
}

The readBusinessLabels() method maps from business ID to labels of the form businessID → Set (labels):

def readBusinessLabels(csv: String, rows: List[Int]=List(-1)): Map[String, Set[Int]] = {
val reader = readMetadata(csv)
reader.drop(1)
.map(x => x match {
case x :: Nil => (x(0).toString, Set[Int]())
case _ => (x(0).toString, x(1).split(" ").map(y => y.toInt).toSet)
}).toMap
}

The readBusinessToImageLabels () method maps from image ID to business ID of the form imageIDbusinessID:

def readBusinessToImageLabels(csv: String, rows: List[Int] = List(-1)): Map[Int, String] = {
val reader = readMetadata(csv)
reader.drop(1)
.map(x => x match {
case x :: Nil => (x(0).toInt, "-1")
case _ => (x(0).toInt, x(1).split(" ").head)
}).toMap
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset