@@ -35,54 +35,72 @@ void DetectionThread::run()
3535 Dim2<int > detectionResolution (detector_->getInputImageDim ());
3636 Point<float > fisheyeCenter (resolution.width / 2 .f , resolution.height / 2 .f );
3737
38- std::vector<DewarpingParameters> dewarpingParams = getDetectionDewarpingParameters (resolution, dewarpCount_);
39- std::vector<ImageFloat> detectionImages = getDetectionImages (detectionResolution, dewarpCount_);
40- std::vector<DewarpingMapping> dewarpingMappings =
41- getDewarpingMappings (dewarpingParams, resolution, detectionResolution, dewarpCount_);
38+ std::vector<DewarpingParameters> dewarpingParams;
39+ std::vector<ImageFloat> detectionImages;
40+ std::vector<DewarpingMapping> dewarpingMappings;
4241 std::vector<SphericalAngleRect> detections;
4342
44- std::cout << " DetectionThread loop started" << std::endl;
45-
46- while (!isAbortRequested ())
43+ try
4744 {
48- // Make sure a new image is actually in the buffer
49- while (!imageBuffer_->getAndClearSwapCount () && !isAbortRequested ())
50- {
51- std::this_thread::sleep_for (std::chrono::milliseconds (1 ));
52- }
45+ // Allocate and prepare objects for dewarping and detection
46+ dewarpingParams = getDetectionDewarpingParameters (resolution, dewarpCount_);
47+ detectionImages = getDetectionImages (detectionResolution, dewarpCount_);
48+ dewarpingMappings = getDewarpingMappings (dewarpingParams, resolution, detectionResolution, dewarpCount_);
5349
54- // We lock the image so we can use it without it being overwritten
55- imageBuffer_->lockInUse ();
56- const Image& image = imageBuffer_->getLocked ();
50+ std::cout << " DetectionThread loop started" << std::endl;
5751
58- // Dewarp each view, detect on each view and concatenate the results
59- for (int i = 0 ; i < dewarpCount_; ++i)
52+ while (!isAbortRequested ())
6053 {
61- dewarper_->dewarpImage (image, detectionImages[i], dewarpingMappings[i]);
62- synchronizer_->sync ();
63- const std::vector<Rectangle> viewDetections = detector_->detectInImage (detectionImages[i]);
54+ // Make sure a new image is actually in the buffer
55+ while (!imageBuffer_->getAndClearSwapCount () && !isAbortRequested ())
56+ {
57+ std::this_thread::sleep_for (std::chrono::milliseconds (1 ));
58+ }
6459
65- for (const Rectangle& detection : viewDetections)
60+ // No need to do a detection if thread is aborted
61+ if (isAbortRequested ())
6662 {
67- detections.push_back (getAngleRectFromDewarpedImageRectangle (
68- detection, dewarpingParams[i], detectionImages[i], fisheyeCenter, dewarpingConfig_.fisheyeAngle ));
63+ break ;
6964 }
70- }
7165
72- bool success = false ;
66+ // We lock the image so we can use it without it being overwritten
67+ imageBuffer_->lockInUse ();
68+ const Image& image = imageBuffer_->getLocked ();
7369
74- // Output the detections, if queue is full keep trying...
75- while (!success && !isAbortRequested ())
76- {
77- success = detectionQueue_->try_enqueue (std::move (detections));
70+ // Dewarp each view, detect on each view and concatenate the results
71+ for (int i = 0 ; i < dewarpCount_; ++i)
72+ {
73+ dewarper_->dewarpImage (image, detectionImages[i], dewarpingMappings[i]);
74+ synchronizer_->sync ();
75+ const std::vector<Rectangle> viewDetections = detector_->detectInImage (detectionImages[i]);
76+
77+ for (const Rectangle& detection : viewDetections)
78+ {
79+ detections.push_back (getAngleRectFromDewarpedImageRectangle (detection, dewarpingParams[i],
80+ detectionImages[i], fisheyeCenter,
81+ dewarpingConfig_.fisheyeAngle ));
82+ }
83+ }
7884
79- if (!success)
85+ bool success = false ;
86+
87+ // Output the detections, if queue is full keep trying...
88+ while (!success && !isAbortRequested ())
8089 {
81- std::this_thread::sleep_for (std::chrono::milliseconds (1 ));
90+ success = detectionQueue_->try_enqueue (std::move (detections));
91+
92+ if (!success)
93+ {
94+ std::this_thread::sleep_for (std::chrono::milliseconds (1 ));
95+ }
8296 }
83- }
8497
85- detections.clear (); // Just making sure...
98+ detections.clear (); // Just making sure... Should be empty because of the std::move
99+ }
100+ }
101+ catch (const std::exception& e)
102+ {
103+ std::cout << " Error in detection thread : " << e.what () << std::endl;
86104 }
87105
88106 objectFactory_->deallocateObjectVector (detectionImages);
0 commit comments