Skip to content

Instantly share code, notes, and snippets.

@hohonuuli
Last active March 15, 2021 23:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hohonuuli/9c73d7abe064a8449468d35eb708d436 to your computer and use it in GitHub Desktop.
Save hohonuuli/9c73d7abe064a8449468d35eb708d436 to your computer and use it in GitHub Desktop.
Scala VOC parser. For Medium article
import scala.xml.Elem
object VocParser {
def parse(xml: Elem): Voc = {
val filename = (xml \ "filename").text
val objects = xml \ "object"
val vocObjects = objects.map(n => {
val name = (n \ "name").text
// VOC is 1-based index. Convert to 0 based.
// See https://cv.gluon.ai/_modules/gluoncv/data/pascal_voc/detection.html#VOCDetection
val xmin = (n \ "bndbox" \ "xmin").text.toInt - 1
val ymin = (n \ "bndbox" \ "ymin").text.toInt - 1
val xmax = (n \ "bndbox" \ "xmax").text.toInt - 1
val ymax = (n \ "bndbox" \ "ymax").text.toInt - 1
VocObject(name, xmin, ymin, xmax, ymax)
});
Voc(filename, objects = vocObjects)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment