Refactoring and modularization

Signed-off-by: Micaela Verucchi <micaela.verucchi@unimore.it>
This commit is contained in:
Micaela Verucchi
2019-10-04 11:12:01 +02:00
parent bb7d382d96
commit 35787cc771
25 changed files with 1708 additions and 1560 deletions
+9 -2
View File
@@ -38,12 +38,20 @@ cuda_add_library(kernels SHARED ${tkdnn_CUSRC})
file(GLOB tkdnn_SRC "src/*.cpp")
set(tkdnn_LIBS kernels ${CUDA_LIBRARIES} ${CUDA_CUBLAS_LIBRARIES} -lcudnn -lnvinfer ${OpenCV_LIBS} -lgdal)
file(GLOB class_SRC "src/class_src/*.cpp")
set(class_LIBS ${OpenCV_LIBS} -lgdal yaml-cpp python2.7)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++11")
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include ${CUDA_INCLUDE_DIRS} ${OPENCV_INCLUDE_DIRS} ${NVINFER_INCLUDES} "~/repos/cereal/include" ${CMAKE_CURRENT_SOURCE_DIR}/tracker_CLASS/c++/src)
include_directories( BEFORE ${MY_SOURCE_DIR}/src /usr/include/python2.7 )
add_library(tkDNN SHARED ${tkdnn_SRC})
target_link_libraries(tkDNN ${tkdnn_LIBS})
add_library(CLASS SHARED ${class_SRC})
target_link_libraries(CLASS ${class_LIBS})
#static
#add_library(tkDNN_static STATIC ${tkdnn_SRC})
#target_link_libraries(tkDNN_static ${tkdnn_LIBS})
@@ -104,8 +112,7 @@ add_executable(yolo3_demo demo/demo/demo.cpp
tracker_CLASS/c++/src/tracker.cpp )
target_link_libraries(yolo3_demo tkDNN)
target_link_libraries(yolo3_demo python2.7 yaml-cpp)
target_link_libraries(yolo3_demo tkDNN CLASS)
+20 -457
View File
@@ -1,89 +1,34 @@
#include <iostream>
#include <signal.h>
#include <stdlib.h> /* srand, rand */
#include <unistd.h>
#include <mutex>
#include <ctime>
#include <pthread.h>
#include <time.h>
#include <chrono>
#include <math.h>
#include <typeinfo>
#include "utils.h"
#include "BoxDetection.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
//saliency
#include <opencv2/core/utility.hpp>
#include <opencv2/saliency.hpp>
#include <opencv2/highgui.hpp>
#include "Yolo3Detection.h"
#include "classutils.h"
#include "message.h"
#include "visualization.h"
#include "tracker.h"
#include "../masa_protocol/include/send.hpp"
#include "../masa_protocol/include/serialize.hpp"
#include "ekf.h"
#include "trackutils.h"
#include "plot.h"
#include "tracker.h"
#include <assert.h>
// #include <assert.h>
// #include <unistd.h>
// #include <mutex>
// #include <ctime>
// #include <pthread.h>
// #include <signal.h>
// #include <chrono>
// #include <math.h>
// #include <typeinfo>
// #include <iostream>
#define MAX_DETECT_SIZE 100
std::chrono::steady_clock::time_point local_clock_start;
std::mutex mutexgRun;
bool gRun;
std::chrono::steady_clock::time_point local_clock_start;
std::mutex mutexgRun;
std::string obj_class[10]{"person", "car", "truck", "bus", "motor", "bike", "rider", "traffic light", "traffic sign", "train"};
//mutex for some opencv operations
std::mutex mutex_cv;
struct ModFrame_t{
std::vector<Tracker> trackers;
geodetic_converter::GeodeticConverter gc;
double adfGeoTransform[6];
cv::Mat H;
cv::Mat original_frame;
tk::dnn::Yolo3Detection yolo;
cv::Mat mask;
// sem for mainthread, detectionthread and topviewthread
std::mutex sem;
};
struct Frame_t{
char *input;
cv::Mat frame;
int frame_nbr;
// sem_vc for mainthread, videocapturethread, originalthread and disparitythread
std::mutex sem_vc;
};
struct Camera_t{
int CAM_IDX;
char *input;
char *pmatrix;
char *maskfile;
char *cameraCalib;
char *maskFileOrient;
bool to_show;
tk::dnn::Yolo3Detection yolo;
double adfGeoTransform[6];
};
struct Show_t{
cv::Mat original, detection, topview, disparity;
bool update_o, update_de, update_t, update_di;
// a single mutex for each operation - the show_updates function must get all mutex
std::mutex mutex_o, mutex_de, mutex_t, mutex_di;
}updates;
Show_t updates;
void sig_handler(int signo)
{
@@ -113,7 +58,6 @@ void *readVideoCapture(void *x_void_ptr)
else
std::cout << "camera started\n";
// cap.set(cv::CAP_PROP_BUFFERSIZE,3);
// std::cout<<"buf size: "<<cap.get(CV_CAP_PROP_BUFFERSIZE)<<std::endl;
auto start_t = std::chrono::steady_clock::now();
@@ -190,7 +134,6 @@ void *readVideoCapture(void *x_void_ptr)
// CAP_PROP_FRAME_COUNT Number of frames in the video file.
std::cout << "id: " << cap.get(cv::CAP_PROP_FRAME_COUNT) << std::endl;
if (!frame_loc.data)
{
usleep(1000000);
@@ -218,384 +161,6 @@ void *readVideoCapture(void *x_void_ptr)
return (void *)0;
}
/* Thread function to show the updated images
**/
void *show_updates(void *x_void_ptr)
{
cv::namedWindow("original", cv::WINDOW_NORMAL);
cv::namedWindow("detection", cv::WINDOW_NORMAL);
cv::namedWindow("topview", cv::WINDOW_NORMAL);
cv::namedWindow("disparity", cv::WINDOW_NORMAL);
cv::Mat original_loc, detection_loc, topview_loc, disparity_loc;
bool update_o_loc, update_de_loc, update_t_loc, update_di_loc;
while (gRun)
{
TIMER_START
// critical section: copy the struct in local variable
// in this way we can unlock the sem for the main thread
if(updates.mutex_o.try_lock())
{
update_o_loc = updates.update_o;
updates.update_o = false;
if(update_o_loc)
original_loc = updates.original.clone();
updates.mutex_o.unlock();
}
if(updates.mutex_de.try_lock())
{
update_de_loc = updates.update_de;
updates.update_de = false;
if(update_de_loc)
detection_loc = updates.detection.clone();
updates.mutex_de.unlock();
}
if(updates.mutex_t.try_lock())
{
update_t_loc = updates.update_t;
updates.update_t = false;
if(update_t_loc)
topview_loc = updates.topview.clone();
updates.mutex_t.unlock();
}
if(updates.mutex_di.try_lock())
{
update_di_loc = updates.update_di;
updates.update_di = false;
if(update_di_loc)
disparity_loc = updates.disparity.clone();
updates.mutex_di.unlock();
}
if(update_o_loc)
cv::imshow("original", original_loc);
if(update_de_loc)
cv::imshow("detection", detection_loc);
if(update_t_loc)
cv::imshow("topview", topview_loc);
if(update_di_loc)
cv::imshow("disparity", disparity_loc);
cv::waitKey(1);
// usleep(20000); //sleep 20 msec
std::cout<<"show_updates: ";
TIMER_STOP
}
return (void *)0;
}
void *originalFrame(void *x_void_ptr)
{
Frame_t *info_show_orig = (Frame_t *) x_void_ptr;
cv::Mat frame_loc;
int frame_nbr_loc = 0;
while (gRun)
{
TIMER_START
// critical section: copy the struct in local variable
// in this way we can unlock the sem for the main thread
info_show_orig->sem_vc.lock();
frame_loc = info_show_orig->frame.clone();
frame_nbr_loc = info_show_orig->frame_nbr;
info_show_orig->sem_vc.unlock();
if (frame_nbr_loc == 0)
{
usleep(1000000);
printf("no frame received\n");
continue;
}
updates.mutex_o.lock();
updates.original = frame_loc.clone();
updates.update_o = true;
updates.mutex_o.unlock();
usleep(10000); //sleep 10 msec
std::cout<<"originalFrame: ";
TIMER_STOP
}
return (void *)0;
}
void *detectionFrame(void *x_void_ptr)
{
ModFrame_t *info_show = (ModFrame_t *) x_void_ptr;
double lat, lon, alt;
int pix_x, pix_y;
cv::Mat original_frame_loc;
std::vector<Tracker> trackers;
geodetic_converter::GeodeticConverter gc;
double adfGeoTransform[6];
cv::Mat H;
tk::dnn::Yolo3Detection yolo;
int num_detected;
cv::Mat mask;
// box variable
tk::dnn::box b;
int x0, w, x1, y0, h, y1;
int objClass;
std::string det_class;;
// float prob;
cv::Scalar intensity;
std::vector<cv::Point2f> map_p, camera_p;
int baseline = 0;
float fontScale = 0.5;
int thickness = 2;
while (gRun)
{
TIMER_START
// critical section: copy the struct in local variable
// in this way we can unlock the sem for the main thread
info_show->sem.lock();
original_frame_loc = info_show->original_frame.clone();
// std::vector<Tracker> trackers;
trackers = info_show->trackers;
// geodetic_converter::GeodeticConverter gc;
gc = info_show->gc;
for(int i = 0; i < 6; i++ )
adfGeoTransform[i] = info_show->adfGeoTransform[i];
// cv::Mat H;
H = info_show->H.clone();
yolo = info_show->yolo;
mask = info_show->mask.clone();
info_show->sem.unlock();
if (trackers.empty())
{
usleep(1000000);
printf("no data available\n");
continue;
}
num_detected = yolo.detected.size();
for (int i = 0; i < num_detected; i++)
{
b = yolo.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 = obj_class[b.cl];
// prob = b.prob;
intensity = mask.at<uchar>(cv::Point(int(x0 + b.w / 2), y1));
if (intensity[0] && objClass < 6)
{
//std::cout<<objClass<<" ("<<prob<<"): "<<x0<<" "<<y0<<" "<<x1<<" "<<y1<<"\n";
cv::rectangle(original_frame_loc, cv::Point(x0, y0), cv::Point(x1, y1), yolo.colors[objClass], 2);
// draw label
cv::Size textSize = getTextSize(det_class, cv::FONT_HERSHEY_SIMPLEX, fontScale, thickness, &baseline);
cv::rectangle(original_frame_loc, cv::Point(x0, y0), cv::Point((x0 + textSize.width - 2), (y0 - textSize.height - 2)), yolo.colors[b.cl], -1);
cv::putText(original_frame_loc, det_class, cv::Point(x0, (y0 - (baseline / 2))), cv::FONT_HERSHEY_SIMPLEX, fontScale, cv::Scalar(255, 255, 255), thickness);
}
}
for (auto t : trackers)
{
for (size_t p = 1; p < t.pred_list_.size(); p++)
{
gc.enu2Geodetic(t.pred_list_[p].x_, t.pred_list_[p].y_, 0, &lat, &lon, &alt);
coord2pixel(lat, lon, pix_x, pix_y, adfGeoTransform);
map_p.clear();
camera_p.clear();
map_p.push_back(cv::Point2f(pix_x, pix_y));
//transform camera pixel to map pixel
cv::perspectiveTransform(map_p, camera_p, H.inv());
// std::cout<<"x,y: "<<pix_x<<", "<<pix_y<<std::endl;
// std::cout<<"map_p: "<<map_p<<std::endl;
// std::cout<<"camera_p: "<<camera_p<<std::endl;
// std::cout<<"size original_frame_loc: "<<original_frame_loc.cols<<", "<<original_frame_loc.rows<<std::endl;
// assert (camera_p[0].x < original_frame_loc.cols);
// assert (camera_p[0].y < original_frame_loc.rows);
if (camera_p[0].x < original_frame_loc.cols && camera_p[0].y < original_frame_loc.rows && camera_p[0].x >= 0 && camera_p[0].y >= 0)
cv::circle(original_frame_loc, cv::Point(camera_p[0].x, camera_p[0].y), 3.0, cv::Scalar(t.r_, t.g_, t.b_), CV_FILLED, 8, 0);
}
}
updates.mutex_de.lock();
updates.detection = original_frame_loc.clone();
updates.update_de = true;
updates.mutex_de.unlock();
std::cout<<"detectionFrame: ";
TIMER_STOP
}
return (void *)0;
}
void *topviewFrame(void *x_void_ptr)
{
ModFrame_t *info_show = (ModFrame_t *) x_void_ptr;
double lat, lon, alt;
int pix_x, pix_y;
cv::Mat frame_top;
cv::Mat original_frame_top;
// original_frame_top = cv::imread("../demo/demo/data/map/map_geo.jpg");
original_frame_top = cv::imread("../demo/demo/data/map/MASA_4670.png");
// original_frame_top = cv::imread("../demo/demo/data/map/MASA_4670_V.png");
std::vector<Tracker> trackers;
geodetic_converter::GeodeticConverter gc;
double adfGeoTransform[6];
cv::Mat H;
while (gRun)
{
TIMER_START
// critical section: copy the struct in local variable
// in this way we can unlock the sem for the main thread
info_show->sem.lock();
// std::vector<Tracker> trackers;
trackers = info_show->trackers;
// geodetic_converter::GeodeticConverter gc;
gc = info_show->gc;
for(int i = 0; i < 6; i++ )
adfGeoTransform[i] = info_show->adfGeoTransform[i];
// cv::Mat H;
H = info_show->H.clone();
info_show->sem.unlock();
if (trackers.empty())
{
usleep(1000000);
printf("no data available\n");
continue;
}
frame_top = original_frame_top.clone();
for (auto t : trackers)
{
for (size_t p = 1; p < t.pred_list_.size(); p++)
{
gc.enu2Geodetic(t.pred_list_[p].x_, t.pred_list_[p].y_, 0, &lat, &lon, &alt);
coord2pixel(lat, lon, pix_x, pix_y, adfGeoTransform);
if (pix_x < frame_top.cols && pix_y < frame_top.rows && pix_x >= 0 && pix_y >= 0)
cv::circle(frame_top, cv::Point(pix_x, pix_y), 7.0, cv::Scalar(t.r_, t.g_, t.b_), CV_FILLED, 8, 0);
}
}
//outputVideo<< frame_top;
// ------------------------------------------------
updates.mutex_t.lock();
updates.topview = frame_top.clone();
updates.update_t = true;
updates.mutex_t.unlock();
std::cout<<"topviewFrame: ";
TIMER_STOP
}
return (void *)0;
}
void *disparityFrame(void *x_void_ptr)
{
Frame_t *info_show_disparity = (Frame_t *) x_void_ptr;
bool first_iteration = true;
cv::Mat frame_loc;
int frame_nbr_loc = 0, pre_frame_nbr_loc = 0;
auto start_t = std::chrono::steady_clock::now();
auto step_t = std::chrono::steady_clock::now();
auto end_t = std::chrono::steady_clock::now();
// information for the disparity map
cv::Mat canny, pre_canny, canny_RGB, pre_canny_RGB;
cv::Mat canny_img;
cv::Mat disparity_frame;
while (gRun)
{
start_t = std::chrono::steady_clock::now();
step_t = start_t;
// critical section: copy the struct in local variable
// in this way we can unlock the sem for the main thread
info_show_disparity->sem_vc.lock();
frame_loc = info_show_disparity->frame.clone();
frame_nbr_loc = info_show_disparity->frame_nbr;
info_show_disparity->sem_vc.unlock();
if (frame_nbr_loc == 0)
{
usleep(1000000);
printf("no frame received\n");
continue;
}
// compute frame disparity only in there is a new frame
if(frame_nbr_loc - pre_frame_nbr_loc > 0)
{
pre_frame_nbr_loc = frame_nbr_loc;
//preprocessing frame
step_t = std::chrono::steady_clock::now();
// src_gray
canny_img = img_laplacian(frame_loc,0);
cv::Canny(canny_img, canny, 100, 100*2 );
// sprintf(buf_frame_crop_name,"../demo/demo/data/img_disparity/%d_%d_canny.jpg",frame_nbr_loc, 999);
// cv::imwrite(buf_frame_crop_name, canny);
end_t = std::chrono::steady_clock::now();
std::cout << " TIME END pre canny : "<<std::chrono::duration_cast<std::chrono::milliseconds>(end_t - step_t).count() << " ms"<<std::endl;
step_t = end_t;
// std::cout<<"o: "<<frame_loc.cols<<" - "<<frame_loc.rows<<std::endl;
// std::cout<<"canny: "<<canny.cols<<" - "<<canny.rows<<std::endl;
// std::cout<<"pre: "<<pre_canny.cols<<" - "<<pre_canny.rows<<std::endl;
if(!first_iteration)
{
// backtorgb = cv::cvtColor(pre_canny,cv::COLOR_GRAY2RGB)
cv::cvtColor(pre_canny, pre_canny_RGB, CV_GRAY2RGB);
cv::cvtColor(canny, canny_RGB, CV_GRAY2RGB);
disparity_frame = frame_disparity(pre_canny_RGB, canny_RGB, frame_nbr_loc, 999, 0);
// std::cout<<"size: "<<disparity_frame.rows<<" - "<<disparity_frame.cols<<std::endl;
// if (disparity_frame.rows == 0 || disparity_frame.cols == 0)
// return -1;
// if (disparity_frame.empty())
// { // only fools don't check...
// std::cout << "image not loaded !" << std::endl;
// return -1;
// }
end_t = std::chrono::steady_clock::now();
std::cout << " TIME canny : frame_disparity : "<<std::chrono::duration_cast<std::chrono::milliseconds>(end_t - step_t).count() << " ms"<<std::endl;
step_t = end_t;
// //--------------------------------
// //frame box disparity on the original image
// step_t_segmentation = std::chrono::steady_clock::now();
// frame_box_disparity(pre_frame, frame, pre_rois, frame_nbr_loc);
// // reset pre_rois for the new roi of the current frame
// // pre_rois.erase(pre_rois.begin(), pre_rois.end());
// end_t_segmentation = std::chrono::steady_clock::now();
// std::cout << " TIME Frame disparity : "<<std::chrono::duration_cast<std::chrono::milliseconds>(end_t_segmentation - step_t_segmentation).count() << " ms"<<std::endl;
// step_t_segmentation = end_t_segmentation;
// //frame box disparity on the preprocessed image
// cv::cvtColor(pre_canny, pre_canny_RGB, CV_GRAY2RGB);
// cv::cvtColor(canny, canny_RGB, CV_GRAY2RGB);
// frame_box_disparity(pre_canny_RGB, canny_RGB, pre_rois, frame_nbr_loc);
// // reset pre_rois for the new roi of the current frame
// pre_rois.erase(pre_rois.begin(), pre_rois.end());
// end_t_segmentation = std::chrono::steady_clock::now();
// std::cout << " TIME Canny Frame disparity : "<<std::chrono::duration_cast<std::chrono::milliseconds>(end_t_segmentation - step_t_segmentation).count() << " ms"<<std::endl;
// step_t_segmentation = end_t_segmentation;
// //---------------------------------
updates.mutex_di.lock();
updates.disparity = disparity_frame.clone();
updates.update_di = true;
updates.mutex_di.unlock();
}
pre_canny = canny.clone();
if(first_iteration)
first_iteration = false;
end_t = std::chrono::steady_clock::now();
std::cout<<"disparityFrame : TIME END pre canny : "<<std::chrono::duration_cast<std::chrono::milliseconds>(end_t - start_t).count() << " ms"<<std::endl;
}
}
return (void *)0;
}
void *computationTask(void *x_void_ptr)
{
Camera_t *camera = (Camera_t *)x_void_ptr;
@@ -731,7 +296,8 @@ void *computationTask(void *x_void_ptr)
tk::dnn::box b;
int x0, h, y1; //w, x1, y0;
int objClass;
std::string det_class;;
std::string det_class;
;
// float prob;
cv::Scalar intensity;
@@ -878,8 +444,7 @@ void *computationTask(void *x_void_ptr)
// segmentation(frame(roi), frame(roi), frame_nbr, i, 1);
/////
convert_coords(coords, x0 + b.w / 2, y1, objClass, H, adfGeoTransform, frame_nbr);
convert_coords(coords, x0 + b.w / 2, y1, objClass, H, adfGeoTransform);
// //std::cout<<objClass<<" ("<<prob<<"): "<<x0<<" "<<y0<<" "<<x1<<" "<<y1<<"\n";
// cv::rectangle(frame, cv::Point(x0, y0), cv::Point(x1, y1), camera->yolo.colors[objClass], 2);
@@ -1038,7 +603,6 @@ int main(int argc, char *argv[])
if (check_visualization > 1)
return -1;
tk::dnn::Yolo3Detection yolo[n_cameras];
for (int i = 0; i < n_cameras; i++)
{
@@ -1064,7 +628,6 @@ int main(int argc, char *argv[])
cameras[i].adfGeoTransform[j] = adfGeoTransform[j];
cameras[i].yolo = yolo[i];
// cameras[i].yolo = yolo;
}
std::cout << "INIZIA:\n";
pthread_t camera_task[n_cameras];
+86 -54
View File
@@ -5,9 +5,13 @@
#include "utils.h"
#include "Network.h"
namespace tk { namespace dnn {
namespace tk
{
namespace dnn
{
enum layerType_t {
enum layerType_t
{
LAYER_DENSE,
LAYER_CONV2D,
LAYER_ACTIVATION,
@@ -26,14 +30,16 @@ enum layerType_t {
/**
Simple layer Father class
*/
class Layer {
class Layer
{
public:
Layer(Network *net);
virtual ~Layer();
virtual layerType_t getLayerType() = 0;
virtual dnnType* infer(dataDim_t &dim, dnnType* srcData) {
virtual dnnType *infer(dataDim_t &dim, dnnType *srcData)
{
std::cout << "No infer action for this layer\n";
return NULL;
}
@@ -41,37 +47,52 @@ public:
dataDim_t input_dim, output_dim;
dnnType *dstData; //where results will be putted
std::string getLayerName() {
std::string getLayerName()
{
layerType_t type = getLayerType();
switch(type) {
case LAYER_DENSE: return "Dense";
case LAYER_CONV2D: return "Conv2d";
case LAYER_ACTIVATION: return "Activation";
case LAYER_FLATTEN: return "Flatten";
case LAYER_MULADD: return "MulAdd";
case LAYER_POOLING: return "Pooling";
case LAYER_SOFTMAX: return "Softmax";
case LAYER_ROUTE: return "Route";
case LAYER_REORG: return "Reorg";
case LAYER_SHORTCUT: return "Shortcut";
case LAYER_UPSAMPLE: return "Upsample";
case LAYER_REGION: return "Region";
case LAYER_YOLO: return "Yolo";
default: return "unknown";
switch (type)
{
case LAYER_DENSE:
return "Dense";
case LAYER_CONV2D:
return "Conv2d";
case LAYER_ACTIVATION:
return "Activation";
case LAYER_FLATTEN:
return "Flatten";
case LAYER_MULADD:
return "MulAdd";
case LAYER_POOLING:
return "Pooling";
case LAYER_SOFTMAX:
return "Softmax";
case LAYER_ROUTE:
return "Route";
case LAYER_REORG:
return "Reorg";
case LAYER_SHORTCUT:
return "Shortcut";
case LAYER_UPSAMPLE:
return "Upsample";
case LAYER_REGION:
return "Region";
case LAYER_YOLO:
return "Yolo";
default:
return "unknown";
}
}
protected:
Network *net;
cudnnTensorDescriptor_t srcTensorDesc, dstTensorDesc;
};
/**
Father class of all layer that need to load trained weights
*/
class LayerWgs : public Layer {
class LayerWgs : public Layer
{
public:
LayerWgs(Network *net, int inputs, int outputs, int kh, int kw, int kt,
@@ -101,11 +122,11 @@ public:
__half *variance16_h, *variance16_d;
};
/**
Dense (full interconnection) layer
*/
class Dense : public LayerWgs {
class Dense : public LayerWgs
{
public:
Dense(Network *net, int out_ch, const char *fname_weights);
@@ -115,11 +136,11 @@ public:
virtual dnnType *infer(dataDim_t &dim, dnnType *srcData);
};
/**
Avaible activation functions
*/
typedef enum {
typedef enum
{
ACTIVATION_ELU = 100,
ACTIVATION_LEAKY = 101
} tkdnnActivationMode_t;
@@ -127,7 +148,8 @@ typedef enum {
/**
Activation layer (it doesnt need weigths)
*/
class Activation : public Layer {
class Activation : public Layer
{
public:
int act_mode;
@@ -142,11 +164,11 @@ protected:
cudnnActivationDescriptor_t activDesc;
};
/**
Convolutional 2D layer
*/
class Conv2d : public LayerWgs {
class Conv2d : public LayerWgs
{
public:
Conv2d(Network *net, int out_ch, int kernelH, int kernelW,
@@ -169,12 +191,12 @@ protected:
size_t ws_sizeInBytes;
};
/**
Flatten layer
is actually a matrix transposition
*/
class Flatten : public Layer {
class Flatten : public Layer
{
public:
Flatten(Network *net);
@@ -184,12 +206,12 @@ public:
virtual dnnType *infer(dataDim_t &dim, dnnType *srcData);
};
/**
MulAdd layer
apply a multiplication and then an addition for each data
*/
class MulAdd : public Layer {
class MulAdd : public Layer
{
public:
MulAdd(Network *net, dnnType mul, dnnType add);
@@ -203,12 +225,11 @@ protected:
dnnType *add_vector;
};
/**
Avaible pooling functions (padding on tkDNN is not supported)
*/
typedef enum {
typedef enum
{
POOLING_MAX = 0,
POOLING_AVERAGE = 1, // count for average includes padded values
POOLING_AVERAGE_EXCLUDE_PADDING = 2 // count for average does not include padded values
@@ -218,7 +239,8 @@ typedef enum {
Pooling layer
currenty supported only 2d pooing (also on 3d input)
*/
class Pooling : public Layer {
class Pooling : public Layer
{
public:
int winH, winW;
@@ -233,7 +255,6 @@ public:
virtual dnnType *infer(dataDim_t &dim, dnnType *srcData);
protected:
cudnnPoolingDescriptor_t poolingDesc;
tkdnnPoolingMode_t pool_mode;
dnnType *tmpInputData, *tmpOutputData;
@@ -243,7 +264,8 @@ protected:
/**
Softmax layer
*/
class Softmax : public Layer {
class Softmax : public Layer
{
public:
Softmax(Network *net);
@@ -257,7 +279,8 @@ public:
Route layer
Merge a list of layers
*/
class Route : public Layer {
class Route : public Layer
{
public:
Route(Network *net, Layer **layers, int layers_n);
@@ -271,12 +294,12 @@ public:
int layers_n; //number of layers
};
/**
Reorg layer
Mantain same dimension but change C*H*W distribution
*/
class Reorg : public Layer {
class Reorg : public Layer
{
public:
Reorg(Network *net, int stride);
@@ -292,7 +315,8 @@ public:
Shortcut layer
sum with stride another layer
*/
class Shortcut : public Layer {
class Shortcut : public Layer
{
public:
Shortcut(Network *net, Layer *backLayer);
@@ -309,7 +333,8 @@ public:
Upsample layer
Mantain same dimension but change C*H*W distribution
*/
class Upsample : public Layer {
class Upsample : public Layer
{
public:
Upsample(Network *net, int stride);
@@ -322,12 +347,14 @@ public:
bool reverse;
};
struct box {
struct box
{
int cl;
float x, y, w, h;
float prob;
};
struct sortable_bbox {
struct sortable_bbox
{
int index;
int cl;
float **probs;
@@ -336,14 +363,17 @@ struct sortable_bbox {
/**
Yolo3 layer
*/
class Yolo : public Layer {
class Yolo : public Layer
{
public:
struct box {
struct box
{
float x, y, w, h;
};
struct detection{
struct detection
{
Yolo::box bbox;
int classes;
float *prob;
@@ -373,7 +403,8 @@ public:
/**
Region layer
*/
class Region : public Layer {
class Region : public Layer
{
public:
Region(Network *net, int classes, int coords, int num);
@@ -385,7 +416,8 @@ public:
virtual dnnType *infer(dataDim_t &dim, dnnType *srcData);
};
class RegionInterpret {
class RegionInterpret
{
public:
RegionInterpret(dataDim_t input_dim, dataDim_t output_dim,
@@ -397,7 +429,6 @@ public:
int classes, coords, num;
float thresh;
box *boxes;
float **probs;
sortable_bbox *s;
@@ -415,5 +446,6 @@ public:
static float box_iou(box a, box b);
};
}}
} // namespace dnn
} // namespace tk
#endif //LAYER_H
+15 -8
View File
@@ -3,7 +3,10 @@
#include "utils.h"
namespace tk { namespace dnn {
namespace tk
{
namespace dnn
{
/**
Data rapresentation beetween layers
@@ -13,20 +16,22 @@ namespace tk { namespace dnn {
w = width (rows)
l = lenght (3rd dimension)
*/
struct dataDim_t {
struct dataDim_t
{
int n, c, h, w, l;
dataDim_t() : n(1), c(1), h(1), w(1), l(1){};
dataDim_t(int _n, int _c, int _h, int _w, int _l = 1) :
n(_n), c(_c), h(_h), w(_w), l(_l) {};
dataDim_t(int _n, int _c, int _h, int _w, int _l = 1) : n(_n), c(_c), h(_h), w(_w), l(_l){};
void print() {
void print()
{
std::cout << "Data dim: " << n << " " << c << " " << h << " " << w << " " << l << "\n";
}
int tot() {
int tot()
{
return n * c * h * w * l;
}
};
@@ -34,7 +39,8 @@ struct dataDim_t {
class Layer;
const int MAX_LAYERS = 256;
class Network {
class Network
{
public:
Network(dataDim_t input_dim);
@@ -62,5 +68,6 @@ public:
bool fp16, dla;
};
}}
} // namespace dnn
} // namespace tk
#endif //NETWORK_H
+12 -7
View File
@@ -7,15 +7,20 @@
#include "Layer.h"
#include "NvInfer.h"
namespace tk { namespace dnn {
namespace tk
{
namespace dnn
{
template<typename T> void writeBUF(char*& buffer, const T& val)
template <typename T>
void writeBUF(char *&buffer, const T &val)
{
*reinterpret_cast<T *>(buffer) = val;
buffer += sizeof(T);
}
template<typename T> T readBUF(const char*& buffer)
template <typename T>
T readBUF(const char *&buffer)
{
T val = *reinterpret_cast<const T *>(buffer);
buffer += sizeof(T);
@@ -41,9 +46,8 @@ public:
virtual IPlugin *createPlugin(const char *layerName, const void *serialData, size_t serialLength);
};
class NetworkRT {
class NetworkRT
{
public:
nvinfer1::DataType dtRT;
@@ -90,5 +94,6 @@ public:
bool deserialize(const char *filename);
};
}}
} // namespace dnn
} // namespace tk
#endif //NETWORKRT_H
+14 -7
View File
@@ -1,3 +1,6 @@
#ifndef YOLO3DDETECTION_H
#define YOLO3DDETECTION_H
#include <iostream>
#include <signal.h>
#include <stdlib.h> /* srand, rand */
@@ -11,13 +14,17 @@
#include "tkdnn.h"
namespace tk { namespace dnn {
namespace tk
{
namespace dnn
{
/**
*
* @author Francesco Gatti
*/
class Yolo3Detection {
class Yolo3Detection
{
private:
tk::dnn::NetworkRT *netRT = nullptr;
@@ -30,8 +37,6 @@ class Yolo3Detection {
cv::Mat imageF;
cv::Mat bgr[3];
public:
int classes = 0;
int num = 0;
@@ -46,14 +51,16 @@ class Yolo3Detection {
virtual ~Yolo3Detection() {}
/**
* Method used for inizialize the class
* Method used to inizialize the class
*
* @return Success of the initialization
*/
bool init(std::string tensor_path);
void addBorders(cv::Mat &imageORIG, cv::Mat &imageWBorders, int &top, int &left);
void update(cv::Mat &frame);
};
}}
} // namespace dnn
} // namespace tk
#endif /*YOLO3DDETECTION_H*/
+32
View File
@@ -0,0 +1,32 @@
#ifndef CALIBRATION_H
#define CALIBRATION_H
#include "gdal.h"
#include <gdal_priv.h>
#include <gdal/gdal.h>
#include "gdal/gdal_priv.h"
#include "gdal/cpl_conv.h"
#include <yaml-cpp/yaml.h>
#include <opencv2/calib3d.hpp>
#include <opencv2/core.hpp>
#include <iostream>
#include <cstring>
struct ObjCoords
{
double lat_;
double long_;
int class_;
};
void readTiff(char *filename, double *adfGeoTransform);
void readCameraCalibrationYaml(const std::string &cameraCalib, cv::Mat &cameraMat, cv::Mat &distCoeff);
void pixel2coord(int x, int y, double &lat, double &lon, double *adfGeoTransform);
void coord2pixel(double lat, double lon, int &x, int &y, double *adfGeoTransform);
void fillMatrix(cv::Mat &H, double *matrix, bool show = false);
void read_projection_matrix(cv::Mat &H, char *path);
void convert_coords(std::vector<ObjCoords> &coords, int x, int y, int detected_class, cv::Mat H, double *adfGeoTransform);
#endif /*CALIBRATION_H*/
+45
View File
@@ -0,0 +1,45 @@
#ifndef CAMERAUTILS_H
#define CAMERAUTILS_H
#include <vector>
#include <mutex>
#include <opencv2/core/core.hpp>
#include "tracker.h"
#include "Yolo3Detection.h"
struct Camera_t
{
int CAM_IDX;
char *input;
char *pmatrix;
char *maskfile;
char *cameraCalib;
char *maskFileOrient;
bool to_show;
tk::dnn::Yolo3Detection yolo;
double adfGeoTransform[6];
};
struct Frame_t
{
char *input;
cv::Mat frame;
int frame_nbr;
// sem_vc for mainthread, videocapturethread, originalthread and disparitythread
std::mutex sem_vc;
};
struct ModFrame_t
{
std::vector<Tracker> trackers;
geodetic_converter::GeodeticConverter gc;
double adfGeoTransform[6];
cv::Mat H;
cv::Mat original_frame;
tk::dnn::Yolo3Detection yolo;
cv::Mat mask;
// sem for mainthread, detectionthread and topviewthread
std::mutex sem;
};
#endif /*CAMERAUTILS_H*/
-316
View File
@@ -1,316 +0,0 @@
#ifndef CLASSUTILS_H
#define CLASSUTILS_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/socket.h> //socket
#include <arpa/inet.h> //inet_addr
#include <unistd.h> //write
#include <opencv2/calib3d.hpp>
#include <opencv2/core.hpp>
#include "gdal.h"
#include <gdal_priv.h>
#include <gdal/gdal.h>
#include "gdal/gdal_priv.h"
#include "gdal/cpl_conv.h"
#include "tracker.h"
#include <yaml-cpp/yaml.h>
#include "../masa_protocol/include/send.hpp"
#include "../masa_protocol/include/serialize.hpp"
struct ObjCoords
{
double lat_;
double long_;
int class_;
};
void readTiff(char *filename, double *adfGeoTransform)
{
GDALDataset *poDataset;
GDALAllRegister();
poDataset = (GDALDataset *)GDALOpen(filename, GA_ReadOnly);
if (poDataset != NULL)
{
poDataset->GetGeoTransform(adfGeoTransform);
}
}
void readCameraCalibrationYaml(const std::string &cameraCalib, cv::Mat &cameraMat, cv::Mat &distCoeff)
{
YAML::Node config = YAML::LoadFile(cameraCalib);
const YAML::Node &node_test1 = config["camera_matrix"];
float data_cm[9];
for (std::size_t i = 0; i < node_test1["data"].size(); i++)
data_cm[i] = node_test1["data"][i].as<float>();
cv::Mat cameraMat_ = cv::Mat(3, 3, CV_32F, data_cm);
cameraMat = cameraMat_.clone();
std::cout << cameraMat << std::endl;
const YAML::Node &node_test2 = config["distortion_coefficients"];
float data_dc[5];
for (std::size_t i = 0; i < node_test2["data"].size(); i++)
data_dc[i] = node_test2["data"][i].as<float>();
cv::Mat distCoeff_ = cv::Mat(5, 1, CV_32F, data_dc);
distCoeff = distCoeff_.clone();
std::cout << distCoeff << std::endl;
}
void pixel2coord(int x, int y, double &lat, double &lon, double *adfGeoTransform)
{
//Returns global coordinates from pixel x, y coordinates
double xoff, a, b, yoff, d, e;
xoff = adfGeoTransform[0];
a = adfGeoTransform[1];
b = adfGeoTransform[2];
yoff = adfGeoTransform[3];
d = adfGeoTransform[4];
e = adfGeoTransform[5];
//printf("%f %f %f %f %f %f\n",xoff, a, b, yoff, d, e );
lon = a * x + b * y + xoff;
lat = d * x + e * y + yoff;
}
void coord2pixel(double lat, double lon, int &x, int &y, double *adfGeoTransform)
{
x = int(round((lon - adfGeoTransform[0]) / adfGeoTransform[1]));
y = int(round((lat - adfGeoTransform[3]) / adfGeoTransform[5]));
}
void fillMatrix(cv::Mat &H, double *matrix, bool show = false)
{
double *vals = (double *)H.data;
for (int i = 0; i < 9; i++)
{
vals[i] = matrix[i];
}
if (show)
std::cout << H << "\n";
}
//FILE *out_file = fopen("prova_pixel.txt", "w");
void convert_coords(std::vector<ObjCoords> &coords, int x, int y, int detected_class, cv::Mat H, double *adfGeoTransform, int frame_nbr)
{
double latitude, longitude;
std::vector<cv::Point2f> x_y, ll;
x_y.push_back(cv::Point2f(x, y));
//transform camera pixel to map pixel
cv::perspectiveTransform(x_y, ll, H);
//tranform to map pixel to map gps
pixel2coord(ll[0].x, ll[0].y, latitude, longitude, adfGeoTransform);
//printf("lat: %f, long:%f \n", latitude, longitude);
ObjCoords coord;
coord.lat_ = latitude;
coord.long_ = longitude;
coord.class_ = detected_class;
coords.push_back(coord);
/*if (detected_class == 0)
{
struct timeval tv;
gettimeofday(&tv, NULL);
unsigned long long t_stamp_ms = (unsigned long long)(tv.tv_sec) * 1000 + (unsigned long long)(tv.tv_usec) / 1000;
//printf(out_file, "%d %lld %d %d\n",frame_nbr, t_stamp_ms, int(ll[0].x), int(ll[0].y));
fprintf(out_file, "%d %lld %f %f\n", frame_nbr, t_stamp_ms, coord.LAT, coord.LONG);
//printf( "%d %lld %f %f\n", frame_nbr, t_stamp_ms, coord.LAT, coord.LONG);
}*/
}
void read_projection_matrix(cv::Mat &H, char *path)
{
FILE *fp;
char *line = NULL;
size_t len = 0;
ssize_t read;
// float *proj_matrix = (float *)malloc(9 * sizeof(float));
double proj_matrix[9] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
int i = 0;
fp = fopen(path, "r");
if (fp == NULL)
exit(EXIT_FAILURE);
while ((read = getline(&line, &len, fp)) != -1)
{
std::cout<<line<<std::endl;
std::stringstream ss(line);
while (ss >> proj_matrix[i])
i++;
}
fclose(fp);
fillMatrix(H, proj_matrix);
free(line);
// free(proj_matrix);
}
void draw_arrow(float angleRad, float vel, cv::Scalar color, cv::Point center, cv::Mat &frame)
{
int angle = angleRad * 180.0 / CV_PI;
auto length = 10 * vel;
auto direction = cv::Point(length * cos(angleRad), length * sin(angleRad)); // calculate direction
double tipLength = .2 + 0.4 * (angle % 180) / 360;
int lineType = 8;
int thickness = 2;
cv::arrowedLine(frame, center, center + direction, color, thickness, lineType, 0, tipLength); // draw arrow!
}
unsigned long long time_in_ms()
{
struct timeval tv;
gettimeofday(&tv, NULL);
unsigned long long t_stamp_ms = (unsigned long long)(tv.tv_sec) * 1000 + (unsigned long long)(tv.tv_usec) / 1000;
return t_stamp_ms;
}
void addRoadUserfromTracker(const std::vector<Tracker> &trackers, Message *m, geodetic_converter::GeodeticConverter &gc, const cv::Mat& maskOrient, double *adfGeoTransform, cv::Mat H)
{
m->t_stamp_ms = time_in_ms();
m->objects.clear();
double lat, lon, alt;
for (auto t : trackers)
{
if (t.pred_list_.size() > 0)
{
Categories cat;
switch (t.class_)
{
case 0:
cat = Categories::C_person;
break;
case 1:
cat = Categories::C_car;
break;
case 2:
cat = Categories::C_car;
break;
case 3:
cat = Categories::C_bus;
break;
case 4:
cat = Categories::C_motorbike;
break;
case 5:
cat = Categories::C_bycicle;
break;
}
//std::cout << t.pred_list_.size() << std::endl;
gc.enu2Geodetic(t.pred_list_.back().x_, t.pred_list_.back().y_, 0, &lat, &lon, &alt);
int pix_x, pix_y;
coord2pixel(lat, lon, pix_x, pix_y, adfGeoTransform);
// TODO: test correctness - added perspective transform call to converter pix_x and pix_y
// sometimes some values are wrong. float ok?
// std::vector<cv::Point2f> map_p, camera_p;
// std::cout<<"--- pix_x, pix_y: "<<pix_x<<", "<<pix_y<<std::endl;
// map_p.push_back(cv::Point2f(pix_x, pix_y));
// std::cout<<"map_p: "<<map_p<<std::endl;
// //transform camera pixel to map pixel
// cv::perspectiveTransform(map_p, camera_p, H.inv());
// std::cout<<"size H: "<<H.cols<<", "<<H.rows<<std::endl;
// std::cout<<"camera_p: "<<camera_p<<std::endl;
// // TODO: in some cases these lines causes seg fault!
// std::cout<<"y, x :"<<camera_p[0].y<<", "<<camera_p[0].x<<std::endl;
// std::cout<<"size maskorient: "<<maskOrient.cols<<", "<<maskOrient.rows<<std::endl;
// // std::cout<<"vec3b: "<<(cv::Vec3b)(pix_y,pix_x);
// assert (camera_p[0].x < maskOrient.cols);
// assert (camera_p[0].y < maskOrient.rows);
// uint8_t maskOrientPixel = maskOrient.at<cv::Vec3b>(camera_p[0].y,camera_p[0].x)[0];
// std::cout<<"boo: "<<maskOrient.at<cv::Vec3b>(camera_p[0].y,camera_p[0].x)<<std::endl;
// uint8_t orientation;
// if(maskOrientPixel != 0)
// {
// orientation = maskOrientPixel;
// // std::cout<<"orientation given by the mask "<< int(orientation)<<std::endl;
// }
// else
// {
// orientation = uint8_t((int((t.pred_list_.back().yaw_ * 57.29 + 360)) % 360) * 17 / 24);
// //std::cout<<"orientation given by the tracker "<< int(orientation)<<std::endl;
// }
// TODO: to validate -> it works for grayscale image (see demo.cpp, row: "cv::Mat maskOrient = cv::imread(camera->maskFileOrient, 0);")
// TODO: include perspective transform
// std::cout<<"y, x :"<<pix_y<<", "<<pix_x<<std::endl;
// std::cout<<"size maskorient: "<<maskOrient.cols<<", "<<maskOrient.rows<<std::endl;
// std::cout<<"point: "<<(cv::Point)(pix_y,pix_x);
// uint8_t maskOrientPixel = maskOrient.at<uchar>(pix_y,pix_x);
// uint8_t orientation;
// if(maskOrientPixel != 0)
// {
// orientation = maskOrientPixel;
// // std::cout<<"orientation given by the mask "<< int(orientation)<<std::endl;
// }
// else
// {
// orientation = uint8_t((int((t.pred_list_.back().yaw_ * 57.29 + 360)) % 360) * 17 / 24);
// //std::cout<<"orientation given by the tracker "<< int(orientation)<<std::endl;
// }
uint8_t orientation = uint8_t((int((t.pred_list_.back().yaw_ * 57.29 + 360)) % 360) * 17 / 24);
// std::cout<<"orient: "<<unsigned(orientation)<<std::endl;
//std::cout << "lat: " << lat << " lon: " << lon << std::endl;
uint8_t velocity = uint8_t(std::abs(t.pred_list_.back().vel_ * 3.6 / 2));
// std::cout<<"vel: "<<unsigned(velocity)<<std::endl;
RoadUser r{static_cast<float>(lat), static_cast<float>(lon), velocity, orientation, cat};
//std::cout << std::setprecision(10) << r.latitude << " , " << r.longitude << " " << int(r.speed) << " " << int(r.orientation) << " " << r.category << std::endl;
m->objects.push_back(r);
}
}
m->num_objects = m->objects.size();
}
void prepare_message(Message *m, const std::vector<ObjCoords> &coords, int idx)
{
m->cam_idx = idx;
m->t_stamp_ms = time_in_ms();
m->num_objects = coords.size();
m->objects.clear();
for (unsigned int i = 0; i < coords.size(); i++)
{
Categories cat;
switch (coords[i].class_)
{
case 0:
cat = Categories::C_person;
break;
case 1:
cat = Categories::C_car;
break;
case 2:
cat = Categories::C_car;
break;
case 3:
cat = Categories::C_bus;
break;
case 4:
cat = Categories::C_motorbike;
break;
case 5:
cat = Categories::C_bycicle;
break;
}
RoadUser r{static_cast<float>(coords[i].lat_), static_cast<float>(coords[i].long_), 0, 1, cat};
std::cout << std::setprecision(10) << r.latitude << " , " << r.longitude << " " << cat << std::endl;
m->objects.push_back(r);
}
m->lights.clear();
}
#endif /*CLASSUTILS_H*/
+22
View File
@@ -0,0 +1,22 @@
#ifndef MESSAGE_H
#define MESSAGE_H
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <opencv2/calib3d.hpp>
#include <opencv2/core.hpp>
// #include <sys/socket.h> //socket
// #include <arpa/inet.h> //inet_addr
// #include <unistd.h> //write
#include "tracker.h"
#include "../masa_protocol/include/send.hpp"
#include "../masa_protocol/include/serialize.hpp"
unsigned long long time_in_ms();
void addRoadUserfromTracker(const std::vector<Tracker> &trackers, Message *m, geodetic_converter::GeodeticConverter &gc, const cv::Mat &maskOrient, double *adfGeoTransform, cv::Mat H);
#endif /*MESSAGE_H*/
+28 -14
View File
@@ -32,13 +32,17 @@
#define COL_CYANB "\033[1;36m"
// Simple Timer
#define TIMER_START timespec start, end; \
#define TIMER_START \
timespec start, end; \
clock_gettime(CLOCK_MONOTONIC, &start);
#define TIMER_STOP_C(col) clock_gettime(CLOCK_MONOTONIC, &end); \
#define TIMER_STOP_C(col) \
clock_gettime(CLOCK_MONOTONIC, &end); \
double t_ns = ((double)(end.tv_sec - start.tv_sec) * 1.0e9 + \
(double)(end.tv_nsec - start.tv_nsec))/1.0e6; \
std::cout<<col<<"Time:"<<std::setw(16)<<t_ns<<" ms\n"<<COL_END;
(double)(end.tv_nsec - start.tv_nsec)) / \
1.0e6; \
std::cout << col << "Time:" << std::setw(16) << t_ns << " ms\n" \
<< COL_END;
#define TIMER_STOP TIMER_STOP_C(COL_CYANB)
@@ -47,42 +51,52 @@
* ******************************************************/
#define EXIT_WAIVED 0
#define FatalError(s) { \
#define FatalError(s) \
{ \
std::stringstream _where, _message; \
_where << __FILE__ << ':' << __LINE__; \
_message << std::string(s) + "\n" << __FILE__ << ':' << __LINE__;\
_message << std::string(s) + "\n" \
<< __FILE__ << ':' << __LINE__; \
std::cerr << _message.str() << "\nAborting...\n"; \
cudaDeviceReset(); \
exit(EXIT_FAILURE); \
}
#define checkCUDNN(status) { \
#define checkCUDNN(status) \
{ \
std::stringstream _error; \
if (status != CUDNN_STATUS_SUCCESS) { \
if (status != CUDNN_STATUS_SUCCESS) \
{ \
_error << "CUDNN failure: " << cudnnGetErrorString(status); \
FatalError(_error.str()); \
} \
}
#define checkCuda(status) { \
#define checkCuda(status) \
{ \
std::stringstream _error; \
if (status != 0) { \
if (status != 0) \
{ \
_error << "Cuda failure: " << cudaGetErrorString(status); \
FatalError(_error.str()); \
} \
}
#define checkERROR(status) { \
#define checkERROR(status) \
{ \
std::stringstream _error; \
if (status != 0) { \
if (status != 0) \
{ \
_error << "Generic failure: " << status; \
FatalError(_error.str()); \
} \
}
#define checkNULL(ptr) { \
#define checkNULL(ptr) \
{ \
std::stringstream _error; \
if (ptr == nullptr) { \
if (ptr == nullptr) \
{ \
_error << "Null pointer"; \
FatalError(_error.str()); \
} \
+42
View File
@@ -0,0 +1,42 @@
#ifndef VIZUALIZATION_H
#define VIZUALIZATION_H
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
//saliency
#include <opencv2/core/utility.hpp>
#include <opencv2/saliency.hpp>
#include <opencv2/highgui.hpp>
#include <chrono>
#include <iostream>
#include <cstring>
#include "tracker.h"
#include "cameraUtils.h"
#include "calibration.h"
#include "boxDetection.h"
struct Show_t
{
cv::Mat original, detection, topview, disparity;
bool update_o, update_de, update_t, update_di;
// a single mutex for each operation - the show_updates function must get all mutex
std::mutex mutex_o, mutex_de, mutex_t, mutex_di;
};
extern Show_t updates;
extern bool gRun;
extern std::string obj_class[10];
/* Thread function to show the updated images
**/
void *show_updates(void *x_void_ptr);
void *originalFrame(void *x_void_ptr);
void *detectionFrame(void *x_void_ptr);
void *topviewFrame(void *x_void_ptr);
void *disparityFrame(void *x_void_ptr);
#endif /*VIZUALIZATION_H*/
+19 -11
View File
@@ -3,15 +3,19 @@
#include "Layer.h"
#include "kernels.h"
namespace tk { namespace dnn {
namespace tk
{
namespace dnn
{
Activation::Activation(Network *net, int act_mode) :
Layer(net) {
Activation::Activation(Network *net, int act_mode) : Layer(net)
{
this->act_mode = act_mode;
checkCuda(cudaMalloc(&dstData, input_dim.tot() * sizeof(dnnType)));
if(int(act_mode) < 100) {
if (int(act_mode) < 100)
{
checkCUDNN(cudnnSetTensor4dDescriptor(srcTensorDesc,
net->tensorFormat,
@@ -26,7 +30,6 @@ Activation::Activation(Network *net, int act_mode) :
input_dim.c,
input_dim.h, input_dim.w));
checkCUDNN(cudnnCreateActivationDescriptor(&activDesc));
checkCUDNN(cudnnSetActivationDescriptor(activDesc,
(cudnnActivationMode_t)act_mode,
@@ -35,7 +38,8 @@ Activation::Activation(Network *net, int act_mode) :
}
}
Activation::~Activation() {
Activation::~Activation()
{
checkCuda(cudaFree(dstData));
@@ -43,12 +47,15 @@ Activation::~Activation() {
checkCUDNN(cudnnDestroyActivationDescriptor(activDesc));
}
dnnType* Activation::infer(dataDim_t &dim, dnnType* srcData) {
dnnType *Activation::infer(dataDim_t &dim, dnnType *srcData)
{
if(act_mode == ACTIVATION_LEAKY) {
if (act_mode == ACTIVATION_LEAKY)
{
activationLEAKYForward(srcData, dstData, dim.tot());
} else {
}
else
{
dnnType alpha = dnnType(1);
dnnType beta = dnnType(0);
checkCUDNN(cudnnActivationForward(net->cudnnHandle,
@@ -63,4 +70,5 @@ dnnType* Activation::infer(dataDim_t &dim, dnnType* srcData) {
return dstData;
}
}}
} // namespace dnn
} // namespace tk
+19 -11
View File
@@ -2,14 +2,18 @@
#include "Layer.h"
namespace tk { namespace dnn {
namespace tk
{
namespace dnn
{
Conv2d::Conv2d(Network *net, int out_ch, int kernelH, int kernelW,
int strideH, int strideW, int paddingH, int paddingW,
const char *fname_weights, bool batchnorm) :
LayerWgs(net, net->getOutputDim().c, out_ch, kernelH, kernelW, 1,
fname_weights, batchnorm) {
fname_weights, batchnorm)
{
this->kernelH = kernelH;
this->kernelW = kernelW;
@@ -59,16 +63,15 @@ Conv2d::Conv2d( Network *net, int out_ch, int kernelH, int kernelW,
srcTensorDesc, filterDesc, convDesc, dstTensorDesc,
algo, &ws_sizeInBytes));
if (ws_sizeInBytes!=0) {
if (ws_sizeInBytes != 0)
{
checkCuda(cudaMalloc(&workSpace, ws_sizeInBytes));
}
checkCUDNN(cudnnSetTensor4dDescriptor(biasTensorDesc,
net->tensorFormat, net->dataType,
1, out_ch, 1, 1));
output_dim.n = n;
output_dim.c = c;
output_dim.h = h;
@@ -79,7 +82,8 @@ Conv2d::Conv2d( Network *net, int out_ch, int kernelH, int kernelW,
checkCuda(cudaMalloc(&dstData, output_dim.tot() * sizeof(dnnType)));
}
Conv2d::~Conv2d() {
Conv2d::~Conv2d()
{
checkCUDNN(cudnnDestroyFilterDescriptor(filterDesc));
checkCUDNN(cudnnDestroyConvolutionDescriptor(convDesc));
@@ -91,8 +95,8 @@ Conv2d::~Conv2d() {
checkCuda(cudaFree(dstData));
}
dnnType* Conv2d::infer(dataDim_t &dim, dnnType* srcData) {
dnnType *Conv2d::infer(dataDim_t &dim, dnnType *srcData)
{
// convolution
dnnType alpha = dnnType(1);
@@ -102,14 +106,17 @@ dnnType* Conv2d::infer(dataDim_t &dim, dnnType* srcData) {
data_d, convDesc, algo, workSpace, ws_sizeInBytes,
&beta, dstTensorDesc, dstData));
if(!batchnorm) {
if (!batchnorm)
{
// bias
alpha = dnnType(1);
beta = dnnType(1);
checkCUDNN(cudnnAddTensor(net->cudnnHandle,
&alpha, biasTensorDesc, bias_d,
&beta, dstTensorDesc, dstData));
} else {
}
else
{
float one = 1;
float zero = 0;
cudnnBatchNormalizationForwardInference(net->cudnnHandle,
@@ -125,4 +132,5 @@ dnnType* Conv2d::infer(dataDim_t &dim, dnnType* srcData) {
return dstData;
}
}}
} // namespace dnn
} // namespace tk
+12 -6
View File
@@ -2,10 +2,13 @@
#include "Layer.h"
namespace tk { namespace dnn {
namespace tk
{
namespace dnn
{
Dense::Dense(Network *net, int out_ch, const char* fname_weights) :
LayerWgs(net, net->getOutputDim().tot(), out_ch, 1, 1, 1, fname_weights) {
Dense::Dense(Network *net, int out_ch, const char *fname_weights) : LayerWgs(net, net->getOutputDim().tot(), out_ch, 1, 1, 1, fname_weights)
{
output_dim.n = 1;
output_dim.c = out_ch;
@@ -17,12 +20,14 @@ Dense::Dense(Network *net, int out_ch, const char* fname_weights) :
checkCuda(cudaMalloc(&dstData, output_dim.tot() * sizeof(dnnType)));
}
Dense::~Dense() {
Dense::~Dense()
{
checkCuda(cudaFree(dstData));
}
dnnType* Dense::infer(dataDim_t &dim, dnnType* srcData) {
dnnType *Dense::infer(dataDim_t &dim, dnnType *srcData)
{
if (dim.n != 1)
FatalError("Not Implemented");
@@ -55,4 +60,5 @@ dnnType* Dense::infer(dataDim_t &dim, dnnType* srcData) {
return dstData;
}
}}
} // namespace dnn
} // namespace tk
+12 -6
View File
@@ -3,9 +3,13 @@
#include "Layer.h"
#include "kernels.h"
namespace tk { namespace dnn {
namespace tk
{
namespace dnn
{
Flatten::Flatten(Network *net) : Layer(net) {
Flatten::Flatten(Network *net) : Layer(net)
{
checkCuda(cudaMalloc(&dstData, input_dim.tot() * sizeof(dnnType)));
@@ -14,15 +18,16 @@ Flatten::Flatten(Network *net) : Layer(net) {
output_dim.h = 1;
output_dim.w = 1;
output_dim.l = 1;
}
Flatten::~Flatten() {
Flatten::~Flatten()
{
checkCuda(cudaFree(dstData));
}
dnnType* Flatten::infer(dataDim_t &dim, dnnType* srcData) {
dnnType *Flatten::infer(dataDim_t &dim, dnnType *srcData)
{
//transpose per channel
matrixTranspose(net->cublasHandle, srcData, dstData, dim.c, dim.h * dim.w * dim.l);
@@ -33,4 +38,5 @@ dnnType* Flatten::infer(dataDim_t &dim, dnnType* srcData) {
return dstData;
}
}}
} // namespace dnn
} // namespace tk
+12 -5
View File
@@ -2,13 +2,18 @@
#include "Layer.h"
namespace tk { namespace dnn {
namespace tk
{
namespace dnn
{
Layer::Layer(Network *net) {
Layer::Layer(Network *net)
{
this->net = net;
if(net != nullptr) {
if (net != nullptr)
{
this->input_dim = net->getOutputDim();
this->output_dim = input_dim;
@@ -20,10 +25,12 @@ Layer::Layer(Network *net) {
}
}
Layer::~Layer() {
Layer::~Layer()
{
checkCUDNN(cudnnDestroyTensorDescriptor(srcTensorDesc));
checkCUDNN(cudnnDestroyTensorDescriptor(dstTensorDesc));
}
}}
} // namespace dnn
} // namespace tk
+18 -9
View File
@@ -4,11 +4,15 @@
#include "Layer.h"
#include "kernels.h"
namespace tk { namespace dnn {
namespace tk
{
namespace dnn
{
LayerWgs::LayerWgs(Network *net, int inputs, int outputs,
int kh, int kw, int kl,
const char* fname_weights, bool batchnorm) : Layer(net) {
const char *fname_weights, bool batchnorm) : Layer(net)
{
this->inputs = inputs;
this->outputs = outputs;
@@ -21,7 +25,8 @@ LayerWgs::LayerWgs(Network *net, int inputs, int outputs,
readBinaryFile(weights_path.c_str(), outputs, &bias_h, &bias_d, seek);
this->batchnorm = batchnorm;
if(batchnorm) {
if (batchnorm)
{
seek += outputs;
readBinaryFile(weights_path.c_str(), outputs, &scales_h, &scales_d, seek);
seek += outputs;
@@ -32,7 +37,8 @@ LayerWgs::LayerWgs(Network *net, int inputs, int outputs,
float eps = CUDNN_BN_MIN_EPSILON;
power_h = new dnnType[outputs];
for(int i=0; i<outputs; i++) power_h[i] = 1.0f;
for (int i = 0; i < outputs; i++)
power_h[i] = 1.0f;
for (int i = 0; i < outputs; i++)
mean_h[i] = mean_h[i] / -sqrt(eps + variance_h[i]);
@@ -41,7 +47,6 @@ LayerWgs::LayerWgs(Network *net, int inputs, int outputs,
variance_h[i] = 1.0f / sqrt(eps + variance_h[i]);
}
if (!net->fp16)
return;
@@ -58,7 +63,8 @@ LayerWgs::LayerWgs(Network *net, int inputs, int outputs,
float2half(bias_d, bias16_d, b_size);
cudaMemcpy(bias16_h, bias16_d, b_size * sizeof(__half), cudaMemcpyDeviceToHost);
if(batchnorm) {
if (batchnorm)
{
power16_h = new __half[b_size];
mean16_h = new __half[b_size];
@@ -97,14 +103,16 @@ LayerWgs::LayerWgs(Network *net, int inputs, int outputs,
}
}
LayerWgs::~LayerWgs() {
LayerWgs::~LayerWgs()
{
delete[] data_h;
delete[] bias_h;
checkCuda(cudaFree(data_d));
checkCuda(cudaFree(bias_d));
if(batchnorm) {
if (batchnorm)
{
delete[] scales_h;
delete[] mean_h;
delete[] variance_h;
@@ -114,4 +122,5 @@ LayerWgs::~LayerWgs() {
}
}
}}
} // namespace dnn
} // namespace tk
+12 -6
View File
@@ -3,9 +3,13 @@
#include "Layer.h"
#include "kernels.h"
namespace tk { namespace dnn {
namespace tk
{
namespace dnn
{
MulAdd::MulAdd(Network *net, dnnType mul, dnnType add) : Layer(net) {
MulAdd::MulAdd(Network *net, dnnType mul, dnnType add) : Layer(net)
{
this->mul = mul;
this->add = add;
@@ -21,17 +25,18 @@ MulAdd::MulAdd(Network *net, dnnType mul, dnnType add) : Layer(net) {
checkCuda(cudaMemcpy(add_vector, add_vector_h, size * sizeof(dnnType), cudaMemcpyHostToDevice));
delete[] add_vector_h;
checkCuda(cudaMalloc(&dstData, input_dim.tot() * sizeof(dnnType)));
}
MulAdd::~MulAdd() {
MulAdd::~MulAdd()
{
checkCuda(cudaFree(add_vector));
checkCuda(cudaFree(dstData));
}
dnnType* MulAdd::infer(dataDim_t &dim, dnnType* srcData) {
dnnType *MulAdd::infer(dataDim_t &dim, dnnType *srcData)
{
matrixMulAdd(net->cublasHandle, srcData, dstData, add_vector, input_dim.tot(), mul);
@@ -41,4 +46,5 @@ dnnType* MulAdd::infer(dataDim_t &dim, dnnType* srcData) {
return dstData;
}
}}
} // namespace dnn
} // namespace tk
+50 -25
View File
@@ -5,9 +5,13 @@
#include "Network.h"
#include "Layer.h"
namespace tk { namespace dnn {
namespace tk
{
namespace dnn
{
Network::Network(dataDim_t input_dim) {
Network::Network(dataDim_t input_dim)
{
this->input_dim = input_dim;
float tk_ver = float(TKDNN_VERSION) / 1000;
@@ -25,10 +29,12 @@ Network::Network(dataDim_t input_dim) {
fp16 = false;
dla = false;
if(const char* env_p = std::getenv("TKDNN_MODE")) {
if (const char *env_p = std::getenv("TKDNN_MODE"))
{
if (strcmp(env_p, "FP16") == 0)
fp16 = true;
else if(strcmp(env_p, "DLA") == 0) {
else if (strcmp(env_p, "DLA") == 0)
{
dla = true;
fp16 = true;
}
@@ -40,23 +46,27 @@ Network::Network(dataDim_t input_dim) {
std::cout << COL_GREENB << "!! DLA INERENCE ENABLED !!" << COL_END << "\n";
}
Network::~Network() {
Network::~Network()
{
checkCUDNN(cudnnDestroy(cudnnHandle));
checkERROR(cublasDestroy(cublasHandle));
}
dnnType* Network::infer(dataDim_t &dim, dnnType* data) {
dnnType *Network::infer(dataDim_t &dim, dnnType *data)
{
//do infer for every layer
for(int i=0; i<num_layers; i++) {
for (int i = 0; i < num_layers; i++)
{
data = layers[i]->infer(dim, data);
}
checkCuda(cudaDeviceSynchronize());
return data;
}
bool Network::addLayer(Layer *l) {
bool Network::addLayer(Layer *l)
{
if (num_layers == MAX_LAYERS)
return false;
@@ -64,7 +74,8 @@ bool Network::addLayer(Layer *l) {
return true;
}
dataDim_t Network::getOutputDim() {
dataDim_t Network::getOutputDim()
{
if (num_layers == 0)
return input_dim;
@@ -72,39 +83,53 @@ dataDim_t Network::getOutputDim() {
return layers[num_layers - 1]->output_dim;
}
void Network::print() {
void Network::print()
{
printCenteredTitle(" NETWORK MODEL ", '=', 60);
std::cout.width(3); std::cout<<std::left<<"N.";
std::cout.width(3);
std::cout << std::left << "N.";
std::cout << " ";
std::cout.width(17); std::cout<<std::left<<"Layer type";
std::cout.width(22); std::cout<<std::left<<"input (H*W,CH)";
std::cout.width(16); std::cout<<std::left<<"output (H*W,CH)";
std::cout.width(17);
std::cout << std::left << "Layer type";
std::cout.width(22);
std::cout << std::left << "input (H*W,CH)";
std::cout.width(16);
std::cout << std::left << "output (H*W,CH)";
std::cout << "\n";
for(int i=0; i<num_layers; i++) {
for (int i = 0; i < num_layers; i++)
{
dataDim_t in = layers[i]->input_dim;
dataDim_t out = layers[i]->output_dim;
std::cout.width(3); std::cout<<std::right<<i;
std::cout.width(3);
std::cout << std::right << i;
std::cout << " ";
std::cout.width(16); std::cout<<std::left<<layers[i]->getLayerName();
std::cout.width(4); std::cout<<std::right<<in.h;
std::cout.width(16);
std::cout << std::left << layers[i]->getLayerName();
std::cout.width(4);
std::cout << std::right << in.h;
std::cout << " x ";
std::cout.width(4); std::cout<<std::right<<in.w;
std::cout.width(4);
std::cout << std::right << in.w;
std::cout << ", ";
std::cout.width(4); std::cout<<std::right<<in.c;
std::cout.width(4);
std::cout << std::right << in.c;
std::cout << " -> ";
std::cout.width(4); std::cout<<std::right<<out.h;
std::cout.width(4);
std::cout << std::right << out.h;
std::cout << " x ";
std::cout.width(4); std::cout<<std::right<<out.w;
std::cout.width(4);
std::cout << std::right << out.w;
std::cout << ", ";
std::cout.width(4); std::cout<<std::right<<out.c;
std::cout.width(4);
std::cout << std::right << out.c;
std::cout << "\n";
}
printCenteredTitle("", '=', 60);
std::cout << "\n";
}
}}
} // namespace dnn
} // namespace tk
@@ -1,4 +1,4 @@
#include "BoxDetection.h"
#include "boxDetection.h"
#include <string.h>
char buf_frame_crop_name[200];
@@ -263,17 +263,20 @@ void image_segmentation(cv::Mat frame_crop, int frame_nbr, int i)
auto end_t_segmentation = std::chrono::steady_clock::now();
cv::Mat ret;
// ret = img_threshold(frame_crop);
if(SAVE) SAVE_TO("../demo/demo/data/img_crop/%d_%d_imgthr.jpg", frame_nbr, i, img_threshold(frame_crop));
if (SAVE)
SAVE_TO("../demo/demo/data/img_crop/%d_%d_imgthr.jpg", frame_nbr, i, img_threshold(frame_crop));
end_t_segmentation = std::chrono::steady_clock::now();
std::cout << " - TIME imgthr (" << frame_nbr << "-" << i << ") : " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t_segmentation - step_t_segmentation).count() << " ms" << std::endl;
step_t_segmentation = end_t_segmentation;
// ret =img_background(frame_crop);
if(SAVE) SAVE_TO("../demo/demo/data/img_crop/%d_%d_imgback.jpg", frame_nbr, i, img_background(frame_crop));
if (SAVE)
SAVE_TO("../demo/demo/data/img_crop/%d_%d_imgback.jpg", frame_nbr, i, img_background(frame_crop));
end_t_segmentation = std::chrono::steady_clock::now();
std::cout << " - TIME imgback (" << frame_nbr << "-" << i << ") : " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t_segmentation - step_t_segmentation).count() << " ms" << std::endl;
step_t_segmentation = end_t_segmentation;
// ret = img_dist_transform(frame_crop);
if(SAVE) SAVE_TO("../demo/demo/data/img_crop/%d_%d_imgtrans.jpg", frame_nbr, i, img_dist_transform(frame_crop));
if (SAVE)
SAVE_TO("../demo/demo/data/img_crop/%d_%d_imgtrans.jpg", frame_nbr, i, img_dist_transform(frame_crop));
end_t_segmentation = std::chrono::steady_clock::now();
std::cout << " - TIME imgtrans (" << frame_nbr << "-" << i << ") : " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t_segmentation - step_t_segmentation).count() << " ms" << std::endl;
step_t_segmentation = end_t_segmentation;
@@ -291,25 +294,29 @@ void image_gradients(cv::Mat frame_crop, int frame_nbr, int i)
cv::Mat ret;
// sobel
// ret = img_sobel_abssobel(frame_crop, 0);
if(SAVE) SAVE_TO("../demo/demo/data/img_crop/%d_%d_imgsobel_x_8U.jpgg", frame_nbr, i, img_sobel_abssobel(frame_crop, 0));
if (SAVE)
SAVE_TO("../demo/demo/data/img_crop/%d_%d_imgsobel_x_8U.jpgg", frame_nbr, i, img_sobel_abssobel(frame_crop, 0));
end_t_segmentation = std::chrono::steady_clock::now();
std::cout << " - TIME sobel0 (" << frame_nbr << "-" << i << ") : " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t_segmentation - step_t_segmentation).count() << " ms" << std::endl;
step_t_segmentation = end_t_segmentation;
// ret = img_sobel_abssobel(frame_crop, 1);
if(SAVE) SAVE_TO("../demo/demo/data/img_crop/%d_%d_imgsobel_y_8U.jpg", frame_nbr, i, img_sobel_abssobel(frame_crop, 1));
if (SAVE)
SAVE_TO("../demo/demo/data/img_crop/%d_%d_imgsobel_y_8U.jpg", frame_nbr, i, img_sobel_abssobel(frame_crop, 1));
end_t_segmentation = std::chrono::steady_clock::now();
std::cout << " - TIME sobel1 (" << frame_nbr << "-" << i << ") : " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t_segmentation - step_t_segmentation).count() << " ms" << std::endl;
step_t_segmentation = end_t_segmentation;
// ret = img_sobel_abssobel(frame_crop, 2);
if(SAVE) SAVE_TO("../demo/demo/data/img_crop/%d_%d_imgsobel_x_64F.jpg", frame_nbr, i, img_sobel_abssobel(frame_crop, 2));
if (SAVE)
SAVE_TO("../demo/demo/data/img_crop/%d_%d_imgsobel_x_64F.jpg", frame_nbr, i, img_sobel_abssobel(frame_crop, 2));
end_t_segmentation = std::chrono::steady_clock::now();
std::cout << " - TIME sobel2 (" << frame_nbr << "-" << i << ") : " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t_segmentation - step_t_segmentation).count() << " ms" << std::endl;
step_t_segmentation = end_t_segmentation;
// ret = img_sobel_abssobel(frame_crop, 3);
if(SAVE) SAVE_TO("../demo/demo/data/img_crop/%d_%d_imgsobel_y_64F.jpg", frame_nbr, i, img_sobel_abssobel(frame_crop, 3));
if (SAVE)
SAVE_TO("../demo/demo/data/img_crop/%d_%d_imgsobel_y_64F.jpg", frame_nbr, i, img_sobel_abssobel(frame_crop, 3));
end_t_segmentation = std::chrono::steady_clock::now();
std::cout << " - TIME sobel3 (" << frame_nbr << "-" << i << ") : " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t_segmentation - step_t_segmentation).count() << " ms" << std::endl;
@@ -317,19 +324,20 @@ void image_gradients(cv::Mat frame_crop, int frame_nbr, int i)
// laplacian
// ret = img_laplacian(frame_crop, 0);
if(SAVE) SAVE_TO("../demo/demo/data/img_crop/%d_%d_imglaplacian_gr.jpg", frame_nbr, i, img_laplacian(frame_crop, 0));
if (SAVE)
SAVE_TO("../demo/demo/data/img_crop/%d_%d_imglaplacian_gr.jpg", frame_nbr, i, img_laplacian(frame_crop, 0));
end_t_segmentation = std::chrono::steady_clock::now();
std::cout << " - TIME laplacian0 (" << frame_nbr << "-" << i << ") : " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t_segmentation - step_t_segmentation).count() << " ms" << std::endl;
step_t_segmentation = end_t_segmentation;
// ret = img_laplacian(frame_crop, 1);
if(SAVE) SAVE_TO("../demo/demo/data/img_crop/%d_%d_imglaplacian_dst.jpg", frame_nbr, i, img_laplacian(frame_crop, 1));
if (SAVE)
SAVE_TO("../demo/demo/data/img_crop/%d_%d_imglaplacian_dst.jpg", frame_nbr, i, img_laplacian(frame_crop, 1));
end_t_segmentation = std::chrono::steady_clock::now();
std::cout << " - TIME laplacian1 (" << frame_nbr << "-" << i << ") : " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t_segmentation - step_t_segmentation).count() << " ms" << std::endl;
step_t_segmentation = end_t_segmentation;
//////
}
void image_find_contours(cv::Mat frame_crop, int frame_nbr, int i)
@@ -337,7 +345,6 @@ void image_find_contours(cv::Mat frame_crop, int frame_nbr, int i)
// Finding contours in your image
// https://docs.opencv.org/3.4/df/d0d/tutorial_find_contours.html
auto step_t_segmentation = std::chrono::steady_clock::now();
auto end_t_segmentation = std::chrono::steady_clock::now();
// plot lines on figure. 3 ways:
@@ -349,7 +356,8 @@ void image_find_contours(cv::Mat frame_crop, int frame_nbr, int i)
// src_gray
cv::Mat img1 = img_laplacian(frame_crop, 0);
cv::Canny(img1, canny_output1, 100, 100 * 2);
if(SAVE) SAVE_TO("../demo/demo/data/img_crop/%d_%d_canny1.jpg", frame_nbr, i, canny_output1);
if (SAVE)
SAVE_TO("../demo/demo/data/img_crop/%d_%d_canny1.jpg", frame_nbr, i, canny_output1);
end_t_segmentation = std::chrono::steady_clock::now();
std::cout << " - TIME canny1 (" << frame_nbr << "-" << i << ") : " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t_segmentation - step_t_segmentation).count() << " ms" << std::endl;
@@ -358,7 +366,8 @@ void image_find_contours(cv::Mat frame_crop, int frame_nbr, int i)
// cv::Mat img2 = img_laplacian(frame_crop, 2);
// cv::Canny(img2, canny_output2, 100, 100*2 );
cv::Canny(img1, canny_output2, 100, 100 * 2);
if(SAVE) SAVE_TO("../demo/demo/data/img_crop/%d_%d_canny2.jpg", frame_nbr, i, canny_output2);
if (SAVE)
SAVE_TO("../demo/demo/data/img_crop/%d_%d_canny2.jpg", frame_nbr, i, canny_output2);
end_t_segmentation = std::chrono::steady_clock::now();
std::cout << " - TIME canny2 (" << frame_nbr << "-" << i << ") : " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t_segmentation - step_t_segmentation).count() << " ms" << std::endl;
@@ -366,7 +375,8 @@ void image_find_contours(cv::Mat frame_crop, int frame_nbr, int i)
// dstx
cv::Mat img3a = img_sobel_abssobel(frame_crop, 0);
cv::Canny(img3a, canny_output3a, 100, 100 * 2);
if(SAVE) SAVE_TO("../demo/demo/data/img_crop/%d_%d_canny3a.jpg", frame_nbr, i, canny_output3a);
if (SAVE)
SAVE_TO("../demo/demo/data/img_crop/%d_%d_canny3a.jpg", frame_nbr, i, canny_output3a);
end_t_segmentation = std::chrono::steady_clock::now();
std::cout << " - TIME canny3a (" << frame_nbr << "-" << i << ") : " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t_segmentation - step_t_segmentation).count() << " ms" << std::endl;
@@ -374,7 +384,8 @@ void image_find_contours(cv::Mat frame_crop, int frame_nbr, int i)
// dsty
cv::Mat img3b = img_sobel_abssobel(frame_crop, 1);
cv::Canny(img3b, canny_output3b, 100, 100 * 2);
if(SAVE) SAVE_TO("../demo/demo/data/img_crop/%d_%d_canny3b.jpg", frame_nbr, i, canny_output3b);
if (SAVE)
SAVE_TO("../demo/demo/data/img_crop/%d_%d_canny3b.jpg", frame_nbr, i, canny_output3b);
end_t_segmentation = std::chrono::steady_clock::now();
std::cout << " - TIME canny3b (" << frame_nbr << "-" << i << ") : " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t_segmentation - step_t_segmentation).count() << " ms" << std::endl;
@@ -382,14 +393,16 @@ void image_find_contours(cv::Mat frame_crop, int frame_nbr, int i)
// 1 line
// contours = find_contours(frame_crop, img1, canny_output1, 1);
if(SAVE) SAVE_TO("../demo/demo/data/img_crop/%d_%d_line1.jpg", frame_nbr, i, find_contours(frame_crop, img1, canny_output1, 1));
if (SAVE)
SAVE_TO("../demo/demo/data/img_crop/%d_%d_line1.jpg", frame_nbr, i, find_contours(frame_crop, img1, canny_output1, 1));
end_t_segmentation = std::chrono::steady_clock::now();
std::cout << " - TIME line1 (" << frame_nbr << "-" << i << ") : " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t_segmentation - step_t_segmentation).count() << " ms" << std::endl;
step_t_segmentation = end_t_segmentation;
// 3 line
// contours = find_contours(frame_crop, img1, canny_output2, 1);
if(SAVE) SAVE_TO("../demo/demo/data/img_crop/%d_%d_line2.jpg", frame_nbr, i, find_contours(frame_crop, img1, canny_output2, 1));
if (SAVE)
SAVE_TO("../demo/demo/data/img_crop/%d_%d_line2.jpg", frame_nbr, i, find_contours(frame_crop, img1, canny_output2, 1));
end_t_segmentation = std::chrono::steady_clock::now();
std::cout << " - TIME line2 (" << frame_nbr << "-" << i << ") : " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t_segmentation - step_t_segmentation).count() << " ms" << std::endl;
@@ -398,7 +411,8 @@ void image_find_contours(cv::Mat frame_crop, int frame_nbr, int i)
cv::Mat img_line = frame_crop.clone();
img_line = find_contours(img_line, img3a, canny_output3a, 1);
img_line = find_contours(img_line, img3b, canny_output3b, 1);
if(SAVE) SAVE_TO("../demo/demo/data/img_crop/%d_%d_line3.jpg", frame_nbr, i, img_line);
if (SAVE)
SAVE_TO("../demo/demo/data/img_crop/%d_%d_line3.jpg", frame_nbr, i, img_line);
end_t_segmentation = std::chrono::steady_clock::now();
std::cout << " - TIME line3 (" << frame_nbr << "-" << i << ") : " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t_segmentation - step_t_segmentation).count() << " ms" << std::endl;
@@ -410,7 +424,6 @@ void image_find_contours(cv::Mat frame_crop, int frame_nbr, int i)
// sprintf(buf_frame_crop_name,"../demo/demo/data/img_crop/%d_%d_line3bis.jpg",frame_nbr, i);
// cv::imwrite(buf_frame_crop_name, img_line);
// cv::Mat canny_output4;
// cv::Mat img4 = img_laplacian(frame_crop, 0);
// cv::Canny(img4, canny_output4, 100, 100*2 );
@@ -418,7 +431,6 @@ void image_find_contours(cv::Mat frame_crop, int frame_nbr, int i)
// cv::imwrite(buf_frame_crop_name, fit_rectangular(frame_crop, img4, canny_output4));
}
void image_saliency(cv::Mat frame_crop, int frame_nbr, int i)
{
// https://github.com/opencv/opencv_contrib/blob/master/modules/saliency/samples/computeSaliency.cpp
@@ -434,7 +446,8 @@ void image_saliency(cv::Mat frame_crop, int frame_nbr, int i)
cv::Mat spect_res = compute_saliency(frame_crop, saliencyAlgorithm, const_molt_mat, 0);
if (!spect_res.empty())
{
if(SAVE) SAVE_TO("../demo/demo/data/img_crop/%d_%d_saliency_SpectralResidual.jpg", frame_nbr, i, spect_res);
if (SAVE)
SAVE_TO("../demo/demo/data/img_crop/%d_%d_saliency_SpectralResidual.jpg", frame_nbr, i, spect_res);
}
else
{
@@ -449,7 +462,8 @@ void image_saliency(cv::Mat frame_crop, int frame_nbr, int i)
spect_res = compute_saliency(frame_crop, saliencyAlgorithm, const_molt_mat, 1);
if (!spect_res.empty())
{
if(SAVE) SAVE_TO("../demo/demo/data/img_crop/%d_%d_saliency_BinarySpectralResidual.jpg", frame_nbr, i, spect_res);
if (SAVE)
SAVE_TO("../demo/demo/data/img_crop/%d_%d_saliency_BinarySpectralResidual.jpg", frame_nbr, i, spect_res);
}
else
{
@@ -465,7 +479,8 @@ void image_saliency(cv::Mat frame_crop, int frame_nbr, int i)
spect_res = compute_saliency(frame_crop, saliencyAlgorithm, const_molt_mat, 0);
if (!spect_res.empty())
{
if(SAVE) SAVE_TO("../demo/demo/data/img_crop/%d_%d_saliency_FineGrained.jpg", frame_nbr, i, spect_res);
if (SAVE)
SAVE_TO("../demo/demo/data/img_crop/%d_%d_saliency_FineGrained.jpg", frame_nbr, i, spect_res);
}
else
{
@@ -509,7 +524,8 @@ void image_saliency(cv::Mat frame_crop, int frame_nbr, int i)
saliencyAlgorithm.dynamicCast<cv::saliency::MotionSaliencyBinWangApr2014>()->init();
cvtColor(frame_sal, frame_sal, cv::COLOR_BGR2GRAY);
saliencyAlgorithm->computeSaliency(frame_sal, saliencyMap);
if(SAVE) SAVE_TO("../demo/demo/data/img_crop/%d_%d_saliency_BinWangApr.jpg", frame_nbr, i, saliencyMap);
if (SAVE)
SAVE_TO("../demo/demo/data/img_crop/%d_%d_saliency_BinWangApr.jpg", frame_nbr, i, saliencyMap);
end_t_segmentation = std::chrono::steady_clock::now();
std::cout << " - TIME BING WANG APR 2014(" << frame_nbr << "-" << i << ") : " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t_segmentation - step_t_segmentation).count() << " ms" << std::endl;
@@ -552,7 +568,8 @@ cv::Mat frame_disparity(cv::Mat pre_frame, cv::Mat frame, int frame_nbr, int i,
}
}
}
if(SAVE) SAVE_TO("../demo/demo/data/img_disparity/%d_%d_dif.jpg", frame_nbr, i, foregroundMask);
if (SAVE)
SAVE_TO("../demo/demo/data/img_disparity/%d_%d_dif.jpg", frame_nbr, i, foregroundMask);
return foregroundMask;
}
@@ -569,7 +586,8 @@ void frame_box_disparity(cv::Mat pre_frame, cv::Mat frame, std::vector <cv::Rect
for (auto r : pre_rois)
{
if(SAVE) SAVE_TO("../demo/demo/data/img_disparity/%d_%d_orig.jpg", frame_nbr, id, pre_frame(r));
if (SAVE)
SAVE_TO("../demo/demo/data/img_disparity/%d_%d_orig.jpg", frame_nbr, id, pre_frame(r));
//resize last roi with a tollerance
dx = r.width / roi_tollerance;
@@ -588,8 +606,10 @@ void frame_box_disparity(cv::Mat pre_frame, cv::Mat frame, std::vector <cv::Rect
//crop pre_frame and current frame
pre_frame_crop = pre_frame(r);
frame_crop = frame(r);
if(SAVE) SAVE_TO("../demo/demo/data/img_disparity/%d_%d_cur.jpg", frame_nbr, id, frame_crop);
if(SAVE) SAVE_TO("../demo/demo/data/img_disparity/%d_%d_pre.jpg", frame_nbr, id, pre_frame_crop);
if (SAVE)
SAVE_TO("../demo/demo/data/img_disparity/%d_%d_cur.jpg", frame_nbr, id, frame_crop);
if (SAVE)
SAVE_TO("../demo/demo/data/img_disparity/%d_%d_pre.jpg", frame_nbr, id, pre_frame_crop);
// difference from two consecutive frame
step_t_segmentation = std::chrono::steady_clock::now();
@@ -612,7 +632,8 @@ void segmentation(cv::Mat pre_frame, cv::Mat frame_crop, int frame_nbr, int i, i
else
sprintf(buf_str, "a box frame");
if(SAVE) SAVE_TO("../demo/demo/data/img_crop/%d_%d.jpg", frame_nbr, i, frame_crop);
if (SAVE)
SAVE_TO("../demo/demo/data/img_crop/%d_%d.jpg", frame_nbr, i, frame_crop);
auto step_t_segmentation = std::chrono::steady_clock::now();
auto end_t_segmentation = std::chrono::steady_clock::now();
@@ -654,6 +675,4 @@ void segmentation(cv::Mat pre_frame, cv::Mat frame_crop, int frame_nbr, int i, i
std::cout << " TIME " << buf_str << ": frame_disparity : " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t_segmentation - step_t_segmentation).count() << " ms" << std::endl;
step_t_segmentation = end_t_segmentation;
}
}
+108
View File
@@ -0,0 +1,108 @@
#include "calibration.h"
void readTiff(char *filename, double *adfGeoTransform)
{
GDALDataset *poDataset;
GDALAllRegister();
poDataset = (GDALDataset *)GDALOpen(filename, GA_ReadOnly);
if (poDataset != NULL)
{
poDataset->GetGeoTransform(adfGeoTransform);
}
}
void readCameraCalibrationYaml(const std::string &cameraCalib, cv::Mat &cameraMat, cv::Mat &distCoeff)
{
YAML::Node config = YAML::LoadFile(cameraCalib);
const YAML::Node &node_test1 = config["camera_matrix"];
float data_cm[9];
for (std::size_t i = 0; i < node_test1["data"].size(); i++)
data_cm[i] = node_test1["data"][i].as<float>();
cv::Mat cameraMat_ = cv::Mat(3, 3, CV_32F, data_cm);
cameraMat = cameraMat_.clone();
std::cout << cameraMat << std::endl;
const YAML::Node &node_test2 = config["distortion_coefficients"];
float data_dc[5];
for (std::size_t i = 0; i < node_test2["data"].size(); i++)
data_dc[i] = node_test2["data"][i].as<float>();
cv::Mat distCoeff_ = cv::Mat(5, 1, CV_32F, data_dc);
distCoeff = distCoeff_.clone();
std::cout << distCoeff << std::endl;
}
void pixel2coord(int x, int y, double &lat, double &lon, double *adfGeoTransform)
{
//Returns global coordinates from pixel x, y coordinates
double xoff, a, b, yoff, d, e;
xoff = adfGeoTransform[0];
a = adfGeoTransform[1];
b = adfGeoTransform[2];
yoff = adfGeoTransform[3];
d = adfGeoTransform[4];
e = adfGeoTransform[5];
//printf("%f %f %f %f %f %f\n",xoff, a, b, yoff, d, e );
lon = a * x + b * y + xoff;
lat = d * x + e * y + yoff;
}
void coord2pixel(double lat, double lon, int &x, int &y, double *adfGeoTransform)
{
x = int(round((lon - adfGeoTransform[0]) / adfGeoTransform[1]));
y = int(round((lat - adfGeoTransform[3]) / adfGeoTransform[5]));
}
void fillMatrix(cv::Mat &H, double *matrix, bool show)
{
double *vals = (double *)H.data;
for (int i = 0; i < 9; i++)
{
vals[i] = matrix[i];
}
if (show)
std::cout << H << "\n";
}
void read_projection_matrix(cv::Mat &H, char *path)
{
FILE *fp;
char *line = NULL;
size_t len = 0;
ssize_t read;
double proj_matrix[9] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
int i = 0;
fp = fopen(path, "r");
if (fp == NULL)
exit(EXIT_FAILURE);
while ((read = getline(&line, &len, fp)) != -1)
{
std::cout << line << std::endl;
std::stringstream ss(line);
while (ss >> proj_matrix[i])
i++;
}
fclose(fp);
fillMatrix(H, proj_matrix);
free(line);
}
void convert_coords(std::vector<ObjCoords> &coords, int x, int y, int detected_class, cv::Mat H, double *adfGeoTransform)
{
double latitude, longitude;
std::vector<cv::Point2f> x_y, ll;
x_y.push_back(cv::Point2f(x, y));
//transform camera pixel to map pixel
cv::perspectiveTransform(x_y, ll, H);
//tranform to map pixel to map gps
pixel2coord(ll[0].x, ll[0].y, latitude, longitude, adfGeoTransform);
ObjCoords coord;
coord.lat_ = latitude;
coord.long_ = longitude;
coord.class_ = detected_class;
coords.push_back(coord);
}
+109
View File
@@ -0,0 +1,109 @@
#include "message.h"
#include "calibration.h"
unsigned long long time_in_ms()
{
struct timeval tv;
gettimeofday(&tv, NULL);
unsigned long long t_stamp_ms = (unsigned long long)(tv.tv_sec) * 1000 + (unsigned long long)(tv.tv_usec) / 1000;
return t_stamp_ms;
}
void addRoadUserfromTracker(const std::vector<Tracker> &trackers, Message *m, geodetic_converter::GeodeticConverter &gc, const cv::Mat &maskOrient, double *adfGeoTransform, cv::Mat H)
{
m->t_stamp_ms = time_in_ms();
m->objects.clear();
double lat, lon, alt;
for (auto t : trackers)
{
if (t.pred_list_.size() > 0)
{
Categories cat;
switch (t.class_)
{
case 0:
cat = Categories::C_person;
break;
case 1:
cat = Categories::C_car;
break;
case 2:
cat = Categories::C_car;
break;
case 3:
cat = Categories::C_bus;
break;
case 4:
cat = Categories::C_motorbike;
break;
case 5:
cat = Categories::C_bycicle;
break;
}
//std::cout << t.pred_list_.size() << std::endl;
gc.enu2Geodetic(t.pred_list_.back().x_, t.pred_list_.back().y_, 0, &lat, &lon, &alt);
int pix_x, pix_y;
coord2pixel(lat, lon, pix_x, pix_y, adfGeoTransform);
// TODO: test correctness - added perspective transform call to converter pix_x and pix_y
// sometimes some values are wrong. float ok?
// std::vector<cv::Point2f> map_p, camera_p;
// std::cout<<"--- pix_x, pix_y: "<<pix_x<<", "<<pix_y<<std::endl;
// map_p.push_back(cv::Point2f(pix_x, pix_y));
// std::cout<<"map_p: "<<map_p<<std::endl;
// //transform camera pixel to map pixel
// cv::perspectiveTransform(map_p, camera_p, H.inv());
// std::cout<<"size H: "<<H.cols<<", "<<H.rows<<std::endl;
// std::cout<<"camera_p: "<<camera_p<<std::endl;
// // TODO: in some cases these lines causes seg fault!
// std::cout<<"y, x :"<<camera_p[0].y<<", "<<camera_p[0].x<<std::endl;
// std::cout<<"size maskorient: "<<maskOrient.cols<<", "<<maskOrient.rows<<std::endl;
// // std::cout<<"vec3b: "<<(cv::Vec3b)(pix_y,pix_x);
// assert (camera_p[0].x < maskOrient.cols);
// assert (camera_p[0].y < maskOrient.rows);
// uint8_t maskOrientPixel = maskOrient.at<cv::Vec3b>(camera_p[0].y,camera_p[0].x)[0];
// std::cout<<"boo: "<<maskOrient.at<cv::Vec3b>(camera_p[0].y,camera_p[0].x)<<std::endl;
// uint8_t orientation;
// if(maskOrientPixel != 0)
// {
// orientation = maskOrientPixel;
// // std::cout<<"orientation given by the mask "<< int(orientation)<<std::endl;
// }
// else
// {
// orientation = uint8_t((int((t.pred_list_.back().yaw_ * 57.29 + 360)) % 360) * 17 / 24);
// //std::cout<<"orientation given by the tracker "<< int(orientation)<<std::endl;
// }
// TODO: to validate -> it works for grayscale image (see demo.cpp, row: "cv::Mat maskOrient = cv::imread(camera->maskFileOrient, 0);")
// TODO: include perspective transform
// std::cout<<"y, x :"<<pix_y<<", "<<pix_x<<std::endl;
// std::cout<<"size maskorient: "<<maskOrient.cols<<", "<<maskOrient.rows<<std::endl;
// std::cout<<"point: "<<(cv::Point)(pix_y,pix_x);
// uint8_t maskOrientPixel = maskOrient.at<uchar>(pix_y,pix_x);
// uint8_t orientation;
// if(maskOrientPixel != 0)
// {
// orientation = maskOrientPixel;
// // std::cout<<"orientation given by the mask "<< int(orientation)<<std::endl;
// }
// else
// {
// orientation = uint8_t((int((t.pred_list_.back().yaw_ * 57.29 + 360)) % 360) * 17 / 24);
// //std::cout<<"orientation given by the tracker "<< int(orientation)<<std::endl;
// }
uint8_t orientation = uint8_t((int((t.pred_list_.back().yaw_ * 57.29 + 360)) % 360) * 17 / 24);
// std::cout<<"orient: "<<unsigned(orientation)<<std::endl;
//std::cout << "lat: " << lat << " lon: " << lon << std::endl;
uint8_t velocity = uint8_t(std::abs(t.pred_list_.back().vel_ * 3.6 / 2));
// std::cout<<"vel: "<<unsigned(velocity)<<std::endl;
RoadUser r{static_cast<float>(lat), static_cast<float>(lon), velocity, orientation, cat};
//std::cout << std::setprecision(10) << r.latitude << " , " << r.longitude << " " << int(r.speed) << " " << int(r.orientation) << " " << r.category << std::endl;
m->objects.push_back(r);
}
}
m->num_objects = m->objects.size();
}
+377
View File
@@ -0,0 +1,377 @@
#include "visualization.h"
/* Thread function to show the updated images
**/
void *show_updates(void *x_void_ptr)
{
cv::namedWindow("original", cv::WINDOW_NORMAL);
cv::namedWindow("detection", cv::WINDOW_NORMAL);
cv::namedWindow("topview", cv::WINDOW_NORMAL);
cv::namedWindow("disparity", cv::WINDOW_NORMAL);
cv::Mat original_loc, detection_loc, topview_loc, disparity_loc;
bool update_o_loc, update_de_loc, update_t_loc, update_di_loc;
while (gRun)
{
TIMER_START
// critical section: copy the struct in local variable
// in this way we can unlock the sem for the main thread
if (updates.mutex_o.try_lock())
{
update_o_loc = updates.update_o;
updates.update_o = false;
if (update_o_loc)
original_loc = updates.original.clone();
updates.mutex_o.unlock();
}
if (updates.mutex_de.try_lock())
{
update_de_loc = updates.update_de;
updates.update_de = false;
if (update_de_loc)
detection_loc = updates.detection.clone();
updates.mutex_de.unlock();
}
if (updates.mutex_t.try_lock())
{
update_t_loc = updates.update_t;
updates.update_t = false;
if (update_t_loc)
topview_loc = updates.topview.clone();
updates.mutex_t.unlock();
}
if (updates.mutex_di.try_lock())
{
update_di_loc = updates.update_di;
updates.update_di = false;
if (update_di_loc)
disparity_loc = updates.disparity.clone();
updates.mutex_di.unlock();
}
if (update_o_loc)
cv::imshow("original", original_loc);
if (update_de_loc)
cv::imshow("detection", detection_loc);
if (update_t_loc)
cv::imshow("topview", topview_loc);
if (update_di_loc)
cv::imshow("disparity", disparity_loc);
cv::waitKey(1);
// usleep(20000); //sleep 20 msec
std::cout << "show_updates: ";
TIMER_STOP
}
return (void *)0;
}
void *originalFrame(void *x_void_ptr)
{
Frame_t *info_show_orig = (Frame_t *)x_void_ptr;
cv::Mat frame_loc;
int frame_nbr_loc = 0;
while (gRun)
{
TIMER_START
// critical section: copy the struct in local variable
// in this way we can unlock the sem for the main thread
info_show_orig->sem_vc.lock();
frame_loc = info_show_orig->frame.clone();
frame_nbr_loc = info_show_orig->frame_nbr;
info_show_orig->sem_vc.unlock();
if (frame_nbr_loc == 0)
{
usleep(1000000);
printf("no frame received\n");
continue;
}
updates.mutex_o.lock();
updates.original = frame_loc.clone();
updates.update_o = true;
updates.mutex_o.unlock();
usleep(10000); //sleep 10 msec
std::cout << "originalFrame: ";
TIMER_STOP
}
return (void *)0;
}
void *detectionFrame(void *x_void_ptr)
{
ModFrame_t *info_show = (ModFrame_t *)x_void_ptr;
double lat, lon, alt;
int pix_x, pix_y;
cv::Mat original_frame_loc;
std::vector<Tracker> trackers;
geodetic_converter::GeodeticConverter gc;
double adfGeoTransform[6];
cv::Mat H;
tk::dnn::Yolo3Detection yolo;
int num_detected;
cv::Mat mask;
// box variable
tk::dnn::box b;
int x0, w, x1, y0, h, y1;
int objClass;
std::string det_class;
;
// float prob;
cv::Scalar intensity;
std::vector<cv::Point2f> map_p, camera_p;
int baseline = 0;
float fontScale = 0.5;
int thickness = 2;
while (gRun)
{
TIMER_START
// critical section: copy the struct in local variable
// in this way we can unlock the sem for the main thread
info_show->sem.lock();
original_frame_loc = info_show->original_frame.clone();
// std::vector<Tracker> trackers;
trackers = info_show->trackers;
// geodetic_converter::GeodeticConverter gc;
gc = info_show->gc;
for (int i = 0; i < 6; i++)
adfGeoTransform[i] = info_show->adfGeoTransform[i];
// cv::Mat H;
H = info_show->H.clone();
yolo = info_show->yolo;
mask = info_show->mask.clone();
info_show->sem.unlock();
if (trackers.empty())
{
usleep(1000000);
printf("no data available\n");
continue;
}
num_detected = yolo.detected.size();
for (int i = 0; i < num_detected; i++)
{
b = yolo.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 = obj_class[b.cl];
// prob = b.prob;
intensity = mask.at<uchar>(cv::Point(int(x0 + b.w / 2), y1));
if (intensity[0] && objClass < 6)
{
//std::cout<<objClass<<" ("<<prob<<"): "<<x0<<" "<<y0<<" "<<x1<<" "<<y1<<"\n";
cv::rectangle(original_frame_loc, cv::Point(x0, y0), cv::Point(x1, y1), yolo.colors[objClass], 2);
// draw label
cv::Size textSize = getTextSize(det_class, cv::FONT_HERSHEY_SIMPLEX, fontScale, thickness, &baseline);
cv::rectangle(original_frame_loc, cv::Point(x0, y0), cv::Point((x0 + textSize.width - 2), (y0 - textSize.height - 2)), yolo.colors[b.cl], -1);
cv::putText(original_frame_loc, det_class, cv::Point(x0, (y0 - (baseline / 2))), cv::FONT_HERSHEY_SIMPLEX, fontScale, cv::Scalar(255, 255, 255), thickness);
}
}
for (auto t : trackers)
{
for (size_t p = 1; p < t.pred_list_.size(); p++)
{
gc.enu2Geodetic(t.pred_list_[p].x_, t.pred_list_[p].y_, 0, &lat, &lon, &alt);
coord2pixel(lat, lon, pix_x, pix_y, adfGeoTransform);
map_p.clear();
camera_p.clear();
map_p.push_back(cv::Point2f(pix_x, pix_y));
//transform camera pixel to map pixel
cv::perspectiveTransform(map_p, camera_p, H.inv());
// std::cout<<"x,y: "<<pix_x<<", "<<pix_y<<std::endl;
// std::cout<<"map_p: "<<map_p<<std::endl;
// std::cout<<"camera_p: "<<camera_p<<std::endl;
// std::cout<<"size original_frame_loc: "<<original_frame_loc.cols<<", "<<original_frame_loc.rows<<std::endl;
// assert (camera_p[0].x < original_frame_loc.cols);
// assert (camera_p[0].y < original_frame_loc.rows);
if (camera_p[0].x < original_frame_loc.cols && camera_p[0].y < original_frame_loc.rows && camera_p[0].x >= 0 && camera_p[0].y >= 0)
cv::circle(original_frame_loc, cv::Point(camera_p[0].x, camera_p[0].y), 3.0, cv::Scalar(t.r_, t.g_, t.b_), CV_FILLED, 8, 0);
}
}
updates.mutex_de.lock();
updates.detection = original_frame_loc.clone();
updates.update_de = true;
updates.mutex_de.unlock();
std::cout << "detectionFrame: ";
TIMER_STOP
}
return (void *)0;
}
void *topviewFrame(void *x_void_ptr)
{
ModFrame_t *info_show = (ModFrame_t *)x_void_ptr;
double lat, lon, alt;
int pix_x, pix_y;
cv::Mat frame_top;
cv::Mat original_frame_top;
// original_frame_top = cv::imread("../demo/demo/data/map/map_geo.jpg");
original_frame_top = cv::imread("../demo/demo/data/map/MASA_4670.png");
// original_frame_top = cv::imread("../demo/demo/data/map/MASA_4670_V.png");
std::vector<Tracker> trackers;
geodetic_converter::GeodeticConverter gc;
double adfGeoTransform[6];
cv::Mat H;
while (gRun)
{
TIMER_START
// critical section: copy the struct in local variable
// in this way we can unlock the sem for the main thread
info_show->sem.lock();
// std::vector<Tracker> trackers;
trackers = info_show->trackers;
// geodetic_converter::GeodeticConverter gc;
gc = info_show->gc;
for (int i = 0; i < 6; i++)
adfGeoTransform[i] = info_show->adfGeoTransform[i];
// cv::Mat H;
H = info_show->H.clone();
info_show->sem.unlock();
if (trackers.empty())
{
usleep(1000000);
printf("no data available\n");
continue;
}
frame_top = original_frame_top.clone();
for (auto t : trackers)
{
for (size_t p = 1; p < t.pred_list_.size(); p++)
{
gc.enu2Geodetic(t.pred_list_[p].x_, t.pred_list_[p].y_, 0, &lat, &lon, &alt);
coord2pixel(lat, lon, pix_x, pix_y, adfGeoTransform);
if (pix_x < frame_top.cols && pix_y < frame_top.rows && pix_x >= 0 && pix_y >= 0)
cv::circle(frame_top, cv::Point(pix_x, pix_y), 7.0, cv::Scalar(t.r_, t.g_, t.b_), CV_FILLED, 8, 0);
}
}
//outputVideo<< frame_top;
// ------------------------------------------------
updates.mutex_t.lock();
updates.topview = frame_top.clone();
updates.update_t = true;
updates.mutex_t.unlock();
std::cout << "topviewFrame: ";
TIMER_STOP
}
return (void *)0;
}
void *disparityFrame(void *x_void_ptr)
{
Frame_t *info_show_disparity = (Frame_t *)x_void_ptr;
bool first_iteration = true;
cv::Mat frame_loc;
int frame_nbr_loc = 0, pre_frame_nbr_loc = 0;
auto start_t = std::chrono::steady_clock::now();
auto step_t = std::chrono::steady_clock::now();
auto end_t = std::chrono::steady_clock::now();
// information for the disparity map
cv::Mat canny, pre_canny, canny_RGB, pre_canny_RGB;
cv::Mat canny_img;
cv::Mat disparity_frame;
while (gRun)
{
start_t = std::chrono::steady_clock::now();
step_t = start_t;
// critical section: copy the struct in local variable
// in this way we can unlock the sem for the main thread
info_show_disparity->sem_vc.lock();
frame_loc = info_show_disparity->frame.clone();
frame_nbr_loc = info_show_disparity->frame_nbr;
info_show_disparity->sem_vc.unlock();
if (frame_nbr_loc == 0)
{
usleep(1000000);
printf("no frame received\n");
continue;
}
// compute frame disparity only in there is a new frame
if (frame_nbr_loc - pre_frame_nbr_loc > 0)
{
pre_frame_nbr_loc = frame_nbr_loc;
//preprocessing frame
step_t = std::chrono::steady_clock::now();
// src_gray
canny_img = img_laplacian(frame_loc, 0);
cv::Canny(canny_img, canny, 100, 100 * 2);
// sprintf(buf_frame_crop_name,"../demo/demo/data/img_disparity/%d_%d_canny.jpg",frame_nbr_loc, 999);
// cv::imwrite(buf_frame_crop_name, canny);
end_t = std::chrono::steady_clock::now();
std::cout << " TIME END pre canny : " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t - step_t).count() << " ms" << std::endl;
step_t = end_t;
// std::cout<<"o: "<<frame_loc.cols<<" - "<<frame_loc.rows<<std::endl;
// std::cout<<"canny: "<<canny.cols<<" - "<<canny.rows<<std::endl;
// std::cout<<"pre: "<<pre_canny.cols<<" - "<<pre_canny.rows<<std::endl;
if (!first_iteration)
{
// backtorgb = cv::cvtColor(pre_canny,cv::COLOR_GRAY2RGB)
cv::cvtColor(pre_canny, pre_canny_RGB, CV_GRAY2RGB);
cv::cvtColor(canny, canny_RGB, CV_GRAY2RGB);
disparity_frame = frame_disparity(pre_canny_RGB, canny_RGB, frame_nbr_loc, 999, 0);
// std::cout<<"size: "<<disparity_frame.rows<<" - "<<disparity_frame.cols<<std::endl;
// if (disparity_frame.rows == 0 || disparity_frame.cols == 0)
// return -1;
// if (disparity_frame.empty())
// { // only fools don't check...
// std::cout << "image not loaded !" << std::endl;
// return -1;
// }
end_t = std::chrono::steady_clock::now();
std::cout << " TIME canny : frame_disparity : " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t - step_t).count() << " ms" << std::endl;
step_t = end_t;
// //--------------------------------
// //frame box disparity on the original image
// step_t_segmentation = std::chrono::steady_clock::now();
// frame_box_disparity(pre_frame, frame, pre_rois, frame_nbr_loc);
// // reset pre_rois for the new roi of the current frame
// // pre_rois.erase(pre_rois.begin(), pre_rois.end());
// end_t_segmentation = std::chrono::steady_clock::now();
// std::cout << " TIME Frame disparity : "<<std::chrono::duration_cast<std::chrono::milliseconds>(end_t_segmentation - step_t_segmentation).count() << " ms"<<std::endl;
// step_t_segmentation = end_t_segmentation;
// //frame box disparity on the preprocessed image
// cv::cvtColor(pre_canny, pre_canny_RGB, CV_GRAY2RGB);
// cv::cvtColor(canny, canny_RGB, CV_GRAY2RGB);
// frame_box_disparity(pre_canny_RGB, canny_RGB, pre_rois, frame_nbr_loc);
// // reset pre_rois for the new roi of the current frame
// pre_rois.erase(pre_rois.begin(), pre_rois.end());
// end_t_segmentation = std::chrono::steady_clock::now();
// std::cout << " TIME Canny Frame disparity : "<<std::chrono::duration_cast<std::chrono::milliseconds>(end_t_segmentation - step_t_segmentation).count() << " ms"<<std::endl;
// step_t_segmentation = end_t_segmentation;
// //---------------------------------
updates.mutex_di.lock();
updates.disparity = disparity_frame.clone();
updates.update_di = true;
updates.mutex_di.unlock();
}
pre_canny = canny.clone();
if (first_iteration)
first_iteration = false;
end_t = std::chrono::steady_clock::now();
std::cout << "disparityFrame : TIME END pre canny : " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t - start_t).count() << " ms" << std::endl;
}
}
return (void *)0;
}