A very useful functionality was added to OpenCV’s DNN module: a Tensorflow net importer.




     To use the DNN, the opencv_contrib is needed, make sure to install it. This article is focused on the Python language, where the function has the following format:

As you might have seen, to use it, two files are needed:

  • frozen_inference_graph.pb
  • graph.pbtxt

 

About Tensorflow’s .pb and .pbtxt files

     Tensorflow models usually have a fairly high number of parameters. Freezing is the process to identify and save just the required ones (graph, weights, etc) into a single file that you can use later. So, in other words, it’s the TF way to “export” your model. The freezing process produces a Protobuf ( .pb) file.

     The good news is: There are a bunch of trained, optimized and widely used models on the Tensorflow’s detection model zoo repository that you can use freely. You won’t need to train one (if the available models, trained with well know datasets, fit your needs).

     Additionally, OpenCV requires an extra configuration file based on the .pb, the .pbtxt. It is possible to import your own models and generate your own .pbtxt files by using one of the following files from the OpenCV Github repository.

     If you want to use pre-trained models, the amazing OpenCV community already did the hard work for the following models.

 

ModelVersion
MobileNet-SSD v12017_11_17weightsconfig
MobileNet-SSD v1 PPN2018_07_03weightsconfig
MobileNet-SSD v22018_03_29weightsconfig
Inception-SSD v22017_11_17weightsconfig
Faster-RCNN Inception v22018_01_28weightsconfig
Faster-RCNN ResNet-502018_01_28weightsconfig
Mask-RCNN Inception v22018_01_28weightsconfig
Font: OpenCV’s Github wiki.

The Python code

The entire import/use procedure could be split into 5 steps:

  • Load your model using the downloaded files;
  • Load your images;
  • Use those images as network inputs;
  • Get the output with the detected objects.

 

Take a look at the Python code snippet for doing these steps:

 

Output examples

 

OpenCV+Tensorflow output OpenCV+Tensorflow output

 

 

 

Have fun with your projects! 
If this post helped you, please consider buying me a coffee 🙂