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:
1 | cv2.dnn.readNetFromTensorflow('frozen_inference_graph.pb', 'graph.pbtxt') |
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.
| Model | Version | ||
|---|---|---|---|
| MobileNet-SSD v1 | 2017_11_17 | weights | config |
| MobileNet-SSD v1 PPN | 2018_07_03 | weights | config |
| MobileNet-SSD v2 | 2018_03_29 | weights | config |
| Inception-SSD v2 | 2017_11_17 | weights | config |
| Faster-RCNN Inception v2 | 2018_01_28 | weights | config |
| Faster-RCNN ResNet-50 | 2018_01_28 | weights | config |
| Mask-RCNN Inception v2 | 2018_01_28 | weights | config |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | # How to load a Tensorflow model using OpenCV # Jean Vitor de Paulo Blog - https://jeanvitor.com/tensorflow-object-detecion-opencv/ import cv2 # Load a model imported from Tensorflow tensorflowNet = cv2.dnn.readNetFromTensorflow('frozen_inference_graph.pb', 'graph.pbtxt') # Input image img = cv2.imread('img.jpg') rows, cols, channels = img.shape # Use the given image as input, which needs to be blob(s). tensorflowNet.setInput(cv2.dnn.blobFromImage(img, size=(300, 300), swapRB=True, crop=False)) # Runs a forward pass to compute the net output networkOutput = tensorflowNet.forward() # Loop on the outputs for detection in networkOutput[0,0]: score = float(detection[2]) if score > 0.2: left = detection[3] * cols top = detection[4] * rows right = detection[5] * cols bottom = detection[6] * rows #draw a red rectangle around detected objects cv2.rectangle(img, (int(left), int(top)), (int(right), int(bottom)), (0, 0, 255), thickness=2) # Show the image with a rectagle surrounding the detected objects cv2.imshow('Image', img) cv2.waitKey() cv2.destroyAllWindows() |
Output examples
Have fun with your projects!
If this post helped you, please consider buying me a coffee 🙂
59 Comments
Leave a Reply
You must be logged in to post a comment.







Hello Jean! Have you ever exported your custom model to .pb , .pbtxt and read it in opencv?
I have never tried custom ones. If you manage to try your own, please report back your experience!
Is there is any way to put labels on detected objects
hello, I’m getting the following error while trying to load a tensorflow model ssd mobilenet V2. Using the pretrained model in Model Zoo everything works fine, but after a fine tuning of the model I get error. Could it be the version of tensorflow used for finetuning? Currently I’m using Tf 1.15. Thanks
Can’t create layer “FeatureExtractor/MobilenetV2/Conv/BatchNorm/FusedBatchNormV3” of type “FusedBatchNormV3” in function ‘getLayerInstance’
hi all and thanks for help
when i’m using my custom object detection
tensorflowNet = cv2.dnn.readNetFromTensorflow(‘models/my_custom.pb’, ‘models/my_custom.pbtxt’)
i get error :
networkOutput = tensorflowNet.forward()
cv2.error: OpenCV(4.1.1) C:\projects\opencv-python\opencv\modules\dnn\src\dnn.cp
p:693: error: (-215:Assertion failed) inputs.size() == requiredOutputs in functi
on ‘cv::dnn::dnn4_v20190621::DataLayer::getMemoryShapes’
hey jean,i am using this code for my custom tensorflow graph of mobilenet ssd,but getting this error:
error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\dnn\src\dnn.cpp:730: error: (-215:Assertion failed) inputs.size() == requiredOutputs in function ‘cv::dnn::dnn4_v20191202::DataLayer::getMemoryShapes’
could you help me please.
Hi,
Please check your images (resolution, channels, format…) to match with what your network expects.
Hi Jean, may I know how to generate tensorflow .pb file to .pbtxt?
I have yolo v3 model with cfg and weights file. I already convert them into .pb file (tensorflow), but now i need .pbtxt. How to i generate the .pbtxt file.?
Thank you.
Look for your reply soon~
how we will get .pbtxt file as I have only .pb file can you please help in this…thank you
Hi Jean,
Thank you for sharing!
I am trying to use OpenCV to recognized a new object based on a trained mobilenet-ssd-v2 model, but always get an error. In Tensorflow it recognizes the new objects, but in OpenCV there is an error.
I am unable to generate .pb and .pbtxt files to use in the cv.dnn.readNetFromTensorflow call.
If you get the already made file, it works, but if you need to generate them, seems pretty impossible.
Could you point me some resource to research how to solve this?
Thank you,
[…] Following the article I wrote previously: “How to load Tensorflow models with OpenCV” now it’s time to approach another widely used ML Library. But first I’d like […]
Traceback (most recent call last):
File “c:/Users/RR/Desktop/project/project1.py”, line 7, in
tensorflowNet = cv2.dnn.readNetFromTensorflow(‘frozen_inference_graph.pb’, ‘graph.pbtxt’)
cv2.error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-h4wtvo23\opencv\modules\dnn\src\caffe\caffe_io.cpp:1133: error: (-2:Unspecified error) FAILED: fs.is_open(). Can’t open “frozen_inference_graph.pb” in function ‘cv::dnn::ReadProtoFromBinaryFile’
what could be the reason for this error?
Anyone has come across this issue:
tensorflowNet = cv2.dnn.readNetFromTensorflow(
cv2.error: OpenCV(4.4.0) /private/var/folders/nz/vv4_9tw56nv9k3tkvyszvwg80000gn/T/pip-req-build-wv7rsg8n/opencv/modules/dnn/src/tensorflow/tf_importer.cpp:672: error: (-215:Assertion failed) const_layers.insert(std::make_pair(name, li)).second in function ‘addConstNodes’
???
Please any hints?
Thanks in advance
Hi, thank you so much for the info.
I was able to find the models and config files but not the classification/labels for the models. How do I get these?
These are the models with pbtxt files:
https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf2_detection_zoo.md
[…] https://jeanvitor.com/tensorflow-object-detecion-opencv/ […]