0%

how to use opencv dnn module

Guide

support framework

cv2.dnn.readNetFromCaffe: deploy.prototxt + iter_140000.caffemodel
cv2.dnn.readNetFromTensorflow: model.pbtxt + model.pb.
cv2.dnn.readNetFromDarknet: yolov3.cfg + yolov3.weights
cv2.dnn.readNetFromTorch: model.t7

readNetFromCaffe

1
2
3
4
5
6
# load our serialized face detector from disk
print("[INFO] loading face detector...")
protoPath = os.path.sep.join([args["detector"], "deploy.prototxt"])
modelPath = os.path.sep.join([args["detector"],
"res10_300x300_ssd_iter_140000.caffemodel"])
net = cv2.dnn.readNetFromCaffe(protoPath, modelPath)

readNetFromTensorflow

1
2
3
4
5
6
7
8
9
10
# derive the paths to the Mask R-CNN weights and model configuration
weightsPath = os.path.sep.join([args["mask_rcnn"],
"frozen_inference_graph.pb"])
configPath = os.path.sep.join([args["mask_rcnn"],
"mask_rcnn_inception_v2_coco_2018_01_28.pbtxt"])

# load our Mask R-CNN trained on the COCO dataset (90 classes)
# from disk
print("[INFO] loading Mask R-CNN from disk...")
net = cv2.dnn.readNetFromTensorflow(weightsPath, configPath)

readNetFromDarknet

1
2
3
4
5
6
7
# derive the paths to the YOLO weights and model configuration
weightsPath = os.path.sep.join([args["yolo"], "yolov3.weights"])
configPath = os.path.sep.join([args["yolo"], "yolov3.cfg"])

# load our YOLO object detector trained on COCO dataset (80 classes)
print("[INFO] loading YOLO from disk...")
net = cv2.dnn.readNetFromDarknet(configPath, weightsPath)

readNetFromTorch

1
2
3
4
5
6
7
# load our serialized face embedding model from disk
print("[INFO] loading face recognizer...")
net = cv2.dnn.readNetFromTorch(args["embedding_model"])

# load the neural style transfer model from disk
print("[INFO] loading style transfer model...")
net = cv2.dnn.readNetFromTorch(args["model"])

Reference

History

  • 20181207: created.