Commit c692ff9ec0ef9b1ba9f9c71da5eed0a4ef1b41e6
1 parent
29b64a88
修正抠图存在绿屏、花屏问题
Showing
1 changed file
with
55 additions
and
65 deletions
src/util/vpc_util.cpp
... | ... | @@ -18,7 +18,6 @@ |
18 | 18 | #include "../decoder/interface/DeviceMemory.hpp" |
19 | 19 | #include "../common/logger.hpp" |
20 | 20 | |
21 | - | |
22 | 21 | void VPCUtil::release() |
23 | 22 | { |
24 | 23 | aclError ret; |
... | ... | @@ -44,9 +43,27 @@ void VPCUtil::release() |
44 | 43 | LOG_INFO("end to reset device is %d", deviceId_); |
45 | 44 | } |
46 | 45 | |
47 | - | |
48 | 46 | vpc_img_info VPCUtil::crop(DeviceMemory *devMem, video_object_info obj) { |
49 | 47 | |
48 | + vpc_img_info img_info ; | |
49 | + | |
50 | + uint32_t cropSizeWidth = (obj.right - obj.left) / 16 * 16; | |
51 | + uint32_t cropSizeHeight = (obj.bottom - obj.top) / 2 * 2; | |
52 | + | |
53 | + uint32_t oddNum = 1; | |
54 | + uint32_t cropLeftOffset = (obj.left + 1) / 2 * 2; // must even | |
55 | + uint32_t cropRightOffset = cropLeftOffset + cropSizeWidth - oddNum; // must odd | |
56 | + uint32_t cropTopOffset = (obj.top + 1) / 2 * 2; // must even | |
57 | + uint32_t cropBottomOffset = cropTopOffset + cropSizeHeight - oddNum; // must odd | |
58 | + | |
59 | + if(cropRightOffset <= cropLeftOffset || cropBottomOffset <= cropTopOffset){ | |
60 | + return img_info; | |
61 | + } | |
62 | + | |
63 | + // LOG_INFO("crop src {} ({}, {}, {}, {})", obj.object_id, obj.left, obj.right, obj.top, obj.bottom); | |
64 | + // LOG_INFO("crop {} ({}, {}, {}, {})", obj.object_id, cropLeftOffset, cropRightOffset, cropTopOffset, cropBottomOffset); | |
65 | + acldvppRoiConfig *cropArea_ = acldvppCreateRoiConfig(cropLeftOffset, cropRightOffset, cropTopOffset, cropBottomOffset); | |
66 | + | |
50 | 67 | acldvppPicDesc *vpcInputDesc_ = acldvppCreatePicDesc(); |
51 | 68 | acldvppSetPicDescData(vpcInputDesc_, devMem->getMem()); |
52 | 69 | acldvppSetPicDescFormat(vpcInputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420); |
... | ... | @@ -56,35 +73,17 @@ vpc_img_info VPCUtil::crop(DeviceMemory *devMem, video_object_info obj) { |
56 | 73 | acldvppSetPicDescHeightStride(vpcInputDesc_, devMem->getHeightStride()); |
57 | 74 | acldvppSetPicDescSize(vpcInputDesc_, devMem->getSize()); |
58 | 75 | |
59 | - const int orimodelInputWidth = obj.right - obj.left; // cur model shape is 224 * 224 | |
60 | - const int orimodelInputHeight = obj.bottom - obj.top; | |
61 | - const int modelInputLeft = obj.left; // cur model shape is 224 * 224 | |
62 | - const int modelInputTop = obj.top; | |
63 | - | |
64 | - // GetPicDevBuffer4JpegD | |
65 | - int modelInputWidth = (orimodelInputWidth + 15) / 16 * 16; | |
66 | - int modelInputHeight = (orimodelInputHeight + 1) / 2 * 2; | |
67 | - | |
68 | - uint32_t oddNum = 1; | |
69 | - uint32_t cropSizeWidth = modelInputWidth; | |
70 | - uint32_t cropSizeHeight = modelInputHeight; | |
71 | - uint32_t cropLeftOffset = modelInputLeft; // must even | |
72 | - uint32_t cropRightOffset = cropLeftOffset + cropSizeWidth - oddNum; // must odd | |
73 | - uint32_t cropTopOffset = modelInputTop; // must even | |
74 | - uint32_t cropBottomOffset = cropTopOffset + cropSizeHeight - oddNum; // must odd | |
75 | - acldvppRoiConfig *cropArea_ = acldvppCreateRoiConfig(cropLeftOffset, cropRightOffset, cropTopOffset, cropBottomOffset); | |
76 | - | |
77 | 76 | /* processdecode */ |
78 | - uint32_t vpcOutBufferSize_ = modelInputWidth * modelInputHeight * 3 / 2; | |
77 | + uint32_t vpcOutBufferSize_ = cropSizeWidth * cropSizeHeight * 3 / 2; | |
79 | 78 | void *vpcOutBufferDev_ = nullptr; |
80 | 79 | acldvppMalloc(&vpcOutBufferDev_, vpcOutBufferSize_); |
81 | 80 | acldvppPicDesc *vpcOutputDesc_ = acldvppCreatePicDesc(); |
82 | 81 | acldvppSetPicDescData(vpcOutputDesc_, vpcOutBufferDev_); |
83 | 82 | acldvppSetPicDescFormat(vpcOutputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420); |
84 | - acldvppSetPicDescWidth(vpcOutputDesc_, modelInputWidth); | |
85 | - acldvppSetPicDescHeight(vpcOutputDesc_, modelInputHeight); | |
86 | - acldvppSetPicDescWidthStride(vpcOutputDesc_, modelInputWidth); | |
87 | - acldvppSetPicDescHeightStride(vpcOutputDesc_, modelInputHeight); | |
83 | + acldvppSetPicDescWidth(vpcOutputDesc_, cropSizeWidth); | |
84 | + acldvppSetPicDescHeight(vpcOutputDesc_, cropSizeHeight); | |
85 | + acldvppSetPicDescWidthStride(vpcOutputDesc_, cropSizeWidth); | |
86 | + acldvppSetPicDescHeightStride(vpcOutputDesc_, cropSizeHeight); | |
88 | 87 | acldvppSetPicDescSize(vpcOutputDesc_, vpcOutBufferSize_); |
89 | 88 | |
90 | 89 | aclrtStream stream_; |
... | ... | @@ -102,7 +101,6 @@ vpc_img_info VPCUtil::crop(DeviceMemory *devMem, video_object_info obj) { |
102 | 101 | (void)acldvppDestroyRoiConfig(cropArea_); |
103 | 102 | cropArea_ = nullptr; |
104 | 103 | |
105 | - vpc_img_info img_info ; | |
106 | 104 | img_info.pic_desc = vpcOutputDesc_; |
107 | 105 | img_info.object_id = obj.object_id; |
108 | 106 | img_info.task_id = obj.task_id; //该物体属于的任务ID号 |
... | ... | @@ -110,7 +108,6 @@ vpc_img_info VPCUtil::crop(DeviceMemory *devMem, video_object_info obj) { |
110 | 108 | img_info.index = obj.index; //该物体所属类别的编号 |
111 | 109 | img_info.confidence = obj.confidence; //该物体的置信度 |
112 | 110 | |
113 | - | |
114 | 111 | // (void)acldvppDestroyPicDesc(vpcOutputDesc_); |
115 | 112 | // vpcOutputDesc_ = nullptr; |
116 | 113 | |
... | ... | @@ -126,7 +123,6 @@ int VPCUtil::init(int32_t devId){ |
126 | 123 | deviceId_ = devId; |
127 | 124 | |
128 | 125 | aclError ret; |
129 | - /* 2.Run the management resource application, including Device, Context, Stream */ | |
130 | 126 | aclrtSetDevice(deviceId_); |
131 | 127 | aclrtCreateContext(&context_, deviceId_); |
132 | 128 | |
... | ... | @@ -136,22 +132,6 @@ int VPCUtil::init(int32_t devId){ |
136 | 132 | |
137 | 133 | } |
138 | 134 | |
139 | -static void check_coordinate(uint32_t& cropLeftOffset, uint32_t& cropRightOffset, uint32_t& cropTopOffset, uint32_t& cropBottomOffset, uint32_t width, uint32_t height){ | |
140 | - if (cropLeftOffset < 0){ | |
141 | - cropLeftOffset = 0; | |
142 | - } | |
143 | - if (cropTopOffset < 0){ | |
144 | - cropTopOffset = 0; | |
145 | - } | |
146 | - if(cropRightOffset > width){ | |
147 | - cropRightOffset = width; | |
148 | - } | |
149 | - if(cropBottomOffset > height){ | |
150 | - cropBottomOffset = height; | |
151 | - } | |
152 | - | |
153 | -} | |
154 | - | |
155 | 135 | vector<vpc_img_info> VPCUtil::crop_batch(DeviceMemory *devMem, vector<video_object_info> objs){ |
156 | 136 | |
157 | 137 | vector<vpc_img_info> vec_img_info; |
... | ... | @@ -161,8 +141,6 @@ vector<vpc_img_info> VPCUtil::crop_batch(DeviceMemory *devMem, vector<video_obje |
161 | 141 | return vec_img_info; |
162 | 142 | } |
163 | 143 | |
164 | - /* 1.ACL initialization */ | |
165 | - // aclInit(nullptr); | |
166 | 144 | aclError ret; |
167 | 145 | aclrtSetDevice(deviceId_); |
168 | 146 | ret = aclrtSetCurrentContext(context_); |
... | ... | @@ -195,25 +173,33 @@ vector<vpc_img_info> VPCUtil::crop_batch(DeviceMemory *devMem, vector<video_obje |
195 | 173 | for (uint32_t i = 0; i < outputBatchSize_; i++) { |
196 | 174 | video_object_info obj = objs[i]; |
197 | 175 | |
198 | - int orimodelInputWidth = obj.right - obj.left; | |
199 | - int orimodelInputHeight = obj.bottom - obj.top; | |
200 | - // GetPicDevBuffer4JpegD | |
201 | - int modelInputWidth = (orimodelInputWidth + 15) / 16 * 16; | |
202 | - int modelInputHeight = (orimodelInputHeight + 1) / 2 * 2; | |
203 | - | |
176 | + uint32_t cropSizeWidth = (obj.right - obj.left) / 16 * 16; | |
177 | + uint32_t cropSizeHeight = (obj.bottom - obj.top) / 2 * 2; | |
178 | + | |
204 | 179 | uint32_t oddNum = 1; |
205 | - uint32_t cropSizeWidth = modelInputWidth; | |
206 | - uint32_t cropSizeHeight = modelInputHeight; | |
207 | - uint32_t cropLeftOffset = obj.left; // must even | |
208 | - uint32_t cropRightOffset = obj.right;//cropLeftOffset + cropSizeWidth - oddNum; // must odd | |
209 | - uint32_t cropTopOffset = obj.top; // must even | |
210 | - uint32_t cropBottomOffset = obj.bottom;//cropTopOffset + cropSizeHeight - oddNum; // must odd | |
211 | - | |
212 | - check_coordinate(cropLeftOffset, cropRightOffset, cropTopOffset, cropBottomOffset, devMem->getWidth(), devMem->getHeight()); | |
180 | + uint32_t cropLeftOffset = (obj.left + 1) / 2 * 2; // must even | |
181 | + uint32_t cropRightOffset = cropLeftOffset + cropSizeWidth - oddNum; // must odd | |
182 | + uint32_t cropTopOffset = (obj.top + 1) / 2 * 2; // must even | |
183 | + uint32_t cropBottomOffset = cropTopOffset + cropSizeHeight - oddNum; // must odd | |
184 | + | |
185 | + if(cropRightOffset <= cropLeftOffset || cropBottomOffset <= cropTopOffset){ | |
186 | + LOG_ERROR("{} <= {} || {} <= {} ", cropRightOffset, cropLeftOffset, cropBottomOffset, cropTopOffset); | |
187 | + // 释放之前成功的部分 再退出 | |
188 | + for(int i = 0; i < vecOutPtr_.size(); i++){ | |
189 | + if (vecOutPtr_[i] != nullptr){ | |
190 | + acldvppFree(vecOutPtr_[i]); | |
191 | + } | |
192 | + if (cropAreas[i] != nullptr) { | |
193 | + (void)acldvppDestroyRoiConfig(cropAreas[i]); | |
194 | + cropAreas[i] = nullptr; | |
195 | + } | |
196 | + } | |
197 | + return vec_img_info; | |
198 | + } | |
213 | 199 | |
214 | 200 | cropAreas[i] = acldvppCreateRoiConfig(cropLeftOffset, cropRightOffset, cropTopOffset, cropBottomOffset); |
215 | 201 | |
216 | - uint32_t vpcOutBufferSize_ = modelInputWidth * modelInputHeight * 3 / 2; | |
202 | + uint32_t vpcOutBufferSize_ = cropSizeWidth * cropSizeHeight * 3 / 2; | |
217 | 203 | void *vpcBatchOutputBufferDev = nullptr; |
218 | 204 | auto ret = acldvppMalloc(&vpcBatchOutputBufferDev, vpcOutBufferSize_); |
219 | 205 | if (ret != ACL_SUCCESS) { |
... | ... | @@ -223,6 +209,10 @@ vector<vpc_img_info> VPCUtil::crop_batch(DeviceMemory *devMem, vector<video_obje |
223 | 209 | if (vecOutPtr_[i] != nullptr){ |
224 | 210 | acldvppFree(vecOutPtr_[i]); |
225 | 211 | } |
212 | + if (cropAreas[i] != nullptr) { | |
213 | + (void)acldvppDestroyRoiConfig(cropAreas[i]); | |
214 | + cropAreas[i] = nullptr; | |
215 | + } | |
226 | 216 | } |
227 | 217 | return vec_img_info; |
228 | 218 | } |
... | ... | @@ -230,10 +220,10 @@ vector<vpc_img_info> VPCUtil::crop_batch(DeviceMemory *devMem, vector<video_obje |
230 | 220 | vpcOutputDesc = acldvppGetPicDesc(outputBatchPicDesc_, i); |
231 | 221 | (void)acldvppSetPicDescData(vpcOutputDesc, vpcBatchOutputBufferDev); |
232 | 222 | (void)acldvppSetPicDescFormat(vpcOutputDesc, PIXEL_FORMAT_YUV_SEMIPLANAR_420); |
233 | - (void)acldvppSetPicDescWidth(vpcOutputDesc, modelInputWidth); | |
234 | - (void)acldvppSetPicDescHeight(vpcOutputDesc, modelInputHeight); | |
235 | - (void)acldvppSetPicDescWidthStride(vpcOutputDesc, modelInputWidth); | |
236 | - (void)acldvppSetPicDescHeightStride(vpcOutputDesc, modelInputHeight); | |
223 | + (void)acldvppSetPicDescWidth(vpcOutputDesc, cropSizeWidth); | |
224 | + (void)acldvppSetPicDescHeight(vpcOutputDesc, cropSizeHeight); | |
225 | + (void)acldvppSetPicDescWidthStride(vpcOutputDesc, cropSizeWidth); | |
226 | + (void)acldvppSetPicDescHeightStride(vpcOutputDesc, cropSizeHeight); | |
237 | 227 | (void)acldvppSetPicDescSize(vpcOutputDesc, vpcOutBufferSize_); |
238 | 228 | } |
239 | 229 | ... | ... |