diff --git a/config/config.yaml b/config/config.yaml new file mode 100644 index 0000000..0a7b143 --- /dev/null +++ b/config/config.yaml @@ -0,0 +1,15 @@ +classes1 : 13 #number of classes +conf_thresh1 : 0.3 #threshold on the condifence of the bbox +net1 : ../demo/yolo4x_fp16.rt +classes2 : 13 #number of classes +conf_thresh2 : 0.3 #threshold on the condifence of the bbox +net2 : ../demo/yolo4x_fp16.rt +classes3 : 13 #number of classes +conf_thresh3 : 0.3 #threshold on the condifence of the bbox +net3 : ../demo/yolo4x_fp16.rt +classes4 : 13 #number of classes +conf_thresh4 : 0.3 #threshold on the condifence of the bbox +net4 : ../demo/yolo4x_fp16.rt +classes5 : 13 #number of classes +conf_thresh5 : 0.3 #threshold on the condifence of the bbox +net5 : ../demo/yolo4x_fp16.rt \ No newline at end of file diff --git a/demo/demo.jpg b/config/demo.jpg similarity index 100% rename from demo/demo.jpg rename to config/demo.jpg diff --git a/demo/config.yaml b/demo/config.yaml deleted file mode 100644 index a8de9ff..0000000 --- a/demo/config.yaml +++ /dev/null @@ -1,7 +0,0 @@ -classes : 13 #number of classes -map_points : 0 #number of recall points (0 for all, 101 for COCO, 11 PascalVOC) -map_levels : 10 #number of IoU step for the AP -map_step : 0.05 #step of IoU -IoU_thresh : 0.5 #starting IoU threshold -conf_thresh : 0.001 #threshold on the condifence of the bbox -verbose : false #print on screen information diff --git a/handler.cpp b/handler.cpp index 984e0f7..1b446a0 100644 --- a/handler.cpp +++ b/handler.cpp @@ -26,6 +26,7 @@ //#include "CenternetDetection.h" //#include "MobilenetDetection.h" #include "evaluation.h" +#include "tkdnn.h" #include #include #include @@ -35,6 +36,12 @@ using namespace cv; #include #include #include "image.h" +void free_image(image m) +{ + if(m.data){ + free(m.data); + } +} image make_empty_image(int w, int h, int c) { image out; @@ -106,21 +113,38 @@ cv::Mat image_to_mat(image img) int h=0; int w=0; int channels; - char ntype = 'y'; - const char *config_filename = "../demo/config.yaml"; - const char * net = "../demo/yolo4_fp32.rt"; + const char *config_filename = "../config/config.yaml"; + const char * net1="../config/yolo4x_fp16.rt"; + const char * net2="../config/yolo4x_fp16.rt"; + const char * net3="../config/yolo4x_fp16.rt"; + const char * net4="../config/yolo4x_fp16.rt"; + const char * net5="../config/yolo4x_fp16.rt"; + int classes1 , classes2 , classes3 , classes4 , classes5,len; char * img_data; - bool show = false; - bool verbose; - int classes, map_points, map_levels ,len; + string ustring; - float map_step, IoU_thresh, conf_thresh; - tk::dnn::Yolo3Detection yolo; - tk::dnn::DetectionNN *detNN; - int n_classes = classes; + float conf_thresh1 , conf_thresh2 , conf_thresh3 , conf_thresh4 , conf_thresh5; + tk::dnn::Yolo3Detection yolo1; + tk::dnn::DetectionNN *detNN1; + tk::dnn::Yolo3Detection yolo2; + tk::dnn::DetectionNN *detNN2; + tk::dnn::Yolo3Detection yolo3; + tk::dnn::DetectionNN *detNN3; + tk::dnn::Yolo3Detection yolo4; + tk::dnn::DetectionNN *detNN4; + unsigned char * sockData; + + std::vector batch_frames; + std::vector batch_dnn_input; + std::vector classesNames1; + std::vector classesNames2; + std::vector classesNames3; + std::vector classesNames4; std::vector images; - std::vector detected_bbox; - tk::dnn::Frame f; + std::vector detected_bbox1; + std::vector detected_bbox2; + std::vector detected_bbox3; + std::vector detected_bbox4; //read parametersi handler::handler(utility::string_t url):m_listener(url) { @@ -132,18 +156,21 @@ string name_from_path(string path) { return path.substr(path.find_last_of("/\\")+1); } - void handler::init_bag(){tk::dnn::readmAPParams(config_filename, classes, map_points, map_levels, map_step, - IoU_thresh, conf_thresh, verbose); + void handler::init_bag(){tk::dnn::readmAPParams(config_filename, classes1,conf_thresh1, classes2,conf_thresh2 + , classes3,conf_thresh3, classes4,conf_thresh4,classes5,conf_thresh5); - - //extract network name from rt path - std::string net_name; - removePathAndExtension(net, net_name); - std::cout<<"Network: "<init(net, n_classes, 1, conf_thresh); + detNN1 = &yolo1; + detNN1->init(net1, classes1, 1, conf_thresh1); + classesNames1=detNN1-> classesNames; + detNN2 = &yolo2; + detNN2->init(net2, classes2, 1, conf_thresh2); + classesNames2=detNN2-> classesNames; + detNN3 = &yolo3; + detNN3->init(net3, classes3, 1, conf_thresh4); + classesNames3=detNN3-> classesNames; + detNN4 = &yolo4; + detNN4->init(net4, classes4, 1, conf_thresh4); + classesNames4=detNN4-> classesNames; return;} @@ -168,52 +195,89 @@ string name_from_path(string path) }).wait(); //printSize(ustring); BOOST_LOG_TRIVIAL(info) << "[" << name_from_path(string(__FILE__)) << " " << __LINE__ << "] " << "Detection Started"; - unsigned char * sockData = (unsigned char *)ustring.c_str(); - unsigned char *data = stbi_load_from_memory(sockData, len, &w, &h, &channels, 0); - im = load_image_file(data, channels, 0, 0, w, h); - // free(data); - //free(sockData); + unsigned char *idata; + sockData = (unsigned char *)ustring.c_str(); + idata = stbi_load_from_memory(sockData, len, &w, &h, &channels, 0); + im = load_image_file(idata, channels, 0, 0, w, h); + batch_dnn_input.clear(); + batch_frames.clear(); gray=image_to_mat(im); // free(im); cv::Mat in[] = {gray, gray,gray}; cv::merge(in, 3, frame); - std::vector batch_frames; + batch_frames.push_back(frame); int height = frame.rows; int width = frame.cols; - std::vector batch_dnn_input; + batch_dnn_input.push_back(frame.clone()); - //inference - detected_bbox.clear(); - detNN->update(batch_dnn_input,1); - detNN->draw(batch_frames); - detected_bbox = detNN->detected; - try{ + + json::value response; vector jsonArray; - // save detections labels - for(auto d:detected_bbox){ - //convert detected bb in the same format as label - /// / / / - tk::dnn::BoundingBox b; - b.x = (d.x + d.w/2) / width; - b.y = (d.y + d.h/2) / height; - b.w = d.w / width; - b.h = d.h / height; - b.prob = d.prob; - b.cl = d.cl; - f.det.push_back(b); - - json::value detection; - detection["label"] = json::value::string(string("battery")); - detection["x"] = json::value::number(d.x); - detection["y"] = json::value::number(d.y); - detection["w"] = json::value::number(d.w); - detection["h"] = json::value::number(d.h); - detection["prob"] = json::value::number(b.prob); + + detected_bbox1.clear(); + detNN1->update(batch_dnn_input,1); + std::cout<detected; + + for(auto d1:detected_bbox1){ + json::value detection; + std::cout<<"1"<<" "<< d1.cl << " "<< d1.prob << " "<< d1.x << " "<< d1.y << " "<< d1.w << " "<< d1.h <<"\n"; + detection["label"] = json::value::string(classesNames1[d1.cl]); + detection["x"] = json::value::number(d1.x); + detection["y"] = json::value::number(d1.y); + detection["w"] = json::value::number(d1.w); + detection["h"] = json::value::number(d1.h); + detection["prob"] = json::value::number(d1.prob); jsonArray.push_back(detection); - std::cout<< d.cl << " "<< d.prob << " "<< b.x << " "<< b.y << " "<< b.w << " "<< b.h <<"\n"; } + detected_bbox2.clear(); + detNN2->update(batch_dnn_input,1); + std::cout<detected; + + for(auto d2:detected_bbox2){ + std::cout<<"2"<<" "<< d2.cl << " "<< d2.prob << " "<< d2.x << " "<< d2.y << " "<< d2.w << " "<< d2.h <<"\n"; + json::value detection; + detection["label"] = json::value::string(classesNames2[d2.cl]); + detection["x"] = json::value::number(d2.x); + detection["y"] = json::value::number(d2.y); + detection["w"] = json::value::number(d2.w); + detection["h"] = json::value::number(d2.h); + detection["prob"] = json::value::number(d2.prob); + jsonArray.push_back(detection); +} + detected_bbox3.clear(); + detNN3->update(batch_dnn_input,1); + detected_bbox3 = detNN3->detected; + for(auto d3:detected_bbox3){ + std::cout<< "3"<<" "<update(batch_dnn_input,1); + detected_bbox4 = detNN4->detected; + + for(auto d4:detected_bbox4){ + std::cout<<"4"<<" "< -#include -#include /* srand, rand */ -#ifdef __linux__ -#include -#endif - -#include -#include "utils.h" - -#include -#include -#include -#include -#include "Yolo3Detection.h" -//#include "CenternetDetection.h" -//#include "MobilenetDetection.h" -#include "evaluation.h" -#include -#include -#include - -uint64_t timeSinceEpochMillisec() { - using namespace std::chrono; - return duration_cast(system_clock::now().time_since_epoch()).count(); -} -int baggage() { - std::cout << timeSinceEpochMillisec() << std::endl; - char ntype = 'y'; - const char *config_filename = "../demo/config.yaml"; - const char * net = "../demo/yolo4_fp32.rt"; - const char * img_path = "../demo/demo.jpg"; - bool show = false; - bool verbose; - int classes, map_points, map_levels; - float map_step, IoU_thresh, conf_thresh; - - //read parameters - tk::dnn::readmAPParams(config_filename, classes, map_points, map_levels, map_step, - IoU_thresh, conf_thresh, verbose); - - - //extract network name from rt path - std::string net_name; - removePathAndExtension(net, net_name); - std::cout<<"Network: "<init(net, n_classes, 1, conf_thresh); - - //read images - // std::ifstream all_labels(labels_path); - std::cout << timeSinceEpochMillisec() << std::endl; - std::string l_filename; - std::vector images; - std::vector detected_bbox; - - std::cout<<"Reading groundtruth and generating detections"< batch_frames; - batch_frames.push_back(frame); - int height = frame.rows; - int width = frame.cols; - -// if(!frame.data) - // break; - std::vector batch_dnn_input; - batch_dnn_input.push_back(frame.clone()); - std::cout<<"test1"<<"\n"; - //inference - detected_bbox.clear(); - detNN->update(batch_dnn_input,1); - detNN->draw(batch_frames); - detected_bbox = detNN->detected; - std::cout<<"test2"<<"\n"; - // save detections labels - for(auto d:detected_bbox){ - //convert detected bb in the same format as label - /// / / / - tk::dnn::BoundingBox b; - b.x = (d.x + d.w/2) / width; - b.y = (d.y + d.h/2) / height; - b.w = d.w / width; - b.h = d.h / height; - b.prob = d.prob; - b.cl = d.cl; - f.det.push_back(b); - - std::cout<< d.cl << " "<< d.prob << " "<< b.x << " "<< b.y << " "<< b.w << " "<< b.h <<"\n"; - - if(show)// draw rectangle for detection - cv::rectangle(batch_frames[0], cv::Point(d.x, d.y), cv::Point(d.x + d.w, d.y + d.h), cv::Scalar(0, 0, 255), 2); - } - //images.push_back(f); - - if(show){ - cv::imshow("detection", batch_frames[0]); - cv::waitKey(0); - } - std::cout << timeSinceEpochMillisec() << std::endl; - return 0; - } - diff --git a/src/Yolo3Detection.cpp b/src/Yolo3Detection.cpp index 0c638e6..efccbd1 100644 --- a/src/Yolo3Detection.cpp +++ b/src/Yolo3Detection.cpp @@ -118,6 +118,7 @@ void Yolo3Detection::postprocess(const int bi, const bool mAP){ float x1 = (b.x+b.w/2.); float y0 = (b.y-b.h/2.); float y1 = (b.y+b.h/2.); + std::cout< - namespace tk { namespace dnn { void Frame::print() const{ @@ -16,17 +15,23 @@ void PR::print(){ std::cout<<"precision: "<(); - map_points = config["map_points"].as(); - map_levels = config["map_levels"].as(); - map_step = config["map_step"].as(); - IoU_thresh = config["IoU_thresh"].as(); - conf_thresh = config["conf_thresh"].as(); - verbose = config["verbose"].as(); + classes1 = config["classes1"].as(); + conf_thresh1 = config["conf_thresh1"].as(); + classes2 = config["classes2"].as(); + conf_thresh2 = config["conf_thresh2"].as(); + classes3 = config["classes3"].as(); + conf_thresh3 = config["conf_thresh3"].as(); + classes4 = config["classes4"].as(); + conf_thresh4 = config["conf_thresh4"].as(); + classes5 = config["classes5"].as(); + conf_thresh5 = config["conf_thresh5"].as(); } /* Credits to https://github.com/AlexeyAB/darknet/blob/master/src/detector.c*/ diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt deleted file mode 100644 index c635261..0000000 --- a/test/CMakeLists.txt +++ /dev/null @@ -1,69 +0,0 @@ -cmake_minimum_required(VERSION 3.5) -set(PROJ_NAME tkDNN) -project (tkDNN) -set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake) -if(UNIX) - -#### -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fPIC -Wno-deprecated-declarations -Wno-unused-variable ") -endif() -if(WIN32) -set(CMAKE_CXX_STANDARD 11) -set(CMAKE_CXX_FLAGS "/O2 /FS /EHsc") -set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) -#add extras for baggage -endif(WIN32) -include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include/tkDNN) - -# project specific flags -if(DEBUG) - add_definitions(-DDEBUG) -endif() - -add_definitions(-DTKDNN_PATH="${CMAKE_CURRENT_SOURCE_DIR}") - -#------------------------------------------------------------------------------- -# CUDA -#------------------------------------------------------------------- -#------------------------------------------------------------------------------- -# External Libraries -#------------------------------------------------------------------------------ -find_package(OpenCV REQUIRED) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DOPENCV") - -# gives pr -#------------------------------------------------------------------------------- -# Build Libraries -#---------------------------------------------------------------------------- -file(GLOB_RECURSE SOURCE_FILES "client_img.cpp") - -add_executable(client ${SOURCE_FILES}) -#set(Casablanca_LIBRARIES "-lboost_log -lboost_log_setup -lboost_thread -lboost_system -lcrypto -lssl -lcpprest -lpthread" ) -set(tkdnn_LIBS ${OpenCV_LIBS}) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") -# Link BaggageAI library' include folder. -include_directories(${OPENCV_INCLUDE_DIRS} ${CMAKE_CXX_FLAGS}) -# Define BaggageAI library' shared library. -#add_library(${BAGGAGEAI_LIB_NAME} SHARED IMPORTED) -# Set a path to BaggageAI library' shared library -#set_property(TARGET ${BAGGAGEAI_LIB_NAME} PROPERTY IMPORTED_LOCATION "${BAGGAGEAI_PATH}/libBaggageAI.so") - -# Link all libraries together. -target_link_libraries(client ${tkdnn_LIBS}) - -#static -#add_executable(demo demo/inf.cpp) -#target_link_libraries(demo tkDNN) - -#------------------------------------------------------------------------------- -# Install -#------------------------------------------------------------------------------- -#if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) -# set (CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install" -# CACHE PATH "default install path" FORCE) -#endif() -message("install dir:" ${CMAKE_INSTALL_PREFIX}) -install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/cmake/" # source directory - DESTINATION "share/tkDNN/cmake/" # target directory -) - diff --git a/test/Makefile b/test/Makefile deleted file mode 100644 index 2d280f8..0000000 --- a/test/Makefile +++ /dev/null @@ -1,13 +0,0 @@ -DEBUG= -g -OPENCV= -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_video - -PKG_CONF_CFLAG= 'pkg-config --cflags opencv --libs opencv' -PKGCONF_LIBS= 'pkg-config --libs opencv' - -client: - clear - g++ -std=c++11 $(PKG_CONF_CFLAG) -o client client.cpp $(PKGCONF_LIBS) - -server: - clear - g++ -std=c++11 $(PKG_CONF_CFLAG) -o server server.cpp $(PKGCONF_LIBS) diff --git a/test/client_img.cpp b/test/client_img.cpp deleted file mode 100644 index 9b6f862..0000000 --- a/test/client_img.cpp +++ /dev/null @@ -1,89 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "opencv2/core/core.hpp" -#include -#include - -#define LOCALHOST "127.0.0.1" -#define PORT 8080 -#define FRAME_WIDTH 608 -#define FRAME_HEIGHT 608 - -using namespace std; -using namespace cv; - -void error(const char *msg) -{ - perror(msg); - exit(0); -} - -int main() -{ - int sockfd, portno, n, imgSize, IM_HEIGHT, IM_WIDTH; - struct sockaddr_in serv_addr; - struct hostent *server; - char buffer[256]; - Mat cameraFeed; - - portno = PORT; - sockfd = socket(AF_INET, SOCK_STREAM, 0); - - if (sockfd < 0) error("ERROR opening socket"); - - server = gethostbyname(LOCALHOST); - - if (server == NULL) { - fprintf(stderr,"ERROR, no such host\n"); - exit(0); - } - - bzero((char *) &serv_addr, sizeof(serv_addr)); - serv_addr.sin_family = AF_INET; - bcopy((char *)server->h_addr, - (char *)&serv_addr.sin_addr.s_addr, - server->h_length); - serv_addr.sin_port = htons(portno); - - if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) - error("ERROR connecting"); - -// VideoCapture capture; - -// capture.open(0); - - // while(true) - // { - /* store image to matrix && test frame */ -// cv::Mat frame=cv::imread(demo.jpg); - cameraFeed = cv::imread("demo.jpg", cv::IMREAD_COLOR); - int height = cameraFeed.rows; - int width = cameraFeed.cols; - std::cout< -#include -#include -#include -#include -#include "opencv2/core/core.hpp" -#include - -#include //socket -#include -#include - -using namespace std; -using namespace cv; - -#define PORT 7200 - -#define FRAME_WIDTH 640 -#define FRAME_HEIGHT 480 - -void error(const char *msg) -{ - perror(msg); - exit(1); -} - -int main() -{ - int sockfd, newsockfd, portno, n, imgSize, bytes=0, IM_HEIGHT, IM_WIDTH;; - socklen_t clilen; - char buffer[256]; - struct sockaddr_in serv_addr, cli_addr; - Mat img; - - sockfd=socket(AF_INET, SOCK_STREAM, 0); - if(sockfd<0) error("ERROR opening socket"); - - bzero((char*)&serv_addr, sizeof(serv_addr)); - portno = PORT; - - serv_addr.sin_family=AF_INET; - serv_addr.sin_addr.s_addr=INADDR_ANY; - serv_addr.sin_port=htons(portno); - - if(bind(sockfd, (struct sockaddr *) &serv_addr, - sizeof(serv_addr))<0) error("ERROR on binding"); - - listen(sockfd,5); - clilen=sizeof(cli_addr); - - newsockfd=accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); - if(newsockfd<0) error("ERROR on accept"); - - uchar sock[3]; - cout << sock <(i,j) = Vec3b(sockData[ptr+0],sockData[ptr+1],sockData[ptr+2]); - ptr=ptr+3; - } - std::cout<<"t3"<<"\n"; - int height = img.cols; - std::cout<