From ef9b42c05a3951e396cdd1adaae59b21f1270e4c Mon Sep 17 00:00:00 2001 From: Abhishek Gola Date: Wed, 11 Jun 2025 15:59:06 +0530 Subject: [PATCH] Merge pull request #27349 from abhishek-gola:deblurring_onnx_sample Added DNN based deblurring samples #27349 Corresponding pull request adding quantized onnx model to opencv_zoo: https://github.com/opencv/opencv_zoo/pull/295 Model size: 88MB ### 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 --- samples/dnn/deblurring.cpp | 136 +++++++++++++++++++++++++++++++++++++ samples/dnn/deblurring.py | 115 +++++++++++++++++++++++++++++++ samples/dnn/models.yml | 14 ++++ 3 files changed, 265 insertions(+) create mode 100644 samples/dnn/deblurring.cpp create mode 100644 samples/dnn/deblurring.py diff --git a/samples/dnn/deblurring.cpp b/samples/dnn/deblurring.cpp new file mode 100644 index 0000000000..4c910be678 --- /dev/null +++ b/samples/dnn/deblurring.cpp @@ -0,0 +1,136 @@ +/* +This file is part of OpenCV project. +It is subject to the license terms in the LICENSE file found in the top-level directory +of this distribution and at http://opencv.org/license.html. + +This sample deblurs the given blurry image. + +Copyright (C) 2025, Bigvision LLC. + +How to use: + Sample command to run: + `./example_dnn_deblurring` + + You can download NAFNet deblurring model using + `python download_models.py NAFNet` + + References: + Github: https://github.com/megvii-research/NAFNet + PyTorch model: https://drive.google.com/file/d/14D4V4raNYIOhETfcuuLI3bGLB-OYIv6X/view + + PyTorch model was converted to ONNX and then ONNX model was further quantized using block quantization from [opencv_zoo](https://github.com/opencv/opencv_zoo/blob/main/tools/quantize/block_quantize.py) + + Set environment variable OPENCV_DOWNLOAD_CACHE_DIR to point to the directory where models are downloaded. Also, point OPENCV_SAMPLES_DATA_PATH to opencv/samples/data. +*/ + +#include +#include + +#include +#include +#include + +#include "common.hpp" + +using namespace cv; +using namespace dnn; +using namespace std; + +const string about = "Use this script for image deblurring using OpenCV. \n\n" + "Firstly, download required models i.e. NAFNet using `download_models.py` (if not already done). Set environment variable OPENCV_DOWNLOAD_CACHE_DIR to point to the directory where models are downloaded. Also, point OPENCV_SAMPLES_DATA_PATH to opencv/samples/data.\n" + "To run:\n" + "\t Example: ./example_dnn_deblurring [--input=] \n\n" + "Deblurring model path can also be specified using --model argument.\n\n"; + +const string param_keys = + "{ help h | | show help message}" + "{ @alias | NAFNet | An alias name of model to extract preprocessing parameters from models.yml file. }" + "{ zoo | ../dnn/models.yml | An optional path to file with preprocessing parameters }" + "{ input i | licenseplate_motion.jpg | image file path}"; + +const string backend_keys = format( + "{ backend | default | Choose one of computation backends: " + "default: automatically (by default), " + "openvino: Intel's Deep Learning Inference Engine (https://software.intel.com/openvino-toolkit), " + "opencv: OpenCV implementation, " + "vkcom: VKCOM, " + "cuda: CUDA, " + "webnn: WebNN }"); + +const string target_keys = format( + "{ target | cpu | Choose one of target computation devices: " + "cpu: CPU target (by default), " + "opencl: OpenCL, " + "opencl_fp16: OpenCL fp16 (half-float precision), " + "vpu: VPU, " + "vulkan: Vulkan, " + "cuda: CUDA, " + "cuda_fp16: CUDA fp16 (half-float preprocess) }"); + +string keys = param_keys + backend_keys + target_keys; + + +int main(int argc, char **argv) +{ + CommandLineParser parser(argc, argv, keys); + + if (!parser.has("@alias") || parser.has("help")) + { + cout<("@alias"); + string zooFile = findFile(parser.get("zoo")); + keys += genPreprocArguments(modelName, zooFile); + parser = CommandLineParser(argc, argv, keys); + parser.about("Use this script to run image deblurring using OpenCV."); + + const string sha1 = parser.get("sha1"); + const string modelPath = findModel(parser.get("model"), sha1); + string imgPath = parser.get("input"); + const string backend = parser.get("backend"); + const string target = parser.get("target"); + float scale = parser.get("scale"); + bool swapRB = parser.get("rgb"); + Scalar mean_v = parser.get("mean"); + + EngineType engine = ENGINE_AUTO; + if (backend != "default" || target != "cpu"){ + engine = ENGINE_CLASSIC; + } + + Net net = readNetFromONNX(modelPath, engine); + net.setPreferableBackend(getBackendID(backend)); + net.setPreferableTarget(getTargetID(target)); + + Mat inputImage = imread(findFile(imgPath)); + if (inputImage.empty()) { + cerr << "Error: Input image could not be loaded." << endl; + return -1; + } + Mat image = inputImage.clone(); + + Mat image_blob = blobFromImage(image, scale, Size(image.cols, image.rows), mean_v, swapRB, false); + + net.setInput(image_blob); + Mat output = net.forward(); + + // Post Processing + Mat output_transposed(3, &output.size[1], CV_32F, output.ptr()); + + vector channels = { + Mat(output_transposed.size[1], output_transposed.size[2], CV_32F, output_transposed.ptr(2)), + Mat(output_transposed.size[1], output_transposed.size[2], CV_32F, output_transposed.ptr(1)), + Mat(output_transposed.size[1], output_transposed.size[2], CV_32F, output_transposed.ptr(0)) + }; + + Mat outputImage; + merge(channels, outputImage); + outputImage.convertTo(outputImage, CV_8UC3, 255.0); + + imshow("Input Image", inputImage); + imshow("Output Image", outputImage); + waitKey(0); + return 0; +} diff --git a/samples/dnn/deblurring.py b/samples/dnn/deblurring.py new file mode 100644 index 0000000000..0908a23385 --- /dev/null +++ b/samples/dnn/deblurring.py @@ -0,0 +1,115 @@ +#!/usr/bin/env python +''' +This file is part of OpenCV project. +It is subject to the license terms in the LICENSE file found in the top-level directory +of this distribution and at http://opencv.org/license.html. + +This sample deblurs the given blurry image. + +Copyright (C) 2025, Bigvision LLC. + +How to use: + Sample command to run: + `python deblurring.py` + + You can download NAFNet deblurring model using + `python download_models.py NAFNet` + + References: + Github: https://github.com/megvii-research/NAFNet + PyTorch model: https://drive.google.com/file/d/14D4V4raNYIOhETfcuuLI3bGLB-OYIv6X/view + + PyTorch model was converted to ONNX and then ONNX model was further quantized using block quantization from [opencv_zoo](https://github.com/opencv/opencv_zoo/blob/main/tools/quantize/block_quantize.py) + + Set environment variable OPENCV_DOWNLOAD_CACHE_DIR to point to the directory where models are downloaded. Also, point OPENCV_SAMPLES_DATA_PATH to opencv/samples/data. +''' + +import argparse +import cv2 as cv +import numpy as np +from common import * + +def help(): + print( + ''' + Use this script for image deblurring using OpenCV. + + Firstly, download required models i.e. NAFNet using `download_models.py` (if not already done). Set environment variable OPENCV_DOWNLOAD_CACHE_DIR to specify where models should be downloaded. Also, point OPENCV_SAMPLES_DATA_PATH to opencv/samples/data. + + To run: + Example: python deblurring.py [--input=] + + Deblurring model path can also be specified using --model argument. + ''' + ) + +def get_args_parser(): + backends = ("default", "openvino", "opencv", "vkcom", "cuda") + targets = ("cpu", "opencl", "opencl_fp16", "ncs2_vpu", "hddl_vpu", "vulkan", "cuda", "cuda_fp16") + + parser = argparse.ArgumentParser(add_help=False) + parser.add_argument('--zoo', default=os.path.join(os.path.dirname(os.path.abspath(__file__)), 'models.yml'), + help='An optional path to file with preprocessing parameters.') + parser.add_argument('--input', '-i', default="licenseplate_motion.jpg", help='Path to image file.', required=False) + parser.add_argument('--backend', default="default", type=str, choices=backends, + help="Choose one of computation backends: " + "default: automatically (by default), " + "openvino: Intel's Deep Learning Inference Engine (https://software.intel.com/openvino-toolkit), " + "opencv: OpenCV implementation, " + "vkcom: VKCOM, " + "cuda: CUDA, " + "webnn: WebNN") + parser.add_argument('--target', default="cpu", type=str, choices=targets, + help="Choose one of target computation devices: " + "cpu: CPU target (by default), " + "opencl: OpenCL, " + "opencl_fp16: OpenCL fp16 (half-float precision), " + "ncs2_vpu: NCS2 VPU, " + "hddl_vpu: HDDL VPU, " + "vulkan: Vulkan, " + "cuda: CUDA, " + "cuda_fp16: CUDA fp16 (half-float preprocess)") + args, _ = parser.parse_known_args() + add_preproc_args(args.zoo, parser, 'deblurring', prefix="", alias="NAFNet") + parser = argparse.ArgumentParser(parents=[parser], + description='Image deblurring using OpenCV.', + formatter_class=argparse.ArgumentDefaultsHelpFormatter) + return parser.parse_args() + +def main(): + if hasattr(args, 'help'): + help() + exit(1) + + args.model = findModel(args.model, args.sha1) + + 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)) + + input_image = cv.imread(findFile(args.input)) + image = input_image.copy() + height, width = image.shape[:2] + + image_blob = cv.dnn.blobFromImage(image, args.scale, (width, height), args.mean, args.rgb, False) + net.setInput(image_blob) + out = net.forward() + + # Postprocessing + output = out[0] + output = np.transpose(output, (1, 2, 0)) + output = np.clip(output * 255.0, 0, 255).astype(np.uint8) + out_image = cv.cvtColor(output, cv.COLOR_RGB2BGR) + + cv.imshow("input image: ", input_image) + cv.imshow("output image: ", out_image) + cv.waitKey(0) + +if __name__ == '__main__': + args = get_args_parser() + main() diff --git a/samples/dnn/models.yml b/samples/dnn/models.yml index 5c5861bc8f..b2d7f59e75 100644 --- a/samples/dnn/models.yml +++ b/samples/dnn/models.yml @@ -492,3 +492,17 @@ mcc: sha1: "8350cb8f078ecefa1cd566e89930ede25a192310" config: "graph.pbtxt" sample: "mcc" + +################################################################################ +# Deblurring model. +################################################################################ + +NAFNet: + load_info: + url: "https://drive.google.com/uc?export=dowload&id=1ZLRhkpCekNruJZggVpBgSoCx3k7bJ-5v" + sha1: "7dabf3d4ede0770ef326afc4511f7e67a791286d" + model: "deblurring_nafnet_2025may.onnx" + mean: [0, 0, 0] + scale: 0.00392 + rgb: true + sample: "deblurring"