@@ -18,7 +18,6 @@ InstanceSegmentation::InstanceSegmentation(const YAML::Node &config) : Model(con
1818}
1919
2020void InstanceSegmentation::Inference (const std::string &input_path, const cv::String &save_path, const bool video) {
21-
2221 cv::VideoCapture capture;
2322 capture.open (input_path);
2423 cv::Size size = cv::Size ((int )capture.get (cv::CAP_PROP_FRAME_WIDTH), (int )capture.get (cv::CAP_PROP_FRAME_HEIGHT));
@@ -35,33 +34,26 @@ void InstanceSegmentation::Inference(const std::string &input_path, const cv::St
3534 int index = 0 ;
3635 float total_time = 0 ;
3736 cv::Mat frame;
38-
39- while (capture.isOpened ())
40- {
37+ cuda_preprocess_init (maxImageSize);
38+ while (capture.isOpened ()) {
4139 index++;
42- if (imgBatch.size () < batchSize) // get input
43- {
40+ if (imgBatch.size () < batchSize) {
4441 capture.read (frame);
45-
46- if (frame.empty ())
47- {
48- sample::gLogWarning << " no more video or camera frame" << std::endl;
42+ if (frame.empty ()) {
43+ std::cout << " no more video or camera frame" << std::endl;
4944 auto start_time = std::chrono::high_resolution_clock::now ();
5045 std::vector<Segmentations> seg_results = InferenceImages (imgBatch);
5146 auto end_time = std::chrono::high_resolution_clock::now ();
5247 segs.insert (segs.end (), seg_results.begin (), seg_results.end ());
5348 imgs.insert (imgs.end (), imgBatch.begin (), imgBatch.end ());
54- imgBatch.clear (); // clear
49+ imgBatch.clear ();
5550 total_time += std::chrono::duration<float , std::milli>(end_time - start_time).count ();
5651 break ;
57- }
58- else
59- {
52+ } else {
6053 imgBatch.emplace_back (frame.clone ());
6154 }
6255 }
63- else // infer
64- {
56+ else {
6557 auto start_time = std::chrono::high_resolution_clock::now ();
6658 auto seg_results = InferenceImages (imgBatch);
6759 auto end_time = std::chrono::high_resolution_clock::now ();
@@ -84,7 +76,7 @@ void InstanceSegmentation::Inference(const std::string &input_path, const std::s
8476 std::vector<std::string> imgInfo;
8577 imgInfo.reserve (batchSize);
8678 float total_time = 0 ;
87- cuda_preprocess_init (kMaxInputImageSize );
79+ cuda_preprocess_init (maxImageSize );
8880 // cuda_postprocess_init(39, imageWidth, imageHeight);
8981 for (const std::string &image_name : image_list) {
9082 index++;
@@ -176,11 +168,39 @@ void InstanceSegmentation::Visualize(const std::vector<Segmentations> &segmentat
176168 continue ;
177169 auto instances = segmentations[i].segs ;
178170 for (const auto &ins : instances) {
171+ cv::Mat mask = ins.mask ;
172+ cv::Mat img_mask = scale_mask (mask, frame);
173+ cv::Mat reg_img = cv::Mat::zeros (frame.rows , frame.cols , CV_8UC1);
174+ for (int row = ins.y ; row < ins.y + ins.h ; row++) {
175+ if (row < 0 || row >= frame.rows ) continue ;
176+ cv::Vec<uint8_t , 1 > *data_Ptr = reg_img.ptr <cv::Vec<uint8_t , 1 >> (row);
177+ for (int col = ins.x ; col < ins.x + ins.w ; col++)
178+ {
179+ if (col < 0 || col >= frame.cols ) continue ;
180+ data_Ptr[col][0 ] = 1 ;
181+ }
182+ }
183+ cv::bitwise_and (img_mask, reg_img, img_mask);
184+
185+ std::vector<cv::Mat> contours;
186+ cv::Mat hierarchy;
187+ cv::Mat colored_img = frame.clone ();
188+ cv::findContours (img_mask, contours, hierarchy,
189+ cv::RETR_CCOMP, cv::CHAIN_APPROX_SIMPLE);
190+ cv::drawContours (colored_img, contours, -1 , class_colors[ins.label ], -1 , cv::LINE_8,
191+ hierarchy, 100 );
192+ frame = 0.4 * colored_img + 0.6 * frame;
193+
179194 auto score = cv::format (" %.3f" , ins.score );
180195 std::string text = class_labels[ins.label ] + " |" + score;
181- cv::putText (frame, text, cv::Point (ins.x - ins.w / 2 , ins.y - ins.h / 2 - 5 ),
182- cv::FONT_HERSHEY_SIMPLEX, 0.7 , class_colors[ins.label ], 2 );
183- cv::Rect rect (ins.x - ins.w / 2 , ins.y - ins.h / 2 , ins.w , ins.h );
196+ cv::Size text_size = cv::getTextSize (text, font_face, font_scale, thickness, nullptr );
197+ cv::Point org;
198+ org.x = ins.x ;
199+ org.y = ins.y + text_size.height + 2 ;
200+ cv::Rect text_back = cv::Rect (org.x , org.y - text_size.height , text_size.width , text_size.height + 5 );
201+ cv::rectangle (frame, text_back, class_colors[ins.label ], -1 );
202+ cv::putText (frame, text, org, font_face, font_scale, cv::Scalar (255 , 255 , 255 ), thickness);
203+ cv::Rect rect (ins.x , ins.y , ins.w , ins.h );
184204 cv::rectangle (frame, rect, class_colors[ins.label ], 2 , cv::LINE_8, 0 );
185205 }
186206 writer.write (frame);
0 commit comments