4.1 Pipeline: Adding Bounding Boxes to a Photo


Many of the computer vision models will identify bounding boxes for objects within a photo. For example, the faces command of the azcv package returns as the first field the bounding box of any faces found in a photo, one face per line.

A relatively simple pipeline can add the bounding boxes for the identified faces to the image.

As an example, we first download an image from the Internet saving it as the file faces.jpg using wget.

wget https://bit.ly/38GgwPP -O faces.jpg

The 12 model is then called upon to identify the faces, saving the output to a text file faces_bb.txt, containing the bounding boxes.

ml faces azcv faces.jpg | tee faces_bb.txt

This text file is concatenated to the cut command to extract the first field (-f1) where fields are denoted by a comma (-d,). This field is the bounding box of each face. Using xargs and awk a command is constructed using convert from imagemagick to draw the blue rectangles of width 3 pixels for each of the identified faces, saving the resulting image as faces_tmp.png.

cat faces_bb.txt |
  cut -d, -f1 |
  xargs printf '-draw "rectangle %s,%s %s,%s" ' |
  awk '{print "faces.jpg -fill none -stroke blue -strokewidth 3 " $0 "faces_tmp.png"}' |
  xargs convert

If a polygon of 4 points rather than a rectangle is returned by the model, then:

$ ml detect azface 3818.jpg |
  grep forehead_occluded |
  cut -d, -f1 | 
  xargs printf "-draw 'polygon %s,%s %s,%s %s,%s %s,%s' " |
  awk '{print "3818.jpg -fill none -stroke red -strokewidth 5 " $0 "bb.png"}' |
  xargs convert

Your donation will support ongoing availability and give you access to the PDF version of this book. Desktop Survival Guides include Data Science, GNU/Linux, and MLHub. Books available on Amazon include Data Mining with Rattle and Essentials of Data Science. Popular open source software includes rattle, wajig, and mlhub. Hosted by Togaware, a pioneer of free and open source software since 1984. Copyright © 1995-2022 Graham.Williams@togaware.com Creative Commons Attribution-ShareAlike 4.0