Modify map demo, using abstract class. Move draw function in abstract class

Signed-off-by: Micaela Verucchi <micaelaverucchi@gmail.com>
This commit is contained in:
Micaela Verucchi
2020-03-23 19:32:37 +01:00
parent bbcc33c0cf
commit df37e11709
10 changed files with 85 additions and 150 deletions
+4 -35
View File
@@ -131,7 +131,7 @@ void CenternetDetection::preprocess(cv::Mat &frame)
// auto step_t = std::chrono::steady_clock::now();
// auto end_t = std::chrono::steady_clock::now();
cv::Size sz = originalSize;
std::cout<<"image: "<<sz.width<<", "<<sz.height<<std::endl;
// std::cout<<"image: "<<sz.width<<", "<<sz.height<<std::endl;
cv::Size sz_old;
float scale = 1.0;
float new_height = sz.height * scale;
@@ -186,7 +186,7 @@ void CenternetDetection::preprocess(cv::Mat &frame)
checkCuda( cudaDeviceSynchronize() );
sz = imageF1_d.size();
std::cout<<"size: "<<sz.height<<" "<<sz.width<<" - "<<std::endl;
// std::cout<<"size: "<<sz.height<<" "<<sz.width<<" - "<<std::endl;
// end_t = std::chrono::steady_clock::now();
// std::cout << " TIME resize: " << std::chrono::duration_cast<std::chrono:: microseconds>(end_t - step_t).count() << " us" << std::endl;
// step_t = end_t;
@@ -226,7 +226,7 @@ void CenternetDetection::preprocess(cv::Mat &frame)
cv::Mat imageF;
resize(frame, imageF, cv::Size(new_width, new_height));
sz = imageF.size();
std::cout<<"size: "<<sz.height<<" "<<sz.width<<" - "<<std::endl;
// std::cout<<"size: "<<sz.height<<" "<<sz.width<<" - "<<std::endl;
// end_t = std::chrono::steady_clock::now();
// std::cout << " TIME resize: " << std::chrono::duration_cast<std::chrono:: microseconds>(end_t - step_t).count() << " us" << std::endl;
// step_t = end_t;
@@ -238,7 +238,7 @@ void CenternetDetection::preprocess(cv::Mat &frame)
// step_t = end_t;
sz = imageF.size();
std::cout<<"size: "<<sz.height<<" "<<sz.width<<" - "<<std::endl;
// std::cout<<"size: "<<sz.height<<" "<<sz.width<<" - "<<std::endl;
imageF.convertTo(imageF, CV_32FC3, 1/255.0);
// end_t = std::chrono::steady_clock::now();
// std::cout << " TIME convertto: " << std::chrono::duration_cast<std::chrono:: microseconds>(end_t - step_t).count() << " us" << std::endl;
@@ -425,37 +425,6 @@ void CenternetDetection::postprocess(dnnType **rt_out, const int n_out)
// step_t = end_t;
}
cv::Mat CenternetDetection::draw(cv::Mat &frame)
{
tk::dnn::box b;
int x0, w, x1, y0, h, y1;
int objClass;
std::string det_class;
int baseline = 0;
float font_scale = 0.5;
int thickness = 2;
int num_detected = detected.size();
for (int i = 0; i < num_detected; i++){
b = detected[i];
x0 = b.x;
w = b.w;
x1 = b.x + w;
y0 = b.y;
h = b.h;
y1 = b.y + h;
objClass = b.cl;
det_class = classesNames[objClass];
cv::rectangle(frame, cv::Point(x0, y0), cv::Point(x1, y1), colors[objClass], 2);
// draw label
cv::Size textSize = getTextSize(det_class, cv::FONT_HERSHEY_SIMPLEX, font_scale, thickness, &baseline);
cv::rectangle(frame, cv::Point(x0, y0), cv::Point((x0 + textSize.width - 2), (y0 - textSize.height - 2)), colors[b.cl], -1);
cv::putText(frame, det_class, cv::Point(x0, (y0 - (baseline / 2))), cv::FONT_HERSHEY_SIMPLEX, font_scale, cv::Scalar(255, 255, 255), thickness);
}
return frame;
}
}}
+12 -31
View File
@@ -131,8 +131,7 @@ float MobilenetDetection::iou(const tk::dnn::box &a, const tk::dnn::box &b)
bool MobilenetDetection::init(const std::string& tensor_path, const int n_classes)
{
std::cout<<"MobilenetDetection Init"<<std::endl;
std::cout<<(tensor_path).c_str()<<"\n";
netRT = new tk::dnn::NetworkRT(NULL, (tensor_path).c_str());
imageSize = netRT->input_dim.h;
classes = n_classes;
@@ -179,7 +178,7 @@ bool MobilenetDetection::init(const std::string& tensor_path, const int n_classe
if(classes == 21){
const char *classes_names_[] = {
"BACKGROUND", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus",
"aeroplane", "bicycle", "bird", "boat", "bottle", "bus",
"car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike",
"person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"};
classesNames = std::vector<std::string>(classes_names_, std::end(classes_names_));
@@ -187,7 +186,7 @@ bool MobilenetDetection::init(const std::string& tensor_path, const int n_classe
}
else if (classes == 81){
const char *classes_names_[] = {
"BACKGROUND", "person" , "bicycle" , "car" , "motorbike" , "aeroplane" , "bus" ,
"person" , "bicycle" , "car" , "motorbike" , "aeroplane" , "bus" ,
"train" , "truck" , "boat" , "traffic light" , "fire hydrant" , "stop sign" ,
"parking meter" , "bench" , "bird" , "cat" , "dog" , "horse" , "sheep" , "cow" ,
"elephant" , "bear" , "zebra" , "giraffe" , "backpack" , "umbrella" , "handbag" ,
@@ -210,7 +209,6 @@ bool MobilenetDetection::init(const std::string& tensor_path, const int n_classe
void MobilenetDetection::preprocess(cv::Mat &frame)
{
std::cout<<"preprocess"<<std::endl;
#ifdef OPENCV_CUDA
//move original image on GPU
cv::cuda::GpuMat orig_img, frame_nomean;
@@ -248,8 +246,10 @@ void MobilenetDetection::preprocess(cv::Mat &frame)
void MobilenetDetection::update(cv::Mat &frame)
{
TIMER_START
detected.clear();
if(!frame.data) {
std::cout<<"MOBILENET: NO IMAGE DATA\n";
return;
}
originalSize = frame.size();
//preprocess
@@ -271,6 +271,7 @@ void MobilenetDetection::update(cv::Mat &frame)
rt_out[0] = (dnnType *)netRT->buffersRT[3];
rt_out[1] = (dnnType *)netRT->buffersRT[4];
detected.clear();
//postprocess
postprocess(rt_out, 2);
@@ -313,12 +314,12 @@ void MobilenetDetection::postprocess(dnnType **rt_out, const int n_out)
remaining.clear();
tk::dnn::box b;
b.cl = boxes[0].cl;
b.cl = boxes[0].cl -1 ; //remove background class
b.prob = boxes[0].prob;
b.x = boxes[0].x * width;
b.x = boxes[0].x * width;
b.y = boxes[0].y * height;
b.w = boxes[0].w * width;
b.h = boxes[0].h * height;
b.w = boxes[0].w * width - b.x; //convert from x1 to width
b.h = boxes[0].h * height - b.y; //convert from y1 to height
detected.push_back(b);
for (size_t j = 1; j < boxes.size(); j++){
if (iou(boxes[0], boxes[j]) <= IoUThreshold){
@@ -331,25 +332,5 @@ void MobilenetDetection::postprocess(dnnType **rt_out, const int n_out)
}
cv::Mat MobilenetDetection::draw(cv::Mat &frame)
{
int baseline = 0;
float font_scale = 0.5;
int thickness = 2;
tk::dnn::box b;
for (size_t i = 0; i < detected.size(); i++){
b = detected[i];
std::string det_class = classesNames[b.cl];
cv::rectangle(frame, cv::Point(b.x, b.y), cv::Point(b.w, b.h), colors[b.cl], 2);
// draw label
cv::Size text_size = getTextSize(det_class, cv::FONT_HERSHEY_SIMPLEX, font_scale, thickness, &baseline);
cv::rectangle(frame, cv::Point(b.x, b.y), cv::Point((b.x + text_size.width - 2), (b.y - text_size.height - 2)), colors[b.cl], -1);
cv::putText(frame, det_class, cv::Point(b.x, (b.y - (baseline / 2))), cv::FONT_HERSHEY_SIMPLEX, font_scale, cv::Scalar(255, 255, 255), thickness);
}
return frame;
}
} // namespace dnn
} // namespace tk
+2 -32
View File
@@ -43,6 +43,8 @@ bool Yolo3Detection::init(const std::string& tensor_path, const int n_classes) {
float b = getColor(0, offset, classes);
colors[c] = cv::Scalar(int(255.0*b), int(255.0*g), int(255.0*r));
}
classesNames = getYoloLayer()->classesNames;
return true;
}
@@ -60,7 +62,6 @@ void Yolo3Detection::preprocess(cv::Mat &frame)
//write channels
for(int i=0; i<netRT->input_dim.c; i++) {
std::cout<<"copio il channel"<<i<<std::endl;
int idx = i*imagePreproc.rows*imagePreproc.cols;
int ch = netRT->input_dim.c-1 -i;
checkCuda( cudaMemcpy((void*)&input_d[idx], (void*)bgr[ch].data, imagePreproc.rows*imagePreproc.cols*sizeof(dnnType), cudaMemcpyDeviceToDevice));
@@ -122,8 +123,6 @@ void Yolo3Detection::postprocess(dnnType **rt_out, const int n_out)
float x_ratio = float(originalSize.width) / float(netRT->input_dim.w);
float y_ratio = float(originalSize.height) / float(netRT->input_dim.h);
std::cout<<"RATIO:"<<x_ratio<<" "<<y_ratio<<std::endl;
// compute dets
nDets = 0;
for(int i=0; i<n_out; i++) {
@@ -166,37 +165,8 @@ void Yolo3Detection::postprocess(dnnType **rt_out, const int n_out)
detected.push_back(res);
}
}
std::cout<<"N detections: "<<detected.size()<<std::endl;
}
cv::Mat Yolo3Detection::draw(cv::Mat &frame)
{
tk::dnn::box b;
int x0, w, x1, y0, h, y1;
int objClass;
std::string det_class;
int baseline = 0;
float font_scale = 0.5;
int thickness = 2;
// draw dets
for(int i=0; i<detected.size(); i++) {
b = detected[i];
x0 = b.x;
x1 = b.x + b.w;
y0 = b.y;
y1 = b.y + b.h;
det_class = getYoloLayer()->classesNames[b.cl];
// draw rectangle
cv::rectangle(frame, cv::Point(x0, y0), cv::Point(x1, y1), colors[b.cl], 2);
// draw label
cv::Size text_size = getTextSize(det_class, cv::FONT_HERSHEY_SIMPLEX, font_scale, thickness, &baseline);
cv::rectangle(frame, cv::Point(x0, y0), cv::Point((x0 + text_size.width - 2), (y0 - text_size.height - 2)), colors[b.cl], -1);
cv::putText(frame, det_class, cv::Point(x0, (y0 - (baseline / 2))), cv::FONT_HERSHEY_SIMPLEX, font_scale, cv::Scalar(255, 255, 255), thickness);
}
return frame;
}
tk::dnn::Yolo* Yolo3Detection::getYoloLayer(int n)
{