Skip to content

Commit

Permalink
add visual result style
Browse files Browse the repository at this point in the history
  • Loading branch information
FagangJin committed Nov 15, 2019
1 parent 6833a7e commit 3285cd0
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
39 changes: 39 additions & 0 deletions README.md
Expand Up @@ -41,6 +41,45 @@ b). If you need full capacity which thor does, including `vis`, `geometry`, `dat

- **2050.01.01**: to be continue..

- **2019.11.16**: We demonstrate how to using `thor::vis` to draw detections in your algorithm:

![](https://s2.ax1x.com/2019/11/15/Mdig8e.png)

```c++
#include "thor/dl.h"
#include "thor/os.h"
#include "thor/structures.h"
#include "thor/vis.h"

using namespace std;
using namespace cv;
using namespace thor::vis;
using namespace thor::dl;

int main() {
vector<thor::Box> all_detections;
for (int i = 0; i < num_det; i++) {
// Show results over confidence threshold
if (scores[i] >= 0.33f) {
float x1 = boxes[i * 4 + 0] * scale_x;
float y1 = boxes[i * 4 + 1] * scale_y;
float x2 = boxes[i * 4 + 2] * scale_x;
float y2 = boxes[i * 4 + 3] * scale_y;
thor::Box one_box{x1, y1, x2, y2, thor::BoxFormat::XYXY};
one_box.score = scores[i];
one_box.idx = classes[i] + 1;
all_detections.emplace_back(one_box);
}
}

// draw
auto res_image = thor::vis::VisualizeDetectionStyleDetectron2(
image, all_detections, COCO_CLASSES);
}
```

Above is a simple example using `thor::vis::VisualizeDetectionStyleDetectron2` draw bounding boxes using detectron2 style. From our experiences, these steps time cost is about **0.0001**s, so it's doesn't matter if you generate your box format to `thor::Box` format first and then call our drawing method.

- **2019.09.24**: I just notice thor has been recommended by 爱可可老师 through weibo, here is link: https://weibo.com/1402400261/I8p1gnIkK , check it out!!! If you like this project, pls give a star!!

- **2019.08.29**: A new header-only args parser lib has been added into thor. Original implementation from [here](https://github.com/Taywee/args). We did some changes than original, roughly usage can be used like this:
Expand Down
5 changes: 5 additions & 0 deletions include/vis.h
Expand Up @@ -69,6 +69,11 @@ cv::Mat VisualizeDetection(cv::Mat &img, vector<thor::Box> detections,
bool enable_mask = true,
float confidence_threshold = 0.02,
bool normalized = false);
cv::Mat VisualizeDetectionStyleDetectron2(cv::Mat &img, vector<thor::Box> detections,
vector<string> classes_names,
bool enable_mask = true,
float confidence_threshold = 0.02,
bool normalized = false);

// adding render HumanPose on image
void renderHumanPose(std::vector<HumanPose> &poses, cv::Mat &image);
Expand Down
52 changes: 52 additions & 0 deletions src/vis.cpp
Expand Up @@ -415,6 +415,58 @@ cv::Mat VisualizeDetection(cv::Mat &img, vector<thor::Box> detections, vector<st
// maybe combine a mask img back later
return combined;
}

cv::Mat VisualizeDetectionStyleDetectron2(cv::Mat &img, vector<thor::Box> detections, vector<string> classes_names, bool enable_mask, float confidence_threshold, bool normalized){
// for detectron2 style drawing bounding boxes
const int font = cv::FONT_HERSHEY_SIMPLEX;
const float font_scale = 0.35;
const int font_thickness = 1;
cv::Mat mask = cv::Mat::zeros(img.size(), CV_8UC3);
for (int i = 0; i < detections.size(); ++i)
{
thor::Box box = detections[i];
box.to_xyxy();
const float score = box.score;
if (score >= confidence_threshold)
{
cv::Point pt1, pt2;
if (normalized)
{
pt1.x = (img.cols * box.xmin);
pt1.y = (img.rows * box.ymin);
pt2.x = (img.cols * box.xmax);
pt2.y = (img.rows * box.ymax);
}
else
{
pt1.x = box.xmin;
pt1.y = box.ymin;
pt2.x = box.xmax;
pt2.y = box.ymax;
}

cv::Scalar u_c = thor::vis::gen_unique_color_cv(box.idx);
cv::rectangle(img, pt1, pt2, u_c, 1, 8, 0);
cv::rectangle(mask, pt1, pt2, u_c, cv::FILLED, 0);

char score_str[256];
sprintf(score_str, "%.1f", score);
std::string label_text = classes_names[box.idx] + ":" + string(score_str);
int base_line = 0;
cv::Point text_origin = cv::Point(pt1.x, pt1.y-2);
cv::Size text_size = cv::getTextSize(label_text, font, font_scale, font_thickness, &base_line);
cv::rectangle(img, cv::Point(text_origin.x, text_origin.y),
cv::Point(text_origin.x + text_size.width, text_origin.y - text_size.height),
cv::Scalar(0, 0, 0), -1, 0);
cv::putText(img, label_text, text_origin, font, font_scale, cv::Scalar(255, 255, 255), font_thickness);
}
}
cv::Mat combined;
cv::addWeighted(img, 0.8, mask, 0.6, 0.6, combined);
// maybe combine a mask img back later
return combined;
};

#endif

} // namespace vis
Expand Down

0 comments on commit 3285cd0

Please sign in to comment.