1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-29 23:33:05 +04:00

Merge pull request #26334 from gursimarsingh:dnn_engine_change

Modify DNN Samples to use ENGINE_CLASSIC for Non-Default Back-end or Target #26334

PR resolves #26325 regarding fall-back to ENGINE_CLASSIC if non-default back-end or target is passed by user.
### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
Gursimar Singh
2024-11-08 11:28:35 +05:30
committed by GitHub
parent 1ae304bb83
commit 979428d590
9 changed files with 39 additions and 19 deletions
+2 -2
View File
@@ -190,7 +190,7 @@ void Net::Impl::setPreferableBackend(Net& net, int backendId)
{
if (mainGraph)
{
CV_LOG_WARNING(NULL, "Back-ends are not supported by the new graph egine for now");
CV_LOG_WARNING(NULL, "Back-ends are not supported by the new graph engine for now");
preferableBackend = backendId;
return;
}
@@ -226,7 +226,7 @@ void Net::Impl::setPreferableTarget(int targetId)
{
if (mainGraph)
{
CV_LOG_WARNING(NULL, "Targtes are not supported by the new graph egine for now");
CV_LOG_WARNING(NULL, "Targets are not supported by the new graph engine for now");
return;
}
if (netWasQuantized && targetId != DNN_TARGET_CPU &&
+5 -1
View File
@@ -143,7 +143,11 @@ int main(int argc, char** argv)
}
CV_Assert(!model.empty());
//! [Read and initialize network]
Net net = readNetFromONNX(model);
EngineType engine = ENGINE_AUTO;
if (backend != "default" || target != "cpu"){
engine = ENGINE_CLASSIC;
}
Net net = readNetFromONNX(model, engine);
net.setPreferableBackend(getBackendID(backend));
net.setPreferableTarget(getTargetID(target));
//! [Read and initialize network]
+4 -3
View File
@@ -82,9 +82,10 @@ def main(func_args=None):
labels = f.read().rstrip('\n').split('\n')
# Load a network
net = cv.dnn.readNet(args.model)
engine = cv.dnn.ENGINE_AUTO
if args.backend != "default" or args.target != "cpu":
engine = cv.dnn.ENGINE_CLASSIC
net = cv.dnn.readNetFromONNX(args.model, engine)
net.setPreferableBackend(get_backend_id(args.backend))
net.setPreferableTarget(get_target_id(args.target))
+5 -1
View File
@@ -83,7 +83,11 @@ int main(int argc, char** argv) {
resize(imgL, imgLResized, Size(256, 256), 0, 0, INTER_CUBIC);
// Prepare the model
dnn::Net net = dnn::readNetFromONNX(onnxModelPath);
EngineType engine = ENGINE_AUTO;
if (backendId != 0 || targetId != 0){
engine = ENGINE_CLASSIC;
}
dnn::Net net = dnn::readNetFromONNX(onnxModelPath, engine);
net.setPreferableBackend(backendId);
net.setPreferableTarget(targetId);
//! [Read and initialize network]
+4 -1
View File
@@ -44,7 +44,10 @@ if __name__ == '__main__':
img_gray_rs *= (100.0 / 255.0) # Scale L channel to 0-100 range
onnx_model_path = args.onnx_model_path # Update this path to your ONNX model's path
session = cv.dnn.readNetFromONNX(onnx_model_path)
engine = cv.dnn.ENGINE_AUTO
if args.backend != 0 or args.target != 0:
engine = cv.dnn.ENGINE_CLASSIC
session = cv.dnn.readNetFromONNX(onnx_model_path, engine)
session.setPreferableBackend(args.backend)
session.setPreferableTarget(args.target)
+8 -4
View File
@@ -29,8 +29,8 @@ static void applyCanny(const Mat& image, Mat& result) {
}
// Load Model
static void loadModel(const string modelPath, String backend, String target, Net &net){
net = readNetFromONNX(modelPath);
static void loadModel(const string modelPath, String backend, String target, Net &net, EngineType engine){
net = readNetFromONNX(modelPath, engine);
net.setPreferableBackend(getBackendID(backend));
net.setPreferableTarget(getTargetID(target));
}
@@ -159,6 +159,10 @@ int main(int argc, char** argv) {
string method = parser.get<String>("method");
String sha1 = parser.get<String>("sha1");
string model = findModel(parser.get<String>("model"), sha1);
EngineType engine = ENGINE_AUTO;
if (backend != "default" || target != "cpu"){
engine = ENGINE_CLASSIC;
}
parser.about(about);
VideoCapture cap;
@@ -179,7 +183,7 @@ int main(int argc, char** argv) {
}
if (method == "dexined") {
loadModel(model, backend, target, net);
loadModel(model, backend, target, net, engine);
}
else{
Mat dummy = Mat::zeros(512, 512, CV_8UC3);
@@ -218,7 +222,7 @@ int main(int argc, char** argv) {
if (!model.empty()){
method = "dexined";
if (net.empty())
loadModel(model, backend, target, net);
loadModel(model, backend, target, net, engine);
destroyWindow("Output");
namedWindow("Input", WINDOW_AUTOSIZE);
namedWindow("Output", WINDOW_AUTOSIZE);
+7 -4
View File
@@ -92,8 +92,8 @@ def setupCannyWindow(image):
cv.createTrackbar('thrs2', 'Output', threshold2, 255, lambda value: [globals().__setitem__('threshold2', value), apply_canny(gray)])
cv.createTrackbar('blur', 'Output', blur_amount, 20, lambda value: [globals().__setitem__('blur_amount', value), apply_canny(gray)])
def loadModel(args):
net = cv.dnn.readNetFromONNX(args.model)
def loadModel(args, engine):
net = cv.dnn.readNetFromONNX(args.model, engine)
net.setPreferableBackend(get_backend_id(args.backend))
net.setPreferableTarget(get_target_id(args.target))
return net
@@ -109,6 +109,9 @@ def apply_dexined(model, image):
def main(func_args=None):
args = get_args_parser(func_args)
engine = cv.dnn.ENGINE_AUTO
if args.backend != "default" or args.target != "cpu":
engine = cv.dnn.ENGINE_CLASSIC
cap = cv.VideoCapture(cv.samples.findFile(args.input) if args.input else 0)
if not cap.isOpened():
@@ -136,7 +139,7 @@ def main(func_args=None):
setupCannyWindow(dummy)
net = None
if method == "dexined":
net = loadModel(args)
net = loadModel(args, engine)
while cv.waitKey(1) < 0:
hasFrame, image = cap.read()
if not hasFrame:
@@ -160,7 +163,7 @@ def main(func_args=None):
print("model: ", args.model)
method = "dexined"
if net is None:
net = loadModel(args)
net = loadModel(args, engine)
cv.destroyWindow('Output')
cv.namedWindow('Output', cv.WINDOW_AUTOSIZE)
cv.moveWindow('Output', 200, 50)
+2 -2
View File
@@ -360,9 +360,9 @@ OCR:
dexined:
load_info:
url: "https://github.com/gursimarsingh/opencv_zoo/raw/dexined_model/models/edge_detection_dexined/dexined.onnx"
url: "https://github.com/opencv/opencv_zoo/raw/refs/heads/main/models/edge_detection_dexined/edge_detection_dexined_2024sep.onnx?download="
sha1: "f86f2d32c3cf892771f76b5e6b629b16a66510e9"
model: "dexined.onnx"
model: "edge_detection_dexined_2024sep.onnx"
mean: [103.5, 116.2, 123.6]
scale: 1.0
width: 512
+2 -1
View File
@@ -184,7 +184,8 @@ def main():
args.yolo_model = findModel(args.yolo_model, args.yolo_sha1)
engine = cv.dnn.ENGINE_AUTO
if args.backend != "default" or args.backend != "cpu":
if args.backend != "default" or args.target != "cpu":
engine = cv.dnn.ENGINE_CLASSIC
yolo_net = cv.dnn.readNetFromONNX(args.yolo_model, engine)
reid_net = cv.dnn.readNetFromONNX(args.model, engine)