#ifndef CENTERNETDETECTION_H #define CENTERNETDETECTION_H #include "CenternetDetection.h" #include "opencv2/imgproc/imgproc.hpp" // #include // #include namespace tk { namespace dnn { float __colors[6][3] = { {1,0,1}, {0,0,1},{0,1,1},{0,1,0},{1,1,0},{1,0,0} }; float get_color2(int c, int x, int max) { float ratio = ((float)x/max)*5; int i = floor(ratio); int j = ceil(ratio); ratio -= i; float r = (1-ratio) * __colors[i % 6][c % 3] + ratio*__colors[j % 6][c % 3]; //printf("%f\n", r); return r; } bool CenternetDetection::init(std::string tensor_path) { std::cout<<(tensor_path).c_str()<<"\n"; netRT = new tk::dnn::NetworkRT(NULL, (tensor_path).c_str() ); dim = tk::dnn::dataDim_t(1, 3, 512, 512, 1); const char *coco_class_name_[] = { "person", "bicycle", "car", "motorcycle", "airplane", "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", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball", "kite", "baseball bat", "baseball glove", "skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple", "sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair", "couch", "potted plant", "bed", "dining table", "toilet", "tv", "laptop", "mouse", "remote", "keyboard", "cell phone", "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors", "teddy bear", "hair drier", "toothbrush" }; coco_class_name = std::vector(coco_class_name_, std::end( coco_class_name_ )); src = cv::Mat(cv::Size(2,3), CV_32F); dst = cv::Mat(cv::Size(2,3), CV_32F); dst2 = cv::Mat(cv::Size(2,3), CV_32F); trans = cv::Mat(cv::Size(3,2), CV_32F); trans2 = cv::Mat(cv::Size(3,2), CV_32F); // dets = tk::dnn::Yolo::allocateDetections(tk::dnn::Yolo::MAX_DETECTIONS, classes); checkCuda(cudaMalloc(&input_d, sizeof(dnnType)*netRT->input_dim.tot())); // dim_hm = tk::dnn::dataDim_t(1, 80, 56, 56, 1); // dim_wh = tk::dnn::dataDim_t(1, 2, 56, 56, 1); // dim_reg = tk::dnn::dataDim_t(1, 2, 56, 56, 1); dim_hm = tk::dnn::dataDim_t(1, 80, 128, 128, 1); dim_wh = tk::dnn::dataDim_t(1, 2, 128, 128, 1); dim_reg = tk::dnn::dataDim_t(1, 2, 128, 128, 1); checkCuda( cudaMalloc(&topk_scores, dim_hm.c * K *sizeof(float)) ); checkCuda( cudaMalloc(&topk_inds_, dim_hm.c * K *sizeof(int)) ); checkCuda( cudaMalloc(&topk_ys_, dim_hm.c * K *sizeof(float)) ); checkCuda( cudaMalloc(&topk_xs_, dim_hm.c * K *sizeof(float)) ); checkCuda( cudaMalloc(&ids_d, dim_hm.c * dim_hm.h * dim_hm.w*sizeof(int)) ); checkCuda( cudaMalloc(&ids_2d, dim_hm.c * dim_hm.h * dim_hm.w*sizeof(int)) ); checkCuda( cudaMallocHost(&ids_, dim_hm.c * dim_hm.h * dim_hm.w*sizeof(int)) ); checkCuda( cudaMallocHost(&ids_2, dim_hm.c * dim_hm.h * dim_hm.w*sizeof(int)) ); for(int i =0; iinput_dim.tot())); mean << 0.408, 0.447, 0.47; stddev << 0.289, 0.274, 0.278; #endif checkCuda( cudaMalloc(&d_ptrs, dim.c * dim.h*dim.w * sizeof(float)) ); // mean << 0.408, 0.447, 0.47; // stddev << 0.289, 0.274, 0.278; // Alloc array used in the kernel checkCuda( cudaMalloc(&src_out, K *sizeof(float)) ); checkCuda( cudaMalloc(&ids_out, K *sizeof(int)) ); // checkCuda( cudaFree(src_out) ); // checkCuda( cudaFree(ids_out) ); dst2.at(0,0)=width * 0.5; dst2.at(0,1)=width * 0.5; dst2.at(1,0)=width * 0.5; dst2.at(1,1)=width * 0.5 + width * -0.5; dst2.at(2,0)=dst2.at(1,0) + (-dst2.at(0,1)+dst2.at(1,1) ); dst2.at(2,1)=dst2.at(1,1) + (dst2.at(0,0)-dst2.at(1,0) ); } cv::Mat CenternetDetection::draw(cv::Mat &imageOrig) { tk::dnn::box b; int x0, w, x1, y0, h, y1; int objClass; std::string det_class; int baseline = 0; float fontScale = 0.5; int thickness = 2; for(int c=0; c sz.height){ s[0] = sz.width * 1.0; s[1] = sz.width * 1.0; } else{ s[0] = sz.height * 1.0; s[1] = sz.height * 1.0; } // ----------- get_affine_transform // rot_rad = pi * 0 / 100 --> 0 src.at(0,0)=c[0]; src.at(0,1)=c[1]; src.at(1,0)=c[0]; src.at(1,1)=c[1] + s[0] * -0.5; dst.at(0,0)=inp_width * 0.5; dst.at(0,1)=inp_height * 0.5; dst.at(1,0)=inp_width * 0.5; dst.at(1,1)=inp_height * 0.5 + inp_width * -0.5; src.at(2,0)=src.at(1,0) + (-src.at(0,1)+src.at(1,1) ); src.at(2,1)=src.at(1,1) + (src.at(0,0)-src.at(1,0) ); dst.at(2,0)=dst.at(1,0) + (-dst.at(0,1)+dst.at(1,1) ); dst.at(2,1)=dst.at(1,1) + (dst.at(0,0)-dst.at(1,0) ); trans = cv::getAffineTransform( src, dst ); end_t = std::chrono::steady_clock::now(); std::cout << " TIME gett affine trans: " << std::chrono::duration_cast(end_t - step_t).count() << " us" << std::endl; step_t = end_t; trans2 = cv::getAffineTransform( dst2, src ); end_t = std::chrono::steady_clock::now(); std::cout << " TIME getAffineTrans 2: " << std::chrono::duration_cast(end_t - step_t).count() << " us" << std::endl; step_t = end_t; } sz_old = sz; #ifdef OPENCV_CUDA cv::cuda::GpuMat im_Orig; cv::cuda::GpuMat imageF1_d, imageF2_d; im_Orig = cv::cuda::GpuMat(imageOrig); cv::cuda::resize (im_Orig, imageF1_d, cv::Size(new_width, new_height)); checkCuda( cudaDeviceSynchronize() ); sz = imageF1_d.size(); std::cout<<"size: "<(end_t - step_t).count() << " us" << std::endl; step_t = end_t; cv::cuda::warpAffine(imageF1_d, imageF2_d, trans, cv::Size(inp_width, inp_height), cv::INTER_LINEAR ); checkCuda( cudaDeviceSynchronize() ); imageF2_d.convertTo(imageF1_d, CV_32FC3, 1/255.0); checkCuda( cudaDeviceSynchronize() ); end_t = std::chrono::steady_clock::now(); std::cout << " TIME convert: " << std::chrono::duration_cast(end_t - step_t).count() << " us" << std::endl; step_t = end_t; dim2 = dim; cv::cuda::GpuMat bgr[3]; cv::cuda::split(imageF1_d,bgr);//split source end_t = std::chrono::steady_clock::now(); std::cout << " TIME split: " << std::chrono::duration_cast(end_t - step_t).count() << " us" << std::endl; step_t = end_t; for(int i=0; i(end_t - step_t).count() << " us" << std::endl; step_t = end_t; checkCuda(cudaMemcpy(input_d, d_ptrs, dim2.tot()*sizeof(dnnType), cudaMemcpyDeviceToDevice)); end_t = std::chrono::steady_clock::now(); std::cout << " TIME Memcpy to input_d: " << std::chrono::duration_cast(end_t - step_t).count() << " us" << std::endl; step_t = end_t; #else cv::Mat imageF; resize(imageOrig, imageF, cv::Size(new_width, new_height)); sz = imageF.size(); std::cout<<"size: "<(end_t - step_t).count() << " us" << std::endl; step_t = end_t; cv::Mat trans = cv::getAffineTransform( src, dst ); cv::warpAffine(imageF, imageF, trans, cv::Size(inp_width, inp_height), cv::INTER_LINEAR ); end_t = std::chrono::steady_clock::now(); std::cout << " TIME warpAffine: " << std::chrono::duration_cast(end_t - step_t).count() << " us" << std::endl; step_t = end_t; sz = imageF.size(); std::cout<<"size: "<(end_t - step_t).count() << " us" << std::endl; step_t = end_t; dim2 = dim; //split channels cv::Mat bgr[3]; cv::split(imageF,bgr);//split source for(int i=0; i<3; i++){ bgr[i] = bgr[i] - mean[i]; bgr[i] = bgr[i] / stddev[i]; } //write channels for(int i=0; i