Pascal VOC is an XML format used for defining rectangular regions of interest in images. Created for the Visual Object Challenge (VOC), it’s commonly used in machine learning communities as an interchange format for detection labels on images. Here’s an example VOC file:
It’s quick and easy to parse this using Scala. Recent versions of Scala do not ship with an XML parsing library, so be sure to include it in your project with these sbt coordinates:
"org.scala-lang.modules" %% "scala-xml" % "1.3.0"
Note that it’s possible to put other fields into a VOC file but, in general, the most useful bits of information are:
- filename — The location of the image
- objects— The bounding box location in pixel image coordinates. The index uses 1-based counting, and Scala is 0-based, so we’ll subtract 1 from the x and y position when we parse the bndbox.
Here’s our VOC model as Scala case classes:
The parser from XML to case objects is below:
Then to wire it all together:
import import scala.xml.XMLval xml = XML.loadFile(new File("my_voc_file.xml"))
val voc = VocParser.parse(xml)