@@ -64,7 +64,6 @@ AsyncDetector::AsyncDetector(const cv::CommandLineParser& parser)
6464// /
6565AsyncDetector::~AsyncDetector ()
6666{
67-
6867}
6968
7069// /
@@ -90,6 +89,8 @@ void AsyncDetector::Process()
9089
9190 for (; !stopFlag;)
9291 {
92+ int64 t1 = cv::getTickCount ();
93+
9394 // Show frame after detecting and tracking
9495 frame_ptr processedFrame = m_framesQue.GetFirstProcessedFrame ();
9596 if (!processedFrame)
@@ -101,14 +102,17 @@ void AsyncDetector::Process()
101102 int64 t2 = cv::getTickCount ();
102103
103104 allTime += t2 - processedFrame->m_dt ;
104- int currTime = cvRound (1000 * (t2 - processedFrame-> m_dt ) / freq);
105+ int currTime = cvRound (1000 * (t2 - t1 ) / freq);
105106
106107 DrawData (processedFrame, framesCounter, currTime);
107108
108- if (!m_outFile.empty () && !writer.isOpened ())
109- writer.open (m_outFile, cv::VideoWriter::fourcc (' H' , ' F' , ' Y' , ' U' ), m_fps, processedFrame->m_frame .size (), true );
110- if (writer.isOpened ())
111- writer << processedFrame->m_frame ;
109+ if (!m_outFile.empty ())
110+ {
111+ if (!writer.isOpened ())
112+ writer.open (m_outFile, cv::VideoWriter::fourcc (' H' , ' F' , ' Y' , ' U' ), m_fps, processedFrame->m_frame .size (), true );
113+ if (writer.isOpened ())
114+ writer << processedFrame->m_frame ;
115+ }
112116
113117#ifndef SILENT_WORK
114118 cv::imshow (" Video" , processedFrame->m_frame );
@@ -216,7 +220,7 @@ void AsyncDetector::DrawData(frame_ptr frameInfo, int framesCounter, int currTim
216220 {
217221 std::cout << " Frame " << framesCounter << " : " ;
218222 int id = frameInfo->m_inDetector .load ();
219- if (id > 0 )
223+ if (id != FrameInfo::StateNotProcessed )
220224 std::cout << " (" << id << " ) detects = " << frameInfo->m_regions .size () << " , " ;
221225 std::cout << " tracks = " << frameInfo->m_tracks .size () << " , time = " << currTime << std::endl;
222226 }
@@ -242,6 +246,26 @@ void AsyncDetector::DrawData(frame_ptr frameInfo, int framesCounter, int currTim
242246 cv::Size labelSize = cv::getTextSize (label, cv::FONT_HERSHEY_SIMPLEX, 0.5 , 1 , &baseLine);
243247
244248 cv::Rect brect = track.m_rrect .boundingRect ();
249+ if (brect.x < 0 )
250+ {
251+ brect.width = std::min (brect.width , frameInfo->m_frame .cols - 1 );
252+ brect.x = 0 ;
253+ }
254+ else if (brect.x + brect.width >= frameInfo->m_frame .cols )
255+ {
256+ brect.x = std::max (0 , frameInfo->m_frame .cols - brect.width - 1 );
257+ brect.width = std::min (brect.width , frameInfo->m_frame .cols - 1 );
258+ }
259+ if (brect.y - labelSize.height < 0 )
260+ {
261+ brect.height = std::min (brect.height , frameInfo->m_frame .rows - 1 );
262+ brect.y = labelSize.height ;
263+ }
264+ else if (brect.y + brect.height >= frameInfo->m_frame .rows )
265+ {
266+ brect.y = std::max (0 , frameInfo->m_frame .rows - brect.height - 1 );
267+ brect.height = std::min (brect.height , frameInfo->m_frame .rows - 1 );
268+ }
245269 DrawFilledRect (frameInfo->m_frame , cv::Rect (cv::Point (brect.x , brect.y - labelSize.height ), cv::Size (labelSize.width , labelSize.height + baseLine)), cv::Scalar (200 , 200 , 200 ), 150 );
246270 cv::putText (frameInfo->m_frame , label, brect.tl (), cv::FONT_HERSHEY_SIMPLEX, 0.5 , cv::Scalar (0 , 0 , 0 ));
247271 }
@@ -287,8 +311,8 @@ void AsyncDetector::CaptureThread(std::string fileName, int startFrame, float* f
287311 detectorConfig.emplace("modelConfiguration", pathToModel + "yolov3-tiny.cfg");
288312 detectorConfig.emplace("modelBinary", pathToModel + "yolov3-tiny.weights");
289313#else
290- detectorConfig.emplace (" modelConfiguration" , pathToModel + " yolov3 .cfg" );
291- detectorConfig.emplace (" modelBinary" , pathToModel + " yolov3 .weights" );
314+ detectorConfig.emplace (" modelConfiguration" , pathToModel + " yolov4-csp .cfg" );
315+ detectorConfig.emplace (" modelBinary" , pathToModel + " yolov4-csp .weights" );
292316#endif
293317 detectorConfig.emplace (" classNames" , pathToModel + " coco.names" );
294318 detectorConfig.emplace (" confidenceThreshold" , " 0.3" );
@@ -312,7 +336,7 @@ void AsyncDetector::CaptureThread(std::string fileName, int startFrame, float* f
312336 trackerSettings.SetDistance (tracking::DistCenters);
313337 trackerSettings.m_kalmanType = tracking::KalmanLinear;
314338 trackerSettings.m_filterGoal = tracking::FilterRect;
315- trackerSettings.m_lostTrackType = tracking::TrackNone ; // Use KCF tracker for collisions resolving. Used if m_filterGoal == tracking::FilterRect
339+ trackerSettings.m_lostTrackType = tracking::TrackCSRT ; // Use KCF tracker for collisions resolving. Used if m_filterGoal == tracking::FilterRect
316340 trackerSettings.m_matchType = tracking::MatchHungrian;
317341 trackerSettings.m_dt = 0 .2f ; // Delta time for Kalman filter
318342 trackerSettings.m_accelNoiseMag = 0 .3f ; // Accel noise magnitude for Kalman filter
@@ -360,8 +384,8 @@ void AsyncDetector::CaptureThread(std::string fileName, int startFrame, float* f
360384
361385 framesQue->AddNewFrame (frameInfo, 15 );
362386
363- #if 0
364- std::this_thread::sleep_for(std::chrono::milliseconds(1000 / cvRound(*fps)));
387+ #if 1
388+ std::this_thread::sleep_for (std::chrono::milliseconds (1000 / cvRound (*fps) - 1 ));
365389#else
366390 std::this_thread::sleep_for(std::chrono::milliseconds(1));
367391 #endif
@@ -401,7 +425,7 @@ void AsyncDetector::DetectThread(const config_t& config, cv::Mat firstFrame, Fra
401425 const regions_t & regions = detector->GetDetects ();
402426 frameInfo->m_regions .assign (regions.begin (), regions.end ());
403427
404- frameInfo->m_inDetector .store (2 );
428+ frameInfo->m_inDetector .store (FrameInfo::StateCompleted );
405429 framesQue->Signal (frameInfo->m_dt );
406430 }
407431 }
@@ -423,7 +447,7 @@ void AsyncDetector::TrackingThread(const TrackerSettings& settings, FramesQueue*
423447 tracker->Update (frameInfo->m_regions , frameInfo->m_clFrame , frameInfo->m_fps );
424448
425449 frameInfo->m_tracks = tracker->GetTracks ();
426- frameInfo->m_inTracker .store (2 );
450+ frameInfo->m_inTracker .store (FrameInfo::StateCompleted );
427451 framesQue->Signal (frameInfo->m_dt );
428452 }
429453 }
0 commit comments