From 455b2fc06ff590914f3a1fb13261510d2b95de2e Mon Sep 17 00:00:00 2001 From: AlexeyAB Date: Thu, 21 Jun 2018 14:45:43 +0300 Subject: [PATCH] Able letterbox for video --- src/demo.c | 14 +++++++++++--- src/image.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/demo.c b/src/demo.c index 6929e0870e1..34627ce1fa9 100644 --- a/src/demo.c +++ b/src/demo.c @@ -52,18 +52,23 @@ void draw_detections_cv(IplImage* show_img, int num, float thresh, box *boxes, f void draw_detections_cv_v3(IplImage* show_img, detection *dets, int num, float thresh, char **names, image **alphabet, int classes, int ext_output); void show_image_cv_ipl(IplImage *disp, const char *name); image get_image_from_stream_resize(CvCapture *cap, int w, int h, int c, IplImage** in_img, int cpp_video_capture, int dont_close); +image get_image_from_stream_letterbox(CvCapture *cap, int w, int h, int c, IplImage** in_img, int cpp_video_capture, int dont_close); int get_stream_fps(CvCapture *cap, int cpp_video_capture); IplImage* in_img; IplImage* det_img; IplImage* show_img; static int flag_exit; +static int letter_box = 0; void *fetch_in_thread(void *ptr) { //in = get_image_from_stream(cap); int dont_close_stream = 0; // set 1 if your IP-camera periodically turns off and turns on video-stream - in_s = get_image_from_stream_resize(cap, net.w, net.h, net.c, &in_img, cpp_video_capture, dont_close_stream); + if(letter_box) + in_s = get_image_from_stream_letterbox(cap, net.w, net.h, net.c, &in_img, cpp_video_capture, dont_close_stream); + else + in_s = get_image_from_stream_resize(cap, net.w, net.h, net.c, &in_img, cpp_video_capture, dont_close_stream); if(!in_s.data){ //error("Stream closed."); printf("Stream closed.\n"); @@ -89,9 +94,12 @@ void *detect_in_thread(void *ptr) free_image(det_s); - int letter = 0; int nboxes = 0; - detection *dets = get_network_boxes(&net, det_s.w, det_s.h, demo_thresh, demo_thresh, 0, 1, &nboxes, letter); + detection *dets = NULL; + if (letter_box) + dets = get_network_boxes(&net, in_img->width, in_img->height, demo_thresh, demo_thresh, 0, 1, &nboxes, 1); // letter box + else + dets = get_network_boxes(&net, det_s.w, det_s.h, demo_thresh, demo_thresh, 0, 1, &nboxes, 0); // resized //if (nms) do_nms_obj(dets, nboxes, l.classes, nms); // bad results if (nms) do_nms_sort(dets, nboxes, l.classes, nms); diff --git a/src/image.c b/src/image.c index dd0098fbeae..a2dc3cacd80 100644 --- a/src/image.c +++ b/src/image.c @@ -1068,6 +1068,37 @@ image get_image_from_stream_resize(CvCapture *cap, int w, int h, int c, IplImage return im; } +image get_image_from_stream_letterbox(CvCapture *cap, int w, int h, int c, IplImage** in_img, int cpp_video_capture, int dont_close) +{ + c = c ? c : 3; + IplImage* src; + if (cpp_video_capture) { + static int once = 1; + if (once) { + once = 0; + do { + src = get_webcam_frame(cap); + if (!src) return make_empty_image(0, 0, 0); + } while (src->width < 1 || src->height < 1 || src->nChannels < 1); + printf("Video stream: %d x %d \n", src->width, src->height); + } + else + src = get_webcam_frame(cap); + } + else src = cvQueryFrame(cap); + + if (cpp_video_capture) + if (!wait_for_stream(cap, src, dont_close)) return make_empty_image(0, 0, 0); + *in_img = cvCreateImage(cvSize(src->width, src->height), IPL_DEPTH_8U, c); + cvResize(src, *in_img, CV_INTER_LINEAR); + image tmp = ipl_to_image(src); + image im = letterbox_image(tmp, w, h); + free_image(tmp); + if (cpp_video_capture) cvReleaseImage(&src); + if (c>1) rgbgr_image(im); + return im; +} + int get_stream_fps(CvCapture *cap, int cpp_video_capture) { int fps = 25;