Added HTTP POST sender with detected objects, use: ./darknet detector demo ... -http_post_host webhook.site/898bbd9b-0ddd-49cf-b81d-1f56be98d870

pull/6241/head
AlexeyAB 6 years ago
parent 7ae664f96d
commit fbdac785d2
  1. 2
      src/coco.c
  2. 16
      src/demo.c
  3. 2
      src/demo.h
  4. 3
      src/detector.c
  5. 79
      src/http_stream.cpp
  6. 3
      src/http_stream.h
  7. 4036
      src/httplib.h
  8. 2
      src/yolo.c

@ -396,5 +396,5 @@ void run_coco(int argc, char **argv)
else if(0==strcmp(argv[2], "valid")) validate_coco(cfg, weights); else if(0==strcmp(argv[2], "valid")) validate_coco(cfg, weights);
else if(0==strcmp(argv[2], "recall")) validate_coco_recall(cfg, weights); else if(0==strcmp(argv[2], "recall")) validate_coco_recall(cfg, weights);
else if(0==strcmp(argv[2], "demo")) demo(cfg, weights, thresh, hier_thresh, cam_index, filename, coco_classes, 80, frame_skip, else if(0==strcmp(argv[2], "demo")) demo(cfg, weights, thresh, hier_thresh, cam_index, filename, coco_classes, 80, frame_skip,
prefix, out_filename, mjpeg_port, json_port, dont_show, ext_output, 0, 0); prefix, out_filename, mjpeg_port, json_port, dont_show, ext_output, 0, 0, 0);
} }

@ -105,7 +105,7 @@ double get_wall_time()
} }
void demo(char *cfgfile, char *weightfile, float thresh, float hier_thresh, int cam_index, const char *filename, char **names, int classes, void demo(char *cfgfile, char *weightfile, float thresh, float hier_thresh, int cam_index, const char *filename, char **names, int classes,
int frame_skip, char *prefix, char *out_filename, int mjpeg_port, int json_port, int dont_show, int ext_output, int letter_box_in, int time_limit_sec) int frame_skip, char *prefix, char *out_filename, int mjpeg_port, int json_port, int dont_show, int ext_output, int letter_box_in, int time_limit_sec, char *http_post_host)
{ {
letter_box = letter_box_in; letter_box = letter_box_in;
in_img = det_img = show_img = NULL; in_img = det_img = show_img = NULL;
@ -201,6 +201,7 @@ void demo(char *cfgfile, char *weightfile, float thresh, float hier_thresh, int
//'W', 'M', 'V', '2' //'W', 'M', 'V', '2'
} }
int send_http_post_once = 0;
const double start_time_lim = get_time_point(); const double start_time_lim = get_time_point();
double before = get_wall_time(); double before = get_wall_time();
@ -231,6 +232,17 @@ void demo(char *cfgfile, char *weightfile, float thresh, float hier_thresh, int
send_json(local_dets, local_nboxes, l.classes, demo_names, frame_id, demo_json_port, timeout); send_json(local_dets, local_nboxes, l.classes, demo_names, frame_id, demo_json_port, timeout);
} }
//char *http_post_server = "webhook.site/898bbd9b-0ddd-49cf-b81d-1f56be98d870";
if (http_post_host && !send_http_post_once) {
int timeout = 3; // 3 seconds
int http_post_port = 80; // 443 https, 80 http
if (send_http_post_request(http_post_host, http_post_port, filename,
dets, nboxes, classes, names, frame_id, ext_output, timeout))
{
if (time_limit_sec > 0) send_http_post_once = 1;
}
}
draw_detections_cv_v3(show_img, local_dets, local_nboxes, demo_thresh, demo_names, demo_alphabet, demo_classes, demo_ext_output); draw_detections_cv_v3(show_img, local_dets, local_nboxes, demo_thresh, demo_names, demo_alphabet, demo_classes, demo_ext_output);
free_detections(local_dets, local_nboxes); free_detections(local_dets, local_nboxes);
@ -332,7 +344,7 @@ void demo(char *cfgfile, char *weightfile, float thresh, float hier_thresh, int
} }
#else #else
void demo(char *cfgfile, char *weightfile, float thresh, float hier_thresh, int cam_index, const char *filename, char **names, int classes, void demo(char *cfgfile, char *weightfile, float thresh, float hier_thresh, int cam_index, const char *filename, char **names, int classes,
int frame_skip, char *prefix, char *out_filename, int mjpeg_port, int json_port, int dont_show, int ext_output, int letter_box_in, int time_limit_sec) int frame_skip, char *prefix, char *out_filename, int mjpeg_port, int json_port, int dont_show, int ext_output, int letter_box_in, int time_limit_sec, char *http_post_host)
{ {
fprintf(stderr, "Demo needs OpenCV for webcam images.\n"); fprintf(stderr, "Demo needs OpenCV for webcam images.\n");
} }

@ -6,7 +6,7 @@
extern "C" { extern "C" {
#endif #endif
void demo(char *cfgfile, char *weightfile, float thresh, float hier_thresh, int cam_index, const char *filename, char **names, int classes, void demo(char *cfgfile, char *weightfile, float thresh, float hier_thresh, int cam_index, const char *filename, char **names, int classes,
int frame_skip, char *prefix, char *out_filename, int mjpeg_port, int json_port, int dont_show, int ext_output, int letter_box_in, int time_limit_sec); int frame_skip, char *prefix, char *out_filename, int mjpeg_port, int json_port, int dont_show, int ext_output, int letter_box_in, int time_limit_sec, char *http_post_host);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

@ -1456,6 +1456,7 @@ void run_detector(int argc, char **argv)
int show_imgs = find_arg(argc, argv, "-show_imgs"); int show_imgs = find_arg(argc, argv, "-show_imgs");
int mjpeg_port = find_int_arg(argc, argv, "-mjpeg_port", -1); int mjpeg_port = find_int_arg(argc, argv, "-mjpeg_port", -1);
int json_port = find_int_arg(argc, argv, "-json_port", -1); int json_port = find_int_arg(argc, argv, "-json_port", -1);
char *http_post_host = find_char_arg(argc, argv, "-http_post_host", 0);
int time_limit_sec = find_int_arg(argc, argv, "-time_limit_sec", 0); int time_limit_sec = find_int_arg(argc, argv, "-time_limit_sec", 0);
char *out_filename = find_char_arg(argc, argv, "-out_filename", 0); char *out_filename = find_char_arg(argc, argv, "-out_filename", 0);
char *outfile = find_char_arg(argc, argv, "-out", 0); char *outfile = find_char_arg(argc, argv, "-out", 0);
@ -1524,7 +1525,7 @@ void run_detector(int argc, char **argv)
if (strlen(filename) > 0) if (strlen(filename) > 0)
if (filename[strlen(filename) - 1] == 0x0d) filename[strlen(filename) - 1] = 0; if (filename[strlen(filename) - 1] == 0x0d) filename[strlen(filename) - 1] = 0;
demo(cfg, weights, thresh, hier_thresh, cam_index, filename, names, classes, frame_skip, prefix, out_filename, demo(cfg, weights, thresh, hier_thresh, cam_index, filename, names, classes, frame_skip, prefix, out_filename,
mjpeg_port, json_port, dont_show, ext_output, letter_box, time_limit_sec); mjpeg_port, json_port, dont_show, ext_output, letter_box, time_limit_sec, http_post_host);
free_list_contents_kvp(options); free_list_contents_kvp(options);
free_list(options); free_list(options);

@ -2,7 +2,6 @@
#include "image.h" #include "image.h"
#include "http_stream.h" #include "http_stream.h"
// //
// a single-threaded, multi client(using select), debug webserver - streaming out mjpg. // a single-threaded, multi client(using select), debug webserver - streaming out mjpg.
// on win, _WIN32 has to be defined, must link against ws2_32.lib (socks on linux are for free) // on win, _WIN32 has to be defined, must link against ws2_32.lib (socks on linux are for free)
@ -24,8 +23,13 @@ using std::endl;
#ifndef USE_CMAKE_LIBS #ifndef USE_CMAKE_LIBS
#pragma comment(lib, "ws2_32.lib") #pragma comment(lib, "ws2_32.lib")
#endif #endif
#include "gettimeofday.h" #define WIN32_LEAN_AND_MEAN
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <time.h> #include <time.h>
#include "gettimeofday.h"
#define PORT unsigned long #define PORT unsigned long
#define ADDRPOINTER int* #define ADDRPOINTER int*
struct _INIT_W32DATA struct _INIT_W32DATA
@ -537,6 +541,77 @@ void send_mjpeg(mat_cv* mat, int port, int timeout, int quality)
} }
// ---------------------------------------- // ----------------------------------------
std::string get_system_frame_time_string()
{
std::time_t t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
struct tm tmp_buf;
localtime_s(&tmp_buf, &t);
char buff[256];
std::strftime(buff, 256, "%A %F %T", &tmp_buf);
std::string system_frame_time = buff;
return system_frame_time;
}
// ----------------------------------------
//#define CPPHTTPLIB_OPENSSL_SUPPORT
#include "httplib.h"
// https://webhook.site/
// https://github.com/yhirose/cpp-httplib
// sent POST http request
int send_http_post_request(char *http_post_host, int server_port, char *videosource,
detection *dets, int nboxes, int classes, char **names, long long int frame_id, int ext_output, int timeout)
{
const float thresh = 0.005; // function get_network_boxes() has already filtred dets by actual threshold
std::string message;
for (int i = 0; i < nboxes; ++i) {
char labelstr[4096] = { 0 };
int class_id = -1;
for (int j = 0; j < classes; ++j) {
int show = strncmp(names[j], "dont_show", 9);
if (dets[i].prob[j] > thresh && show) {
if (class_id < 0) {
strcat(labelstr, names[j]);
class_id = j;
char buff[10];
sprintf(buff, " (%2.0f%%)", dets[i].prob[j] * 100);
strcat(labelstr, buff);
}
else {
strcat(labelstr, ", ");
strcat(labelstr, names[j]);
}
printf("%s: %.0f%% ", names[j], dets[i].prob[j] * 100);
}
}
if (class_id >= 0) {
message += std::string(names[class_id]) + std::string(", id: ") + std::to_string(class_id) + "\n";
}
}
if (!message.empty())
{
std::string time = get_system_frame_time_string();
message += "\ntime:\n" + time + "\n";
message += "videosource:\n" + std::string(videosource);
std::string http_post_host_str = http_post_host;
int slash_index = http_post_host_str.find("/");
std::string http_path = http_post_host_str.substr(slash_index, http_post_host_str.length() - slash_index);
http_post_host_str = http_post_host_str.substr(0, slash_index);
// send HTTP-Post request
httplib::Client cli(http_post_host_str.c_str(), server_port, timeout);
auto res = cli.Post(http_path.c_str(), message, "text/plain");
return 1;
}
return 0;
}
#endif // OPENCV #endif // OPENCV

@ -13,6 +13,9 @@ void send_json(detection *dets, int nboxes, int classes, char **names, long long
#ifdef OPENCV #ifdef OPENCV
void send_mjpeg(mat_cv* mat, int port, int timeout, int quality); void send_mjpeg(mat_cv* mat, int port, int timeout, int quality);
int send_http_post_request(char *http_post_host, int server_port, char *videosource,
detection *dets, int nboxes, int classes, char **names, long long int frame_id, int ext_output, int timeout);
#endif // OPENCV #endif // OPENCV
#ifdef __cplusplus #ifdef __cplusplus

File diff suppressed because it is too large Load Diff

@ -359,5 +359,5 @@ void run_yolo(int argc, char **argv)
else if(0==strcmp(argv[2], "valid")) validate_yolo(cfg, weights); else if(0==strcmp(argv[2], "valid")) validate_yolo(cfg, weights);
else if(0==strcmp(argv[2], "recall")) validate_yolo_recall(cfg, weights); else if(0==strcmp(argv[2], "recall")) validate_yolo_recall(cfg, weights);
else if(0==strcmp(argv[2], "demo")) demo(cfg, weights, thresh, hier_thresh, cam_index, filename, voc_names, 20, frame_skip, else if(0==strcmp(argv[2], "demo")) demo(cfg, weights, thresh, hier_thresh, cam_index, filename, voc_names, 20, frame_skip,
prefix, out_filename, mjpeg_port, json_port, dont_show, ext_output, 0, 0); prefix, out_filename, mjpeg_port, json_port, dont_show, ext_output, 0, 0, 0);
} }

Loading…
Cancel
Save