mirror of https://github.com/opencv/opencv.git
parent
87b1835cb6
commit
ad5898224d
12 changed files with 435 additions and 104 deletions
@ -0,0 +1,94 @@ |
||||
#include <opencv2/core/utils/filesystem.hpp> |
||||
|
||||
using namespace cv; |
||||
|
||||
std::string genArgument(const std::string& argName, const std::string& help, |
||||
const std::string& modelName, const std::string& zooFile, |
||||
char key = ' ', std::string defaultVal = ""); |
||||
|
||||
std::string genPreprocArguments(const std::string& modelName, const std::string& zooFile); |
||||
|
||||
std::string findFile(const std::string& filename); |
||||
|
||||
std::string genArgument(const std::string& argName, const std::string& help, |
||||
const std::string& modelName, const std::string& zooFile, |
||||
char key, std::string defaultVal) |
||||
{ |
||||
if (!modelName.empty()) |
||||
{ |
||||
FileStorage fs(zooFile, FileStorage::READ); |
||||
if (fs.isOpened()) |
||||
{ |
||||
FileNode node = fs[modelName]; |
||||
if (!node.empty()) |
||||
{ |
||||
FileNode value = node[argName]; |
||||
if (!value.empty()) |
||||
{ |
||||
if (value.isReal()) |
||||
defaultVal = format("%f", (float)value); |
||||
else if (value.isString()) |
||||
defaultVal = (std::string)value; |
||||
else if (value.isInt()) |
||||
defaultVal = format("%d", (int)value); |
||||
else if (value.isSeq()) |
||||
{ |
||||
for (size_t i = 0; i < value.size(); ++i) |
||||
{ |
||||
FileNode v = value[(int)i]; |
||||
if (v.isInt()) |
||||
defaultVal += format("%d ", (int)v); |
||||
else if (v.isReal()) |
||||
defaultVal += format("%f ", (float)v); |
||||
else |
||||
CV_Error(Error::StsNotImplemented, "Unexpected value format"); |
||||
} |
||||
} |
||||
else |
||||
CV_Error(Error::StsNotImplemented, "Unexpected field format"); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
return "{ " + argName + " " + key + " | " + defaultVal + " | " + help + " }"; |
||||
} |
||||
|
||||
std::string findFile(const std::string& filename) |
||||
{ |
||||
if (filename.empty() || utils::fs::exists(filename)) |
||||
return filename; |
||||
|
||||
std::string extraPaths[] = {getenv("OPENCV_DNN_TEST_DATA_PATH"), |
||||
getenv("OPENCV_TEST_DATA_PATH")}; |
||||
for (int i = 0; i < 2; ++i) |
||||
{ |
||||
std::string absPath = utils::fs::join(extraPaths[i], utils::fs::join("dnn", filename)); |
||||
if (utils::fs::exists(absPath)) |
||||
return absPath; |
||||
} |
||||
CV_Error(Error::StsObjectNotFound, "File " + filename + " not found! " |
||||
"Please specify a path to /opencv_extra/testdata in OPENCV_DNN_TEST_DATA_PATH " |
||||
"environment variable or pass a full path to model."); |
||||
return ""; |
||||
} |
||||
|
||||
std::string genPreprocArguments(const std::string& modelName, const std::string& zooFile) |
||||
{ |
||||
return genArgument("model", "Path to a binary file of model contains trained weights. " |
||||
"It could be a file with extensions .caffemodel (Caffe), " |
||||
".pb (TensorFlow), .t7 or .net (Torch), .weights (Darknet), .bin (OpenVINO).", |
||||
modelName, zooFile, 'm') + |
||||
genArgument("config", "Path to a text file of model contains network configuration. " |
||||
"It could be a file with extensions .prototxt (Caffe), .pbtxt (TensorFlow), .cfg (Darknet), .xml (OpenVINO).", |
||||
modelName, zooFile, 'c') + |
||||
genArgument("mean", "Preprocess input image by subtracting mean values. Mean values should be in BGR order and delimited by spaces.", |
||||
modelName, zooFile) + |
||||
genArgument("scale", "Preprocess input image by multiplying on a scale factor.", |
||||
modelName, zooFile, ' ', "1.0") + |
||||
genArgument("width", "Preprocess input image by resizing to a specific width.", |
||||
modelName, zooFile, ' ', "-1") + |
||||
genArgument("height", "Preprocess input image by resizing to a specific height.", |
||||
modelName, zooFile, ' ', "-1") + |
||||
genArgument("rgb", "Indicate that model works with RGB input images instead BGR ones.", |
||||
modelName, zooFile); |
||||
} |
@ -0,0 +1,108 @@ |
||||
import sys |
||||
import os |
||||
import cv2 as cv |
||||
|
||||
|
||||
def add_argument(zoo, parser, name, help, required=False, default=None, type=None, action=None, nargs=None): |
||||
if len(sys.argv) <= 1: |
||||
return |
||||
|
||||
modelName = sys.argv[1] |
||||
|
||||
if os.path.isfile(zoo): |
||||
fs = cv.FileStorage(zoo, cv.FILE_STORAGE_READ) |
||||
node = fs.getNode(modelName) |
||||
if not node.empty(): |
||||
value = node.getNode(name) |
||||
if not value.empty(): |
||||
if value.isReal(): |
||||
default = value.real() |
||||
elif value.isString(): |
||||
default = value.string() |
||||
elif value.isInt(): |
||||
default = int(value.real()) |
||||
elif value.isSeq(): |
||||
default = [] |
||||
for i in range(value.size()): |
||||
v = value.at(i) |
||||
if v.isInt(): |
||||
default.append(int(v.real())) |
||||
elif v.isReal(): |
||||
default.append(v.real()) |
||||
else: |
||||
print('Unexpected value format') |
||||
exit(0) |
||||
else: |
||||
print('Unexpected field format') |
||||
exit(0) |
||||
required = False |
||||
|
||||
if action == 'store_true': |
||||
default = 1 if default == 'true' else (0 if default == 'false' else default) |
||||
assert(default is None or default == 0 or default == 1) |
||||
parser.add_argument('--' + name, required=required, help=help, default=bool(default), |
||||
action=action) |
||||
else: |
||||
parser.add_argument('--' + name, required=required, help=help, default=default, |
||||
action=action, nargs=nargs, type=type) |
||||
|
||||
|
||||
def add_preproc_args(zoo, parser, sample): |
||||
aliases = [] |
||||
if os.path.isfile(zoo): |
||||
fs = cv.FileStorage(zoo, cv.FILE_STORAGE_READ) |
||||
root = fs.root() |
||||
for name in root.keys(): |
||||
model = root.getNode(name) |
||||
if model.getNode('sample').string() == sample: |
||||
aliases.append(name) |
||||
|
||||
parser.add_argument('alias', nargs='?', choices=aliases, |
||||
help='An alias name of model to extract preprocessing parameters from models.yml file.') |
||||
add_argument(zoo, parser, 'model', required=True, |
||||
help='Path to a binary file of model contains trained weights. ' |
||||
'It could be a file with extensions .caffemodel (Caffe), ' |
||||
'.pb (TensorFlow), .t7 or .net (Torch), .weights (Darknet), .bin (OpenVINO)') |
||||
add_argument(zoo, parser, 'config', |
||||
help='Path to a text file of model contains network configuration. ' |
||||
'It could be a file with extensions .prototxt (Caffe), .pbtxt or .config (TensorFlow), .cfg (Darknet), .xml (OpenVINO)') |
||||
add_argument(zoo, parser, 'mean', nargs='+', type=float, default=[0, 0, 0], |
||||
help='Preprocess input image by subtracting mean values. ' |
||||
'Mean values should be in BGR order.') |
||||
add_argument(zoo, parser, 'scale', type=float, default=1.0, |
||||
help='Preprocess input image by multiplying on a scale factor.') |
||||
add_argument(zoo, parser, 'width', type=int, |
||||
help='Preprocess input image by resizing to a specific width.') |
||||
add_argument(zoo, parser, 'height', type=int, |
||||
help='Preprocess input image by resizing to a specific height.') |
||||
add_argument(zoo, parser, 'rgb', action='store_true', |
||||
help='Indicate that model works with RGB input images instead BGR ones.') |
||||
add_argument(zoo, parser, 'classes', |
||||
help='Optional path to a text file with names of classes to label detected objects.') |
||||
|
||||
|
||||
def findFile(filename): |
||||
if filename: |
||||
if os.path.exists(filename): |
||||
return filename |
||||
|
||||
samplesDataDir = os.path.join(os.path.dirname(os.path.abspath(__file__)), |
||||
'..', |
||||
'data', |
||||
'dnn') |
||||
if os.path.exists(os.path.join(samplesDataDir, filename)): |
||||
return os.path.join(samplesDataDir, filename) |
||||
|
||||
for path in ['OPENCV_DNN_TEST_DATA_PATH', 'OPENCV_TEST_DATA_PATH']: |
||||
try: |
||||
extraPath = os.environ[path] |
||||
absPath = os.path.join(extraPath, 'dnn', filename) |
||||
if os.path.exists(absPath): |
||||
return absPath |
||||
except KeyError: |
||||
pass |
||||
|
||||
print('File ' + filename + ' not found! Please specify a path to ' |
||||
'/opencv_extra/testdata in OPENCV_DNN_TEST_DATA_PATH environment ' |
||||
'variable or pass a full path to model.') |
||||
exit(0) |
@ -0,0 +1,117 @@ |
||||
%YAML:1.0 |
||||
|
||||
################################################################################ |
||||
# Object detection models. |
||||
################################################################################ |
||||
|
||||
# OpenCV's face detection network |
||||
opencv_fd: |
||||
model: "opencv_face_detector.caffemodel" |
||||
config: "opencv_face_detector.prototxt" |
||||
mean: [104, 177, 123] |
||||
scale: 1.0 |
||||
width: 300 |
||||
height: 300 |
||||
rgb: false |
||||
sample: "object_detection" |
||||
|
||||
# YOLO object detection family from Darknet (https://pjreddie.com/darknet/yolo/) |
||||
# Might be used for all YOLOv2, TinyYolov2 and YOLOv3 |
||||
yolo: |
||||
model: "yolov3.weights" |
||||
config: "yolov3.cfg" |
||||
mean: [0, 0, 0] |
||||
scale: 0.00392 |
||||
width: 416 |
||||
height: 416 |
||||
rgb: true |
||||
classes: "object_detection_classes_yolov3.txt" |
||||
sample: "object_detection" |
||||
|
||||
tiny-yolo-voc: |
||||
model: "tiny-yolo-voc.weights" |
||||
config: "tiny-yolo-voc.cfg" |
||||
mean: [0, 0, 0] |
||||
scale: 0.00392 |
||||
width: 416 |
||||
height: 416 |
||||
rgb: true |
||||
classes: "object_detection_classes_pascal_voc.txt" |
||||
sample: "object_detection" |
||||
|
||||
# Caffe implementation of SSD model from https://github.com/chuanqi305/MobileNet-SSD |
||||
ssd_caffe: |
||||
model: "MobileNetSSD_deploy.caffemodel" |
||||
config: "MobileNetSSD_deploy.prototxt" |
||||
mean: [127.5, 127.5, 127.5] |
||||
scale: 0.007843 |
||||
width: 300 |
||||
height: 300 |
||||
rgb: false |
||||
classes: "object_detection_classes_pascal_voc.txt" |
||||
sample: "object_detection" |
||||
|
||||
# TensorFlow implementation of SSD model from https://github.com/tensorflow/models/tree/master/research/object_detection |
||||
ssd_tf: |
||||
model: "ssd_mobilenet_v1_coco_2017_11_17.pb" |
||||
config: "ssd_mobilenet_v1_coco_2017_11_17.pbtxt" |
||||
mean: [0, 0, 0] |
||||
scale: 1.0 |
||||
width: 300 |
||||
height: 300 |
||||
rgb: true |
||||
classes: "object_detection_classes_coco.txt" |
||||
sample: "object_detection" |
||||
|
||||
# TensorFlow implementation of Faster-RCNN model from https://github.com/tensorflow/models/tree/master/research/object_detection |
||||
faster_rcnn_tf: |
||||
model: "faster_rcnn_inception_v2_coco_2018_01_28.pb" |
||||
config: "faster_rcnn_inception_v2_coco_2018_01_28.pbtxt" |
||||
mean: [0, 0, 0] |
||||
scale: 1.0 |
||||
width: 800 |
||||
height: 600 |
||||
rgb: true |
||||
sample: "object_detection" |
||||
|
||||
################################################################################ |
||||
# Image classification models. |
||||
################################################################################ |
||||
|
||||
# SqueezeNet v1.1 from https://github.com/DeepScale/SqueezeNet |
||||
squeezenet: |
||||
model: "squeezenet_v1.1.caffemodel" |
||||
config: "squeezenet_v1.1.prototxt" |
||||
mean: [0, 0, 0] |
||||
scale: 1.0 |
||||
width: 227 |
||||
height: 227 |
||||
rgb: false |
||||
classes: "classification_classes_ILSVRC2012.txt" |
||||
sample: "classification" |
||||
|
||||
################################################################################ |
||||
# Semantic segmentation models. |
||||
################################################################################ |
||||
|
||||
# ENet road scene segmentation network from https://github.com/e-lab/ENet-training |
||||
# Works fine for different input sizes. |
||||
enet: |
||||
model: "Enet-model-best.net" |
||||
mean: [0, 0, 0] |
||||
scale: 0.00392 |
||||
width: 512 |
||||
height: 256 |
||||
rgb: true |
||||
classes: "enet-classes.txt" |
||||
sample: "segmentation" |
||||
|
||||
fcn8s: |
||||
model: "fcn8s-heavy-pascal.caffemodel" |
||||
config: "fcn8s-heavy-pascal.prototxt" |
||||
mean: [0, 0, 0] |
||||
scale: 1.0 |
||||
width: 500 |
||||
height: 500 |
||||
rgb: false |
||||
sample: "segmentation" |
Loading…
Reference in new issue