Last active
January 23, 2022 13:30
-
-
Save doleron/ebe3ff54a41fa129ce3261a3c8b94e22 to your computer and use it in GitHub Desktop.
C++ unrwapping predictions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct Detection | |
| { | |
| int class_id; | |
| float confidence; | |
| cv::Rect box; | |
| }; | |
| void detect(const cv::Mat &input_image, constcv::Mat &output, std::vector<Detection> &output) { | |
| float x_factor = input_image.cols / 640.; | |
| float y_factor = input_image.rows / 640.; | |
| float *data = (float *)outputs[0].data; | |
| const int dimensions = 85; | |
| const int rows = 25200; | |
| std::vector<int> class_ids; | |
| std::vector<float> confidences; | |
| std::vector<cv::Rect> boxes; | |
| for (int i = 0; i < rows; ++i) { | |
| float confidence = data[4]; | |
| if (confidence >= .4) { | |
| float * classes_scores = data + 5; | |
| cv::Mat scores(1, className.size(), CV_32FC1, classes_scores); | |
| cv::Point class_id; | |
| double max_class_score; | |
| minMaxLoc(scores, 0, &max_class_score, 0, &class_id); | |
| if (max_class_score > SCORE_THRESHOLD) { | |
| confidences.push_back(confidence); | |
| class_ids.push_back(class_id.x); | |
| float x = data[0]; | |
| float y = data[1]; | |
| float w = data[2]; | |
| float h = data[3]; | |
| int left = int((x - 0.5 * w) * x_factor); | |
| int top = int((y - 0.5 * h) * y_factor); | |
| int width = int(w * x_factor); | |
| int height = int(h * y_factor); | |
| boxes.push_back(cv::Rect(left, top, width, height)); | |
| } | |
| } | |
| data += 85; | |
| } | |
| std::vector<int> nms_result; | |
| cv::dnn::NMSBoxes(boxes, confidences, SCORE_THRESHOLD, NMS_THRESHOLD, nms_result); | |
| for (int i = 0; i < nms_result.size(); i++) { | |
| int idx = nms_result[i]; | |
| Detection result; | |
| result.class_id = class_ids[idx]; | |
| result.confidence = confidences[idx]; | |
| result.box = boxes[idx]; | |
| output.push_back(result); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment