forked from opencv/opencv_contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request opencv#3608 from MengqingCao:dvpp_support
Add additional image processing operators for Ascend NPU by utilizing DVPP opencv#3608 The user base for [Ascend NPU](https://www.hiascend.com/en/) and programming with CANN is increasing rapidly, with a growing number of users joining each day. To facilitate the use of these users, this PR provides more support for Ascend backend operators. All operators this PR offers are using use DVPP as the computational unit. Digital Vision Pre-Processing (DVPP) is an image processing unit built into the Ascend AI processor. Its main functions include image and video encoding/decoding, as well as image cropping and scaling. The high-frequency operators with NPU as the backend and basic data structure AscendMat has been provided in opencv#3552, while it still lacks many image processing operators. Moreover, only two interpolation algorithms for the resize operator are supported in opencv#3552. In this PR, the bilinear interpolation algorithm and nearest neighbour interpolation algorithm are implemented for the resize operator, as well as the Ascend implementation of the copyMakeBorder operator. In addition, the serialization of image processing operations is widely used in the preprocessing and post-processing stages of computer vision deep learning methods. Therefore, providing integrated operators is very meaningful for improving the convenience of use for OpenCV and deep learning crossover users. For example, torchvision also provides similar operators: [RESIZED_CROP](https://pytorch.org/vision/stable/generated/torchvision.transforms.functional.resized_crop.html?highlight=resizedcrop). Thus, this PR also provides two serialization processing operators: cropResize and cropResizeMakeBorder. ### 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 - [N/A] 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
- Loading branch information
1 parent
261cfc7
commit 0e3c39f
Showing
12 changed files
with
1,126 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// 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. | ||
|
||
#ifndef ENABLE_DVPP_INTERFACE | ||
#define ENABLE_DVPP_INTERFACE | ||
#endif // ENABLE_DVPP_INTERFACE | ||
|
||
#include <vector> | ||
#include <string> | ||
#include <acl/acl.h> | ||
#include <acl/acl_op_compiler.h> | ||
#include <acl/dvpp/hi_dvpp.h> | ||
#include "acl/acl_op.h" | ||
#include "cann_call.hpp" | ||
|
||
namespace cv | ||
{ | ||
namespace cann | ||
{ | ||
struct AscendPicDesc | ||
{ | ||
const char* name; | ||
std::shared_ptr<hi_void> data; | ||
std::vector<int64_t> batchNum; | ||
|
||
size_t widthAlignment = 16; | ||
size_t heightAlignment = 1; | ||
size_t sizeAlignment = 3; | ||
size_t sizeNum = 3; | ||
|
||
hi_vpc_pic_info Pic; | ||
AscendPicDesc& setMemAlign(); | ||
AscendPicDesc& setPic(hi_pixel_format _picture_format); | ||
std::shared_ptr<hi_void> allocate(); | ||
AscendPicDesc(){}; | ||
AscendPicDesc(const AscendMat& ascendMat, hi_pixel_format _picture_format); | ||
AscendPicDesc(const Mat& mat, hi_pixel_format _picture_format); | ||
}; | ||
|
||
/* | ||
***************************** hi_mpi_vpc warppers *************************** | ||
The DVPP VPC interfaces here are all version v2. Only the following devices are supported: Atlas | ||
Inference Series products, Atlas 200/500 A2 Inference products and Atlas A2 Training Series | ||
products/Atlas 300I A2 Inference products. | ||
*/ | ||
inline void vpcResizeWarpper(hi_vpc_chn chnId, hi_vpc_pic_info& inPic, hi_vpc_pic_info& outPic, | ||
int interpolation, uint32_t* taskID) | ||
{ | ||
uint32_t ret = hi_mpi_vpc_resize(chnId, &inPic, &outPic, 0, 0, interpolation, taskID, -1); | ||
if (ret != HI_SUCCESS) | ||
CV_Error(Error::StsBadFlag, "failed to resize image"); | ||
} | ||
void vpcCropResizeWarpper(hi_vpc_chn chnId, hi_vpc_pic_info& inPic, hi_vpc_pic_info& outPic, | ||
int cnt, uint32_t* taskID, const Rect& rect, Size dsize, | ||
int interpolation); | ||
|
||
void vpcCropResizeMakeBorderWarpper(hi_vpc_chn chnId, std::vector<AscendPicDesc>& inPicDesc, | ||
std::vector<AscendPicDesc>& outPicDesc, int cnt, | ||
uint32_t* taskID, const Rect& rect, Size dsize, | ||
int interpolation, const int borderType, Scalar scalarV, | ||
int top, int left); | ||
void vpcCopyMakeBorderWarpper(hi_vpc_chn chnId, hi_vpc_pic_info& inPic, hi_vpc_pic_info& outPic, | ||
uint32_t* taskID, int* offsets, int bordertype, Scalar value); | ||
/*****************************************************************************/ | ||
|
||
/** | ||
* @brief Interface for calling DVPP operator descriptors. | ||
* The DVPP VPC interfaces here are all version v2. Supported devices: Atlas Inference Series | ||
* products, Atlas 200/500 A2 Inference products and Atlas A2 Training Series products/Atlas 300I A2 | ||
* Inference products. | ||
*/ | ||
class DvppOperatorDesc | ||
{ | ||
private: | ||
DvppOperatorDesc& addInput(AscendPicDesc& picDesc); | ||
DvppOperatorDesc& addOutput(AscendPicDesc& picDesc); | ||
std::set<std::shared_ptr<hi_void>> holder; | ||
|
||
public: | ||
DvppOperatorDesc() | ||
{ | ||
chnId = 0; | ||
stChnAttr = {}; | ||
createChannel(); | ||
} | ||
virtual ~DvppOperatorDesc() { reset(); } | ||
DvppOperatorDesc& addInput(const AscendMat& mat); | ||
DvppOperatorDesc& addOutput(AscendMat& mat); | ||
DvppOperatorDesc& addInput(const Mat& mat); | ||
DvppOperatorDesc& addOutput(Mat& mat); | ||
|
||
DvppOperatorDesc& getResult(Mat& dst, uint32_t& taskIDResult); | ||
DvppOperatorDesc& getResult(AscendMat& dst, uint32_t& taskIDResult); | ||
|
||
DvppOperatorDesc& reset(); | ||
DvppOperatorDesc& createChannel(); | ||
|
||
std::vector<AscendPicDesc> inputDesc_; | ||
std::vector<AscendPicDesc> outputDesc_; | ||
|
||
hi_vpc_chn chnId; | ||
hi_vpc_chn_attr stChnAttr; | ||
}; | ||
|
||
} // namespace cann | ||
} // namespace cv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.