Skip to content

Commit 6871a06

Browse files
committed
Fix some bugs
1 parent a07fbe4 commit 6871a06

4 files changed

Lines changed: 52 additions & 20 deletions

File tree

example/VideoExample.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ void VideoExample::SyncProcess()
151151

152152
FrameInfo frameInfo(m_batchSize);
153153
frameInfo.m_frames.emplace_back(frame);
154+
frameInfo.m_frameInds.emplace_back(framesCounter);
155+
++framesCounter;
154156
for (size_t i = 1; i < m_batchSize; ++i)
155157
{
156158
capture >> frame;
@@ -366,8 +368,7 @@ void VideoExample::CaptureAndDetect(VideoExample* thisPtr, std::atomic<bool>& st
366368
frameInfo.m_cond.notify_one();
367369
break;
368370
}
369-
frameInfo.m_frameInds.emplace_back(framesCounter);
370-
371+
frameInfo.m_frameInds[i] = framesCounter;
371372
++framesCounter;
372373
}
373374
if (frameInfo.m_frames.size() < frameInfo.m_batchSize)
@@ -419,8 +420,9 @@ void VideoExample::Detection(FrameInfo& frame)
419420
if (m_detector->CanGrayProcessing())
420421
frames.emplace_back(frame.m_frames[i].GetUMatGray());
421422
else
422-
frames.emplace_back(frame.m_frames[i].GetUMatBGR());
423+
frames.push_back(frame.m_frames[i].GetUMatBGR());
423424
}
425+
frame.CleanRegions();
424426
m_detector->Detect(frames, frame.m_regions);
425427
}
426428

@@ -431,6 +433,8 @@ void VideoExample::Detection(FrameInfo& frame)
431433
///
432434
void VideoExample::Tracking(FrameInfo& frame)
433435
{
436+
assert(frame.m_regions.size() == frame.m_frames.size());
437+
434438
for (size_t i = 0; i < frame.m_frames.size(); ++i)
435439
{
436440
if (m_tracker->CanColorFrameToTrack())

example/VideoExample.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ class Frame
197197
{
198198
if (m_umBGR.empty())
199199
m_umBGR = m_mBGR.getUMat(cv::ACCESS_READ);
200-
return m_umGray;
200+
return m_umBGR;
201201
}
202202
///
203203
const cv::UMat& GetUMatGray()
@@ -256,6 +256,17 @@ struct FrameInfo
256256
m_frameInds.reserve(m_batchSize);
257257
}
258258

259+
///
260+
void CleanRegions()
261+
{
262+
if (m_regions.size() != m_batchSize)
263+
m_regions.resize(m_batchSize);
264+
for (auto& reg : m_regions)
265+
{
266+
reg.clear();
267+
}
268+
}
269+
259270
std::vector<Frame> m_frames;
260271
std::vector<regions_t> m_regions;
261272
std::vector<int> m_frameInds;

src/Detector/YoloDarknetDetector.cpp

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ void YoloDarknetDetector::Detect(const cv::UMat& colorFrame)
145145
}
146146
}
147147

148-
if (crops.size() > 1)
148+
if (crops.size() > 1 || m_batchSize > 1)
149149
{
150150
nms3<CRegion>(tmpRegions, m_regions, 0.4f,
151151
[](const CRegion& reg) { return reg.m_brect; },
@@ -305,25 +305,42 @@ void YoloDarknetDetector::FillBatchImg(const std::vector<cv::Mat>& batch, image_
305305
///
306306
void YoloDarknetDetector::Detect(const std::vector<cv::UMat>& frames, std::vector<regions_t>& regions)
307307
{
308-
std::vector<cv::Mat> batch;
309-
for (const auto& frame : frames)
308+
if (frames.size() == 1)
310309
{
311-
batch.emplace_back(frame.getMat(cv::ACCESS_READ));
310+
Detect(frames[0].getMat(cv::ACCESS_READ), regions[0]);
312311
}
312+
else
313+
{
314+
std::vector<cv::Mat> batch;
315+
for (const auto& frame : frames)
316+
{
317+
batch.emplace_back(frame.getMat(cv::ACCESS_READ));
318+
}
313319

314-
image_t detImage;
315-
FillBatchImg(batch, detImage);
316-
std::vector<std::vector<bbox_t>> result_vec = m_detector->detectBatch(detImage, static_cast<int>(frames.size()), m_netSize.width, m_netSize.height, m_confidenceThreshold);
320+
image_t detImage;
321+
FillBatchImg(batch, detImage);
322+
std::vector<std::vector<bbox_t>> result_vec = m_detector->detectBatch(detImage, static_cast<int>(frames.size()), m_netSize.width, m_netSize.height, m_confidenceThreshold);
317323

318-
float wk = static_cast<float>(frames[0].cols) / m_netSize.width;
319-
float hk = static_cast<float>(frames[0].rows) / m_netSize.height;
320-
for (size_t i = 0; i < regions.size(); ++i)
321-
{
322-
for (const auto& bbox : result_vec[i])
324+
regions_t tmpRegions;
325+
tmpRegions.reserve(result_vec[0].size() + 16);
326+
float wk = static_cast<float>(frames[0].cols) / m_netSize.width;
327+
float hk = static_cast<float>(frames[0].rows) / m_netSize.height;
328+
for (size_t i = 0; i < regions.size(); ++i)
323329
{
324-
if (m_classesWhiteList.empty() || m_classesWhiteList.find(T2T(bbox.obj_id)) != std::end(m_classesWhiteList))
325-
regions[i].emplace_back(cv::Rect(cvRound(wk * bbox.x), cvRound(hk * bbox.y), cvRound(wk * bbox.w), cvRound(hk * bbox.h)), T2T(bbox.obj_id), bbox.prob);
330+
tmpRegions.clear();
331+
for (const auto& bbox : result_vec[i])
332+
{
333+
if (m_classesWhiteList.empty() || m_classesWhiteList.find(T2T(bbox.obj_id)) != std::end(m_classesWhiteList))
334+
tmpRegions.emplace_back(cv::Rect(cvRound(wk * bbox.x), cvRound(hk * bbox.y), cvRound(wk * bbox.w), cvRound(hk * bbox.h)), T2T(bbox.obj_id), bbox.prob);
335+
}
336+
337+
nms3<CRegion>(tmpRegions, regions[i], 0.4f,
338+
[](const CRegion& reg) { return reg.m_brect; },
339+
[](const CRegion& reg) { return reg.m_confidence; },
340+
[](const CRegion& reg) { return reg.m_type; },
341+
0, 0.f);
326342
}
343+
344+
m_regions.assign(std::begin(regions.back()), std::end(regions.back()));
327345
}
328-
m_regions.assign(std::begin(regions.back()), std::end(regions.back()));
329346
}

src/Detector/darknet/src/yolo_v2_class.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ LIB_API std::vector<std::vector<bbox_t>> Detector::detectBatch(image_t img, int
361361
in_img.data = img.data;
362362
det_num_pair* prediction = network_predict_batch(&net, in_img, batch_size, width, height, thresh, hier_thresh, 0, 0, 0);
363363

364-
std::vector<std::vector<bbox_t>> bbox_vec;
364+
std::vector<std::vector<bbox_t>> bbox_vec(batch_size);
365365

366366
for (int bi = 0; bi < batch_size; ++bi)
367367
{

0 commit comments

Comments
 (0)