...
 
Commits (3)
......@@ -30,3 +30,9 @@ ehthumbs.db
Icon?
Thumbs.db
*.swp
# Weights
weights/
# Build files
build/
......@@ -13,7 +13,7 @@ include(build_type)
set (WITH_GPU true CACHE BOOL "Use CUDA")
set (WITH_CUDNN true CACHE BOOL "Use libcudnn")
set (WITH_ADDRESS_SANITIZER false CACHE BOOL "Enable address sanitizer. NOTE: only works without cuda/cudnn")
set (default_build_type "Release")
set (default_build_type "Debug")
# print the build type
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
......
......@@ -6,6 +6,7 @@ link_directories (${darknet_cpp_SOURCE_DIR}/src)
add_executable(darknet_cpp_detection darknet_cpp_detection.cpp)
add_executable(darknet_cpp_detection_threaded darknet_cpp_detection_threaded.cpp)
add_executable(darknet_cpp_jetson_detection darknet_cpp_jetson_detection.cpp)
add_executable(darknet_cpp_process_video darknet_cpp_process_video.cpp)
# examples link statically for now
target_link_libraries(darknet_cpp_jetson_detection
......@@ -46,3 +47,17 @@ target_link_libraries(darknet_cpp_detection_threaded
${CUDNN_LIBS}
)
add_dependencies (darknet_cpp_detection_threaded darknet)
target_link_libraries(darknet_cpp_process_video
darknet_cpp_static
"-lboost_filesystem -lboost_system"
${DARKNET_ROOT}/libdarknet.a
"-lpthread"
"-ljsoncpp"
${OpenCV_LIBS}
${CUDA_LIBRARIES}
${CUDA_CUBLAS_LIBRARIES}
${CUDA_curand_LIBRARY}
${CUDNN_LIBS}
)
add_dependencies (darknet_cpp_process_video darknet)
/*
* Author: Maarten Vandersteegen EAVISE
* Description: Darknet C++ detection demo
*/
#include "darknet.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <string>
#include <chrono>
#include <jsoncpp/json/json.h>
#include <iostream>
#include <fstream>
#include <string>
#define DETECTION_THRESHOLD 0.24
#define DETECTION_HIER_THRESHOLD 0.5
#define NMS_THRESHOLD 0.4
void print_status(int value, int total, float fps = -1.0){
// get the status
int scale = 100; // How many character will represent the statusbar
int sValue = floor(float(value)/float(total)*scale);
std::cerr << "\033[A\33[2KT\r"; // This will rewrite the previous line
std::cerr << "[";
for (int i = 0; i<scale; i++){
if (i < sValue){
std::cerr << "-";
}else{
std::cerr << " ";
}
}
std::cerr << "] (" << sValue << "/" << scale << ") [" << value << "/" << total <<"] " ;
if ( fps > 0.0 ){
int t = 0, h = 0, m = 0, s = 0;
t = (total - value) / fps;
h = t /3600;
t = t % 3600;
m = t / 60;
t = t % 60;
s = t;
std::cerr << " (FPS " << std::setprecision(4) << std::fixed << fps << " | ETA "
<< std::setfill('0') << std::setw(2) << h << ":"
<< std::setfill('0') << std::setw(2) << m << ":"
<< std::setfill('0') << std::setw(2) << s << ")";
}
std::cerr << std::endl;
}
void write_json(Json::Value data, std::string output_file){
std::ofstream file_id;
file_id.open(output_file);
Json::StyledWriter styledWriter;
file_id << styledWriter.write(data);
file_id.close();
}
Json::Value detection_to_json(Darknet::Detection detection){
Json::Value json_detection = Json::Value();
json_detection["x"] = detection.x;
json_detection["y"] = detection.y;
json_detection["width"] = detection.width;
json_detection["height"] = detection.height;
json_detection["probability"] = detection.probability;
json_detection["label_index"] = detection.label_index;
json_detection["label"] = detection.label;
return json_detection;
}
Json::Value detections_to_json(std::vector<Darknet::Detection> detections){
Json::Value frame_detections(Json::arrayValue);
for ( auto it = detections.begin(); it != detections.end(); ++it){
frame_detections.append(detection_to_json(*it));
}
return frame_detections;
}
int main(int argc, char *argv[])
{
cv::VideoCapture cap;
cv::Mat cvimage;
Darknet::Image dnimage;
Darknet::ConvertCvBgr8 converter;
Darknet::Detector detector;
std::vector<Darknet::Detection> detections;
Json::Value json_data = Json::Value();
if (argc < 6) {
std::cerr << "Usage: " << argv[0] << " <input_names_file> <input_cfg_file> <input_weights_file> <videofile> <detections_out_file>" << std::endl;
return -1;
}
std::string input_names_file(argv[1]);
std::string input_cfg_file(argv[2]);
std::string input_weights_file(argv[3]);
std::string videofile(argv[4]);
std::string detections_out_file(argv[5]);
if (!cap.open(videofile, cv::CAP_FFMPEG)) {
std::cerr << "Could not open video file: " << videofile << std::endl;
return -1;
}
int image_width = cap.get(cv::CAP_PROP_FRAME_WIDTH);
int image_height = cap.get(cv::CAP_PROP_FRAME_HEIGHT);
if (!detector.setup(input_names_file,
input_cfg_file,
input_weights_file,
NMS_THRESHOLD,
DETECTION_THRESHOLD,
DETECTION_HIER_THRESHOLD,
image_width,
image_height)) {
std::cerr << "Setup failed" << std::endl;
return -1;
}
converter.setup(image_width, image_height, detector.get_width(), detector.get_height());
auto prevTime = std::chrono::system_clock::now();
int i = 0;
int num_frames = cap.get(cv::CAP_PROP_FRAME_COUNT);
std::cerr << "Starting to process: " << videofile << std::endl;
while(1) {
if (!cap.read(cvimage)) {
std::cerr << "Video processing done. " << std::endl;
break;
}
// convert and resize opencv image to darknet image
if (!converter.convert(cvimage, dnimage)) {
std::cerr << "Failed to convert opencv image to darknet image" << std::endl;
return -1;
}
// run detector
if (!detector.detect(dnimage)) {
std::cerr << "Failed to run detector" << std::endl;
return -1;
}
print_status(i, num_frames);
detector.get_detections(detections);
if (detections.size() > 0 ){
json_data[std::to_string(i)] = detections_to_json(detections);
}
auto now = std::chrono::system_clock::now();
std::chrono::duration<double> period = (now - prevTime);
prevTime = now;
print_status(i, num_frames, 1 / period.count() );
i++;
}
write_json(json_data, detections_out_file);
return 0;
}