From c692ff9ec0ef9b1ba9f9c71da5eed0a4ef1b41e6 Mon Sep 17 00:00:00 2001 From: fiss <2657262686@qq.com> Date: Wed, 14 Jun 2023 11:52:44 +0800 Subject: [PATCH] 修正抠图存在绿屏、花屏问题 --- src/util/vpc_util.cpp | 120 +++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------------------------------------------- 1 file changed, 55 insertions(+), 65 deletions(-) diff --git a/src/util/vpc_util.cpp b/src/util/vpc_util.cpp index 3f82d34..f331230 100644 --- a/src/util/vpc_util.cpp +++ b/src/util/vpc_util.cpp @@ -18,7 +18,6 @@ #include "../decoder/interface/DeviceMemory.hpp" #include "../common/logger.hpp" - void VPCUtil::release() { aclError ret; @@ -44,9 +43,27 @@ void VPCUtil::release() LOG_INFO("end to reset device is %d", deviceId_); } - vpc_img_info VPCUtil::crop(DeviceMemory *devMem, video_object_info obj) { + vpc_img_info img_info ; + + uint32_t cropSizeWidth = (obj.right - obj.left) / 16 * 16; + uint32_t cropSizeHeight = (obj.bottom - obj.top) / 2 * 2; + + uint32_t oddNum = 1; + uint32_t cropLeftOffset = (obj.left + 1) / 2 * 2; // must even + uint32_t cropRightOffset = cropLeftOffset + cropSizeWidth - oddNum; // must odd + uint32_t cropTopOffset = (obj.top + 1) / 2 * 2; // must even + uint32_t cropBottomOffset = cropTopOffset + cropSizeHeight - oddNum; // must odd + + if(cropRightOffset <= cropLeftOffset || cropBottomOffset <= cropTopOffset){ + return img_info; + } + + // LOG_INFO("crop src {} ({}, {}, {}, {})", obj.object_id, obj.left, obj.right, obj.top, obj.bottom); + // LOG_INFO("crop {} ({}, {}, {}, {})", obj.object_id, cropLeftOffset, cropRightOffset, cropTopOffset, cropBottomOffset); + acldvppRoiConfig *cropArea_ = acldvppCreateRoiConfig(cropLeftOffset, cropRightOffset, cropTopOffset, cropBottomOffset); + acldvppPicDesc *vpcInputDesc_ = acldvppCreatePicDesc(); acldvppSetPicDescData(vpcInputDesc_, devMem->getMem()); acldvppSetPicDescFormat(vpcInputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420); @@ -56,35 +73,17 @@ vpc_img_info VPCUtil::crop(DeviceMemory *devMem, video_object_info obj) { acldvppSetPicDescHeightStride(vpcInputDesc_, devMem->getHeightStride()); acldvppSetPicDescSize(vpcInputDesc_, devMem->getSize()); - const int orimodelInputWidth = obj.right - obj.left; // cur model shape is 224 * 224 - const int orimodelInputHeight = obj.bottom - obj.top; - const int modelInputLeft = obj.left; // cur model shape is 224 * 224 - const int modelInputTop = obj.top; - - // GetPicDevBuffer4JpegD - int modelInputWidth = (orimodelInputWidth + 15) / 16 * 16; - int modelInputHeight = (orimodelInputHeight + 1) / 2 * 2; - - uint32_t oddNum = 1; - uint32_t cropSizeWidth = modelInputWidth; - uint32_t cropSizeHeight = modelInputHeight; - uint32_t cropLeftOffset = modelInputLeft; // must even - uint32_t cropRightOffset = cropLeftOffset + cropSizeWidth - oddNum; // must odd - uint32_t cropTopOffset = modelInputTop; // must even - uint32_t cropBottomOffset = cropTopOffset + cropSizeHeight - oddNum; // must odd - acldvppRoiConfig *cropArea_ = acldvppCreateRoiConfig(cropLeftOffset, cropRightOffset, cropTopOffset, cropBottomOffset); - /* processdecode */ - uint32_t vpcOutBufferSize_ = modelInputWidth * modelInputHeight * 3 / 2; + uint32_t vpcOutBufferSize_ = cropSizeWidth * cropSizeHeight * 3 / 2; void *vpcOutBufferDev_ = nullptr; acldvppMalloc(&vpcOutBufferDev_, vpcOutBufferSize_); acldvppPicDesc *vpcOutputDesc_ = acldvppCreatePicDesc(); acldvppSetPicDescData(vpcOutputDesc_, vpcOutBufferDev_); acldvppSetPicDescFormat(vpcOutputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420); - acldvppSetPicDescWidth(vpcOutputDesc_, modelInputWidth); - acldvppSetPicDescHeight(vpcOutputDesc_, modelInputHeight); - acldvppSetPicDescWidthStride(vpcOutputDesc_, modelInputWidth); - acldvppSetPicDescHeightStride(vpcOutputDesc_, modelInputHeight); + acldvppSetPicDescWidth(vpcOutputDesc_, cropSizeWidth); + acldvppSetPicDescHeight(vpcOutputDesc_, cropSizeHeight); + acldvppSetPicDescWidthStride(vpcOutputDesc_, cropSizeWidth); + acldvppSetPicDescHeightStride(vpcOutputDesc_, cropSizeHeight); acldvppSetPicDescSize(vpcOutputDesc_, vpcOutBufferSize_); aclrtStream stream_; @@ -102,7 +101,6 @@ vpc_img_info VPCUtil::crop(DeviceMemory *devMem, video_object_info obj) { (void)acldvppDestroyRoiConfig(cropArea_); cropArea_ = nullptr; - vpc_img_info img_info ; img_info.pic_desc = vpcOutputDesc_; img_info.object_id = obj.object_id; img_info.task_id = obj.task_id; //该物体属于的任务ID号 @@ -110,7 +108,6 @@ vpc_img_info VPCUtil::crop(DeviceMemory *devMem, video_object_info obj) { img_info.index = obj.index; //该物体所属类别的编号 img_info.confidence = obj.confidence; //该物体的置信度 - // (void)acldvppDestroyPicDesc(vpcOutputDesc_); // vpcOutputDesc_ = nullptr; @@ -126,7 +123,6 @@ int VPCUtil::init(int32_t devId){ deviceId_ = devId; aclError ret; - /* 2.Run the management resource application, including Device, Context, Stream */ aclrtSetDevice(deviceId_); aclrtCreateContext(&context_, deviceId_); @@ -136,22 +132,6 @@ int VPCUtil::init(int32_t devId){ } -static void check_coordinate(uint32_t& cropLeftOffset, uint32_t& cropRightOffset, uint32_t& cropTopOffset, uint32_t& cropBottomOffset, uint32_t width, uint32_t height){ - if (cropLeftOffset < 0){ - cropLeftOffset = 0; - } - if (cropTopOffset < 0){ - cropTopOffset = 0; - } - if(cropRightOffset > width){ - cropRightOffset = width; - } - if(cropBottomOffset > height){ - cropBottomOffset = height; - } - -} - vector VPCUtil::crop_batch(DeviceMemory *devMem, vector objs){ vector vec_img_info; @@ -161,8 +141,6 @@ vector VPCUtil::crop_batch(DeviceMemory *devMem, vector VPCUtil::crop_batch(DeviceMemory *devMem, vectorgetWidth(), devMem->getHeight()); + uint32_t cropLeftOffset = (obj.left + 1) / 2 * 2; // must even + uint32_t cropRightOffset = cropLeftOffset + cropSizeWidth - oddNum; // must odd + uint32_t cropTopOffset = (obj.top + 1) / 2 * 2; // must even + uint32_t cropBottomOffset = cropTopOffset + cropSizeHeight - oddNum; // must odd + + if(cropRightOffset <= cropLeftOffset || cropBottomOffset <= cropTopOffset){ + LOG_ERROR("{} <= {} || {} <= {} ", cropRightOffset, cropLeftOffset, cropBottomOffset, cropTopOffset); + // 释放之前成功的部分 再退出 + for(int i = 0; i < vecOutPtr_.size(); i++){ + if (vecOutPtr_[i] != nullptr){ + acldvppFree(vecOutPtr_[i]); + } + if (cropAreas[i] != nullptr) { + (void)acldvppDestroyRoiConfig(cropAreas[i]); + cropAreas[i] = nullptr; + } + } + return vec_img_info; + } cropAreas[i] = acldvppCreateRoiConfig(cropLeftOffset, cropRightOffset, cropTopOffset, cropBottomOffset); - uint32_t vpcOutBufferSize_ = modelInputWidth * modelInputHeight * 3 / 2; + uint32_t vpcOutBufferSize_ = cropSizeWidth * cropSizeHeight * 3 / 2; void *vpcBatchOutputBufferDev = nullptr; auto ret = acldvppMalloc(&vpcBatchOutputBufferDev, vpcOutBufferSize_); if (ret != ACL_SUCCESS) { @@ -223,6 +209,10 @@ vector VPCUtil::crop_batch(DeviceMemory *devMem, vector VPCUtil::crop_batch(DeviceMemory *devMem, vector