From patchwork Wed Apr 7 14:17:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Guo, Yejun" X-Patchwork-Id: 26795 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id B4B0C44ADD0 for ; Wed, 7 Apr 2021 17:29:14 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 9144168A543; Wed, 7 Apr 2021 17:29:14 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 20A3D687F3D for ; Wed, 7 Apr 2021 17:29:07 +0300 (EEST) IronPort-SDR: /VyvitjMObD2qEfEs/8f7ztUG4N66fmrnkZt2gIO47uDCVlyKcuBIMc6JWwugZenh3MtXWRwz9 Xo29Ob8ddVNw== X-IronPort-AV: E=McAfee;i="6000,8403,9947"; a="278584266" X-IronPort-AV: E=Sophos;i="5.82,203,1613462400"; d="scan'208";a="278584266" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Apr 2021 07:29:05 -0700 IronPort-SDR: WIYJT71ghbKKj6/Vsi1NmXLDz6i5DnzOAV86konWzFWy304rKFNRKFYvvP+8sKlSPmddPyzhAH gIA9BDNVrpKg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.82,203,1613462400"; d="scan'208";a="415293061" Received: from yguo18-skl-u1604.sh.intel.com ([10.239.159.53]) by fmsmga008.fm.intel.com with ESMTP; 07 Apr 2021 07:29:04 -0700 From: "Guo, Yejun" To: ffmpeg-devel@ffmpeg.org Date: Wed, 7 Apr 2021 22:17:18 +0800 Message-Id: <20210407141723.11527-1-yejun.guo@intel.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH V7 1/6] lavfi/dnn_backend_openvino.c: only allow DFT_PROCESS_FRAME to get output dim X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: yejun.guo@intel.com MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" --- libavfilter/dnn/dnn_backend_openvino.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/libavfilter/dnn/dnn_backend_openvino.c b/libavfilter/dnn/dnn_backend_openvino.c index 9a47d74c15..66845fbd51 100644 --- a/libavfilter/dnn/dnn_backend_openvino.c +++ b/libavfilter/dnn/dnn_backend_openvino.c @@ -493,6 +493,11 @@ static DNNReturnType get_output_ov(void *model, const char *input_name, int inpu IEStatusCode status; input_shapes_t input_shapes; + if (ov_model->model->func_type != DFT_PROCESS_FRAME) { + av_log(ctx, AV_LOG_ERROR, "Get output dim only when processing frame.\n"); + return DNN_ERROR; + } + if (ctx->options.input_resizable) { status = ie_network_get_input_shapes(ov_model->network, &input_shapes); input_shapes.shapes->shape.dims[2] = input_height; From patchwork Wed Apr 7 14:17:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Guo, Yejun" X-Patchwork-Id: 26796 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id E550944ADD0 for ; Wed, 7 Apr 2021 17:29:16 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C492068A5F8; Wed, 7 Apr 2021 17:29:16 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 438E8687F87 for ; Wed, 7 Apr 2021 17:29:09 +0300 (EEST) IronPort-SDR: xlp6aWisUwrBplQVW3Kw7g+C9h6imWUMIDTuyAIJEroo7rkwHM8P/ZT2PtFet0jEamI2MNzw2i F29FAd4kRuSQ== X-IronPort-AV: E=McAfee;i="6000,8403,9947"; a="278584271" X-IronPort-AV: E=Sophos;i="5.82,203,1613462400"; d="scan'208";a="278584271" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Apr 2021 07:29:06 -0700 IronPort-SDR: kNOVwjvX5eueVIvCwgGnfHGiUQAaX3GxdcyKywzeV2prQfJVlhb5IRc9Rx85C1O7YjxT69p9TW S6jhw1h3/hiw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.82,203,1613462400"; d="scan'208";a="415293067" Received: from yguo18-skl-u1604.sh.intel.com ([10.239.159.53]) by fmsmga008.fm.intel.com with ESMTP; 07 Apr 2021 07:29:05 -0700 From: "Guo, Yejun" To: ffmpeg-devel@ffmpeg.org Date: Wed, 7 Apr 2021 22:17:19 +0800 Message-Id: <20210407141723.11527-2-yejun.guo@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210407141723.11527-1-yejun.guo@intel.com> References: <20210407141723.11527-1-yejun.guo@intel.com> Subject: [FFmpeg-devel] [PATCH V7 2/6] lavfi/dnn: refine code for frame pre/proc processing X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: yejun.guo@intel.com MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" --- libavfilter/dnn/dnn_backend_native.c | 8 ++++---- libavfilter/dnn/dnn_backend_openvino.c | 8 ++++---- libavfilter/dnn/dnn_backend_tf.c | 8 ++++---- libavfilter/dnn_filter_common.c | 7 +++++++ libavfilter/dnn_filter_common.h | 1 + libavfilter/dnn_interface.h | 6 ++++-- 6 files changed, 24 insertions(+), 14 deletions(-) diff --git a/libavfilter/dnn/dnn_backend_native.c b/libavfilter/dnn/dnn_backend_native.c index d8ae36c52d..d9762eeaf6 100644 --- a/libavfilter/dnn/dnn_backend_native.c +++ b/libavfilter/dnn/dnn_backend_native.c @@ -310,8 +310,8 @@ static DNNReturnType execute_model_native(const DNNModel *model, const char *inp input.data = oprd->data; input.dt = oprd->data_type; if (do_ioproc) { - if (native_model->model->pre_proc != NULL) { - native_model->model->pre_proc(in_frame, &input, native_model->model->filter_ctx); + if (native_model->model->frame_pre_proc != NULL) { + native_model->model->frame_pre_proc(in_frame, &input, native_model->model->filter_ctx); } else { ff_proc_from_frame_to_dnn(in_frame, &input, native_model->model->func_type, ctx); } @@ -358,8 +358,8 @@ static DNNReturnType execute_model_native(const DNNModel *model, const char *inp output.dt = oprd->data_type; if (do_ioproc) { - if (native_model->model->post_proc != NULL) { - native_model->model->post_proc(out_frame, &output, native_model->model->filter_ctx); + if (native_model->model->frame_post_proc != NULL) { + native_model->model->frame_post_proc(out_frame, &output, native_model->model->filter_ctx); } else { ff_proc_from_dnn_to_frame(out_frame, &output, ctx); } diff --git a/libavfilter/dnn/dnn_backend_openvino.c b/libavfilter/dnn/dnn_backend_openvino.c index 66845fbd51..3bea2d526a 100644 --- a/libavfilter/dnn/dnn_backend_openvino.c +++ b/libavfilter/dnn/dnn_backend_openvino.c @@ -166,8 +166,8 @@ static DNNReturnType fill_model_input_ov(OVModel *ov_model, RequestItem *request for (int i = 0; i < request->task_count; ++i) { task = request->tasks[i]; if (task->do_ioproc) { - if (ov_model->model->pre_proc != NULL) { - ov_model->model->pre_proc(task->in_frame, &input, ov_model->model->filter_ctx); + if (ov_model->model->frame_pre_proc != NULL) { + ov_model->model->frame_pre_proc(task->in_frame, &input, ov_model->model->filter_ctx); } else { ff_proc_from_frame_to_dnn(task->in_frame, &input, ov_model->model->func_type, ctx); } @@ -237,8 +237,8 @@ static void infer_completion_callback(void *args) for (int i = 0; i < request->task_count; ++i) { task = request->tasks[i]; if (task->do_ioproc) { - if (task->ov_model->model->post_proc != NULL) { - task->ov_model->model->post_proc(task->out_frame, &output, task->ov_model->model->filter_ctx); + if (task->ov_model->model->frame_post_proc != NULL) { + task->ov_model->model->frame_post_proc(task->out_frame, &output, task->ov_model->model->filter_ctx); } else { ff_proc_from_dnn_to_frame(task->out_frame, &output, ctx); } diff --git a/libavfilter/dnn/dnn_backend_tf.c b/libavfilter/dnn/dnn_backend_tf.c index c0aa510630..fb799d2b70 100644 --- a/libavfilter/dnn/dnn_backend_tf.c +++ b/libavfilter/dnn/dnn_backend_tf.c @@ -756,8 +756,8 @@ static DNNReturnType execute_model_tf(const DNNModel *model, const char *input_n input.data = (float *)TF_TensorData(input_tensor); if (do_ioproc) { - if (tf_model->model->pre_proc != NULL) { - tf_model->model->pre_proc(in_frame, &input, tf_model->model->filter_ctx); + if (tf_model->model->frame_pre_proc != NULL) { + tf_model->model->frame_pre_proc(in_frame, &input, tf_model->model->filter_ctx); } else { ff_proc_from_frame_to_dnn(in_frame, &input, tf_model->model->func_type, ctx); } @@ -818,8 +818,8 @@ static DNNReturnType execute_model_tf(const DNNModel *model, const char *input_n output.dt = TF_TensorType(output_tensors[i]); if (do_ioproc) { - if (tf_model->model->post_proc != NULL) { - tf_model->model->post_proc(out_frame, &output, tf_model->model->filter_ctx); + if (tf_model->model->frame_post_proc != NULL) { + tf_model->model->frame_post_proc(out_frame, &output, tf_model->model->filter_ctx); } else { ff_proc_from_dnn_to_frame(out_frame, &output, ctx); } diff --git a/libavfilter/dnn_filter_common.c b/libavfilter/dnn_filter_common.c index 413adba406..dc5966332a 100644 --- a/libavfilter/dnn_filter_common.c +++ b/libavfilter/dnn_filter_common.c @@ -64,6 +64,13 @@ int ff_dnn_init(DnnContext *ctx, DNNFunctionType func_type, AVFilterContext *fil return 0; } +int ff_dnn_set_frame_proc(DnnContext *ctx, FramePrePostProc pre_proc, FramePrePostProc post_proc) +{ + ctx->model->frame_pre_proc = pre_proc; + ctx->model->frame_post_proc = post_proc; + return 0; +} + DNNReturnType ff_dnn_get_input(DnnContext *ctx, DNNData *input) { return ctx->model->get_input(ctx->model->model, input, ctx->model_inputname); diff --git a/libavfilter/dnn_filter_common.h b/libavfilter/dnn_filter_common.h index 79c4d3efe3..c611d594dc 100644 --- a/libavfilter/dnn_filter_common.h +++ b/libavfilter/dnn_filter_common.h @@ -48,6 +48,7 @@ typedef struct DnnContext { int ff_dnn_init(DnnContext *ctx, DNNFunctionType func_type, AVFilterContext *filter_ctx); +int ff_dnn_set_frame_proc(DnnContext *ctx, FramePrePostProc pre_proc, FramePrePostProc post_proc); DNNReturnType ff_dnn_get_input(DnnContext *ctx, DNNData *input); DNNReturnType ff_dnn_get_output(DnnContext *ctx, int input_width, int input_height, int *output_width, int *output_height); DNNReturnType ff_dnn_execute_model(DnnContext *ctx, AVFrame *in_frame, AVFrame *out_frame); diff --git a/libavfilter/dnn_interface.h b/libavfilter/dnn_interface.h index d3a0c58a61..3c7846f1a5 100644 --- a/libavfilter/dnn_interface.h +++ b/libavfilter/dnn_interface.h @@ -63,6 +63,8 @@ typedef struct DNNData{ DNNColorOrder order; } DNNData; +typedef int (*FramePrePostProc)(AVFrame *frame, DNNData *model, AVFilterContext *filter_ctx); + typedef struct DNNModel{ // Stores model that can be different for different backends. void *model; @@ -80,10 +82,10 @@ typedef struct DNNModel{ const char *output_name, int *output_width, int *output_height); // set the pre process to transfer data from AVFrame to DNNData // the default implementation within DNN is used if it is not provided by the filter - int (*pre_proc)(AVFrame *frame_in, DNNData *model_input, AVFilterContext *filter_ctx); + FramePrePostProc frame_pre_proc; // set the post process to transfer data from DNNData to AVFrame // the default implementation within DNN is used if it is not provided by the filter - int (*post_proc)(AVFrame *frame_out, DNNData *model_output, AVFilterContext *filter_ctx); + FramePrePostProc frame_post_proc; } DNNModel; // Stores pointers to functions for loading, executing, freeing DNN models for one of the backends. From patchwork Wed Apr 7 14:17:20 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Guo, Yejun" X-Patchwork-Id: 26797 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id D180E44ADD0 for ; Wed, 7 Apr 2021 17:29:18 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id BCE7B68A60C; Wed, 7 Apr 2021 17:29:18 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 2814668A516 for ; Wed, 7 Apr 2021 17:29:10 +0300 (EEST) IronPort-SDR: Syh72uKwvWNXGY1tcBQTTNr2aSSu76tMPzjk42H9wW0Sm4RDpDvbwke8e1rM87/Z+vUY7WVv/A CvK3AKNbsbXw== X-IronPort-AV: E=McAfee;i="6000,8403,9947"; a="278584277" X-IronPort-AV: E=Sophos;i="5.82,203,1613462400"; d="scan'208";a="278584277" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Apr 2021 07:29:07 -0700 IronPort-SDR: 30kXKbAkFz0wbRj5VYUgxJKjctYZOwiWPQOdNkLbReNJM2k2uvYeb34utsyiR9/Z/ueufcPc02 dOPo/FPmY/7A== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.82,203,1613462400"; d="scan'208";a="415293072" Received: from yguo18-skl-u1604.sh.intel.com ([10.239.159.53]) by fmsmga008.fm.intel.com with ESMTP; 07 Apr 2021 07:29:06 -0700 From: "Guo, Yejun" To: ffmpeg-devel@ffmpeg.org Date: Wed, 7 Apr 2021 22:17:20 +0800 Message-Id: <20210407141723.11527-3-yejun.guo@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210407141723.11527-1-yejun.guo@intel.com> References: <20210407141723.11527-1-yejun.guo@intel.com> Subject: [FFmpeg-devel] [PATCH V7 3/6] lavfi/dnn: add post process for detection X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: yejun.guo@intel.com MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" --- libavfilter/dnn/dnn_backend_openvino.c | 30 ++++++++++++++++++++------ libavfilter/dnn_filter_common.c | 6 ++++++ libavfilter/dnn_filter_common.h | 1 + libavfilter/dnn_interface.h | 3 +++ 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/libavfilter/dnn/dnn_backend_openvino.c b/libavfilter/dnn/dnn_backend_openvino.c index 3bea2d526a..0757727a9c 100644 --- a/libavfilter/dnn/dnn_backend_openvino.c +++ b/libavfilter/dnn/dnn_backend_openvino.c @@ -236,16 +236,32 @@ static void infer_completion_callback(void *args) av_assert0(request->task_count >= 1); for (int i = 0; i < request->task_count; ++i) { task = request->tasks[i]; - if (task->do_ioproc) { - if (task->ov_model->model->frame_post_proc != NULL) { - task->ov_model->model->frame_post_proc(task->out_frame, &output, task->ov_model->model->filter_ctx); + + switch (task->ov_model->model->func_type) { + case DFT_PROCESS_FRAME: + if (task->do_ioproc) { + if (task->ov_model->model->frame_post_proc != NULL) { + task->ov_model->model->frame_post_proc(task->out_frame, &output, task->ov_model->model->filter_ctx); + } else { + ff_proc_from_dnn_to_frame(task->out_frame, &output, ctx); + } } else { - ff_proc_from_dnn_to_frame(task->out_frame, &output, ctx); + task->out_frame->width = output.width; + task->out_frame->height = output.height; } - } else { - task->out_frame->width = output.width; - task->out_frame->height = output.height; + break; + case DFT_ANALYTICS_DETECT: + if (!task->ov_model->model->detect_post_proc) { + av_log(ctx, AV_LOG_ERROR, "detect filter needs to provide post proc\n"); + return; + } + task->ov_model->model->detect_post_proc(task->out_frame, &output, 1, task->ov_model->model->filter_ctx); + break; + default: + av_assert0(!"should not reach here"); + break; } + task->done = 1; output.data = (uint8_t *)output.data + output.width * output.height * output.channels * get_datatype_size(output.dt); diff --git a/libavfilter/dnn_filter_common.c b/libavfilter/dnn_filter_common.c index dc5966332a..1b922455a3 100644 --- a/libavfilter/dnn_filter_common.c +++ b/libavfilter/dnn_filter_common.c @@ -71,6 +71,12 @@ int ff_dnn_set_frame_proc(DnnContext *ctx, FramePrePostProc pre_proc, FramePrePo return 0; } +int ff_dnn_set_detect_post_proc(DnnContext *ctx, DetectPostProc post_proc) +{ + ctx->model->detect_post_proc = post_proc; + return 0; +} + DNNReturnType ff_dnn_get_input(DnnContext *ctx, DNNData *input) { return ctx->model->get_input(ctx->model->model, input, ctx->model_inputname); diff --git a/libavfilter/dnn_filter_common.h b/libavfilter/dnn_filter_common.h index c611d594dc..8deb18b39a 100644 --- a/libavfilter/dnn_filter_common.h +++ b/libavfilter/dnn_filter_common.h @@ -49,6 +49,7 @@ typedef struct DnnContext { int ff_dnn_init(DnnContext *ctx, DNNFunctionType func_type, AVFilterContext *filter_ctx); int ff_dnn_set_frame_proc(DnnContext *ctx, FramePrePostProc pre_proc, FramePrePostProc post_proc); +int ff_dnn_set_detect_post_proc(DnnContext *ctx, DetectPostProc post_proc); DNNReturnType ff_dnn_get_input(DnnContext *ctx, DNNData *input); DNNReturnType ff_dnn_get_output(DnnContext *ctx, int input_width, int input_height, int *output_width, int *output_height); DNNReturnType ff_dnn_execute_model(DnnContext *ctx, AVFrame *in_frame, AVFrame *out_frame); diff --git a/libavfilter/dnn_interface.h b/libavfilter/dnn_interface.h index 3c7846f1a5..ae5a488341 100644 --- a/libavfilter/dnn_interface.h +++ b/libavfilter/dnn_interface.h @@ -64,6 +64,7 @@ typedef struct DNNData{ } DNNData; typedef int (*FramePrePostProc)(AVFrame *frame, DNNData *model, AVFilterContext *filter_ctx); +typedef int (*DetectPostProc)(AVFrame *frame, DNNData *output, uint32_t nb, AVFilterContext *filter_ctx); typedef struct DNNModel{ // Stores model that can be different for different backends. @@ -86,6 +87,8 @@ typedef struct DNNModel{ // set the post process to transfer data from DNNData to AVFrame // the default implementation within DNN is used if it is not provided by the filter FramePrePostProc frame_post_proc; + // set the post process to interpret detect result from DNNData + DetectPostProc detect_post_proc; } DNNModel; // Stores pointers to functions for loading, executing, freeing DNN models for one of the backends. From patchwork Wed Apr 7 14:17:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Guo, Yejun" X-Patchwork-Id: 26798 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id C551C44ADD0 for ; Wed, 7 Apr 2021 17:29:20 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id A6BF568A71D; Wed, 7 Apr 2021 17:29:20 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 055AA68A6D2 for ; Wed, 7 Apr 2021 17:29:13 +0300 (EEST) IronPort-SDR: 64mbZMJUEjT9glq33MNTuqznFPEAqPFzUe9P6TJaRbWLIykQRFqOR4OowIzhOhui9uyRhr+oNj 1hC9dTG1g82g== X-IronPort-AV: E=McAfee;i="6000,8403,9947"; a="278584279" X-IronPort-AV: E=Sophos;i="5.82,203,1613462400"; d="scan'208";a="278584279" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Apr 2021 07:29:08 -0700 IronPort-SDR: wNrmhhQezrfpe9BPXzoGZvSeOfM2bPxbVwBgl8Roni2VP6IGbEhH7+25/a68wWS4Gi69WLI24J y7bIwcwURVsw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.82,203,1613462400"; d="scan'208";a="415293081" Received: from yguo18-skl-u1604.sh.intel.com ([10.239.159.53]) by fmsmga008.fm.intel.com with ESMTP; 07 Apr 2021 07:29:07 -0700 From: "Guo, Yejun" To: ffmpeg-devel@ffmpeg.org Date: Wed, 7 Apr 2021 22:17:21 +0800 Message-Id: <20210407141723.11527-4-yejun.guo@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210407141723.11527-1-yejun.guo@intel.com> References: <20210407141723.11527-1-yejun.guo@intel.com> Subject: [FFmpeg-devel] [PATCH V7 4/6] lavu: add side data AV_FRAME_DATA_BOUNDING_BOXES X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: yejun.guo@intel.com MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Guo, Yejun --- doc/APIchanges | 2 + libavutil/Makefile | 2 + libavutil/boundingbox.c | 73 +++++++++++++++++++++++++ libavutil/boundingbox.h | 114 ++++++++++++++++++++++++++++++++++++++++ libavutil/frame.c | 1 + libavutil/frame.h | 7 +++ 6 files changed, 199 insertions(+) create mode 100644 libavutil/boundingbox.c create mode 100644 libavutil/boundingbox.h diff --git a/doc/APIchanges b/doc/APIchanges index 9dfcc97d5c..81d01aac1e 100644 --- a/doc/APIchanges +++ b/doc/APIchanges @@ -14,6 +14,8 @@ libavutil: 2017-10-21 API changes, most recent first: +2021-04-xx - xxxxxxxxxx - lavu 56.xx.100 - frame.h boundingbox.h + Add AV_FRAME_DATA_BOUNDING_BOXES 2021-04-06 - xxxxxxxxxx - lavf 58.78.100 - avformat.h Add avformat_index_get_entries_count(), avformat_index_get_entry(), diff --git a/libavutil/Makefile b/libavutil/Makefile index 27bafe9e12..f6d21bb5ce 100644 --- a/libavutil/Makefile +++ b/libavutil/Makefile @@ -11,6 +11,7 @@ HEADERS = adler32.h \ avutil.h \ base64.h \ blowfish.h \ + boundingbox.h \ bprint.h \ bswap.h \ buffer.h \ @@ -104,6 +105,7 @@ OBJS = adler32.o \ avsscanf.o \ base64.o \ blowfish.o \ + boundingbox.o \ bprint.o \ buffer.o \ cast5.o \ diff --git a/libavutil/boundingbox.c b/libavutil/boundingbox.c new file mode 100644 index 0000000000..881661ec18 --- /dev/null +++ b/libavutil/boundingbox.c @@ -0,0 +1,73 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "boundingbox.h" + +AVBoundingBoxHeader *av_bbox_alloc(uint32_t nb_bboxes, size_t *out_size) +{ + size_t size; + struct { + AVBoundingBoxHeader header; + AVBoundingBox boxes[]; + } *ret; + + size = sizeof(*ret); + if (nb_bboxes > (SIZE_MAX - size) / sizeof(*ret->boxes)) + return NULL; + size += sizeof(*ret->boxes) * nb_bboxes; + + ret = av_mallocz(size); + if (!ret) + return NULL; + + ret->header.nb_bboxes = nb_bboxes; + ret->header.bbox_size = sizeof(*ret->boxes); + ret->header.bboxes_offset = (char *)&ret->boxes - (char *)&ret->header; + + if (out_size) + *out_size = sizeof(*ret); + + return &ret->header; +} + +AVBoundingBoxHeader* av_bbox_create_side_data(AVFrame *frame, uint32_t nb_bboxes) +{ + AVBufferRef *buf; + AVBoundingBoxHeader *header; + size_t size; + + header = av_bbox_alloc(nb_bboxes, &size); + if (!header) + return NULL; + if (size > INT_MAX) { + av_freep(&header); + return NULL; + } + buf = av_buffer_create((uint8_t *)header, size, NULL, NULL, 0); + if (!buf) { + av_freep(&header); + return NULL; + } + + if (!av_frame_new_side_data_from_buf(frame, AV_FRAME_DATA_BOUNDING_BOXES, buf)) { + av_buffer_unref(&buf); + return NULL; + } + + return header; +} diff --git a/libavutil/boundingbox.h b/libavutil/boundingbox.h new file mode 100644 index 0000000000..2b77c6c0dc --- /dev/null +++ b/libavutil/boundingbox.h @@ -0,0 +1,114 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef AVUTIL_BOUNDINGBOX_H +#define AVUTIL_BOUNDINGBOX_H + +#include "rational.h" +#include "avassert.h" +#include "frame.h" + +typedef struct AVBoundingBox { + /** + * Distance in pixels from the top edge of the frame to top + * and bottom, and from the left edge of the frame to left and + * right, defining the bounding box. + */ + int top; + int left; + int bottom; + int right; + +#define AV_BBOX_LABEL_NAME_MAX_SIZE 32 + + /** + * Detect result with confidence + */ + char detect_label[AV_BBOX_LABEL_NAME_MAX_SIZE]; + AVRational detect_confidence; + + /** + * At most 4 classifications based on the detected bounding box. + * For example, we can get max 4 different attributes with 4 different + * DNN models on one bounding box. + * classify_count is zero if no classification. + */ +#define AV_NUM_BBOX_CLASSIFY 4 + uint32_t classify_count; + char classify_labels[AV_NUM_BBOX_CLASSIFY][AV_BBOX_LABEL_NAME_MAX_SIZE]; + AVRational classify_confidences[AV_NUM_BBOX_CLASSIFY]; +} AVBoundingBox; + +typedef struct AVBoundingBoxHeader { + /** + * Information about how the bounding box is generated. + * for example, the DNN model name. + */ + char source[128]; + + /** + * The size of frame when it is detected. + */ + int frame_width; + int frame_height; + + /** + * Number of bounding boxes in the array. + */ + uint32_t nb_bboxes; + + /** + * Offset in bytes from the beginning of this structure at which + * the array of bounding boxes starts. + */ + size_t bboxes_offset; + + /** + * Size of each bounding box in bytes. + */ + size_t bbox_size; +} AVBoundingBoxHeader; + +/* + * Get the bounding box at the specified {@code idx}. Must be between 0 and nb_bboxes. + */ +static av_always_inline AVBoundingBox* +av_get_bounding_box(const AVBoundingBoxHeader *header, unsigned int idx) +{ + av_assert0(idx < header->nb_bboxes); + return (AVBoundingBox *)((uint8_t *)header + header->bboxes_offset + + idx * header->bbox_size); +} + +/** + * Allocates memory for AVBoundingBoxHeader, plus an array of {@code nb_bboxes} + * AVBoundingBox, and initializes the variables. + * Can be freed with a normal av_free() call. + * + * @param out_size if non-NULL, the size in bytes of the resulting data array is + * written here. + */ +AVBoundingBoxHeader *av_bbox_alloc(uint32_t nb_bboxes, size_t *out_size); + +/** + * Allocates memory for AVBoundingBoxHeader, plus an array of {@code nb_bboxes} + * AVBoundingBox, in the given AVFrame {@code frame} as AVFrameSideData of type + * AV_FRAME_DATA_BOUNDING_BOXES and initializes the variables. + */ +AVBoundingBoxHeader* av_bbox_create_side_data(AVFrame *frame, uint32_t nb_bboxes); +#endif diff --git a/libavutil/frame.c b/libavutil/frame.c index fefb2b69c0..d8e86263af 100644 --- a/libavutil/frame.c +++ b/libavutil/frame.c @@ -853,6 +853,7 @@ const char *av_frame_side_data_name(enum AVFrameSideDataType type) case AV_FRAME_DATA_VIDEO_ENC_PARAMS: return "Video encoding parameters"; case AV_FRAME_DATA_SEI_UNREGISTERED: return "H.26[45] User Data Unregistered SEI message"; case AV_FRAME_DATA_FILM_GRAIN_PARAMS: return "Film grain parameters"; + case AV_FRAME_DATA_BOUNDING_BOXES: return "Bounding boxes"; } return NULL; } diff --git a/libavutil/frame.h b/libavutil/frame.h index a5ed91b20a..41e22de02a 100644 --- a/libavutil/frame.h +++ b/libavutil/frame.h @@ -198,6 +198,13 @@ enum AVFrameSideDataType { * Must be present for every frame which should have film grain applied. */ AV_FRAME_DATA_FILM_GRAIN_PARAMS, + + /** + * Bounding boxes for object detection and classification, the data is a AVBoundingBoxHeader + * followed with an array of AVBoudingBox, and AVBoundingBoxHeader.nb_bboxes is the number + * of array element. + */ + AV_FRAME_DATA_BOUNDING_BOXES, }; enum AVActiveFormatDescription { From patchwork Wed Apr 7 14:17:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Guo, Yejun" X-Patchwork-Id: 26799 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 4119B44ADD0 for ; Wed, 7 Apr 2021 17:29:22 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 288E668A70F; Wed, 7 Apr 2021 17:29:22 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 787D968A628 for ; Wed, 7 Apr 2021 17:29:15 +0300 (EEST) IronPort-SDR: WXJ8Y05n7DwL8qhF5u6v0sXS7cIlTM/+x+FZkbyyZnXraeKS8xA2eumYLrV7B+9Yu/LmATWsqr i2jsBAU0ANEQ== X-IronPort-AV: E=McAfee;i="6000,8403,9947"; a="278584286" X-IronPort-AV: E=Sophos;i="5.82,203,1613462400"; d="scan'208";a="278584286" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Apr 2021 07:29:09 -0700 IronPort-SDR: 128w5KKMU/jxZxqgotVXqv/12cMb8vxT8AXwJsydC+cdxauBqteBrsXaHY5mi7e/Li+M9bX921 OoH/CvtkTsNA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.82,203,1613462400"; d="scan'208";a="415293099" Received: from yguo18-skl-u1604.sh.intel.com ([10.239.159.53]) by fmsmga008.fm.intel.com with ESMTP; 07 Apr 2021 07:29:08 -0700 From: "Guo, Yejun" To: ffmpeg-devel@ffmpeg.org Date: Wed, 7 Apr 2021 22:17:22 +0800 Message-Id: <20210407141723.11527-5-yejun.guo@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210407141723.11527-1-yejun.guo@intel.com> References: <20210407141723.11527-1-yejun.guo@intel.com> Subject: [FFmpeg-devel] [PATCH V7 5/6] lavfi: show side data of bounding box X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: yejun.guo@intel.com MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Guo, Yejun --- libavfilter/f_sidedata.c | 2 ++ libavfilter/vf_showinfo.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/libavfilter/f_sidedata.c b/libavfilter/f_sidedata.c index 3757723375..912fb0d881 100644 --- a/libavfilter/f_sidedata.c +++ b/libavfilter/f_sidedata.c @@ -71,6 +71,7 @@ static const AVOption filt_name##_options[] = { \ { "S12M_TIMECOD", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_S12M_TIMECODE }, 0, 0, FLAGS, "type" }, \ { "DYNAMIC_HDR_PLUS", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_DYNAMIC_HDR_PLUS }, 0, 0, FLAGS, "type" }, \ { "REGIONS_OF_INTEREST", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_REGIONS_OF_INTEREST }, 0, 0, FLAGS, "type" }, \ + { "BOUNDING_BOXES", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_BOUNDING_BOXES }, 0, 0, FLAGS, "type" }, \ { "SEI_UNREGISTERED", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_SEI_UNREGISTERED }, 0, 0, FLAGS, "type" }, \ { NULL } \ } @@ -100,6 +101,7 @@ static const AVOption filt_name##_options[] = { \ { "S12M_TIMECOD", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_S12M_TIMECODE }, 0, 0, FLAGS, "type" }, \ { "DYNAMIC_HDR_PLUS", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_DYNAMIC_HDR_PLUS }, 0, 0, FLAGS, "type" }, \ { "REGIONS_OF_INTEREST", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_REGIONS_OF_INTEREST }, 0, 0, FLAGS, "type" }, \ + { "BOUNDING_BOXES", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_BOUNDING_BOXES }, 0, 0, FLAGS, "type" }, \ { "SEI_UNREGISTERED", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_SEI_UNREGISTERED }, 0, 0, FLAGS, "type" }, \ { NULL } \ } diff --git a/libavfilter/vf_showinfo.c b/libavfilter/vf_showinfo.c index 6208892005..20a050d721 100644 --- a/libavfilter/vf_showinfo.c +++ b/libavfilter/vf_showinfo.c @@ -38,6 +38,7 @@ #include "libavutil/timecode.h" #include "libavutil/mastering_display_metadata.h" #include "libavutil/video_enc_params.h" +#include "libavutil/boundingbox.h" #include "avfilter.h" #include "internal.h" @@ -153,6 +154,32 @@ static void dump_roi(AVFilterContext *ctx, const AVFrameSideData *sd) } } +static void dump_boundingbox(AVFilterContext *ctx, const AVFrameSideData *sd) +{ + int nb_bboxes; + const AVBoundingBoxHeader *header; + const AVBoundingBox *bbox; + + header = (const AVBoundingBoxHeader *)sd->data; + nb_bboxes = header->nb_bboxes; + av_log(ctx, AV_LOG_INFO, "bounding boxes:\n"); + av_log(ctx, AV_LOG_INFO, "source: %s\n", header->source); + + for (int i = 0; i < nb_bboxes; i++) { + bbox = av_get_bounding_box(header, i); + av_log(ctx, AV_LOG_INFO, "index: %d,\tregion: (%d, %d) -> (%d, %d), label: %s, confidence: %d/%d.\n", + i, bbox->left, bbox->top, bbox->right, bbox->bottom, + bbox->detect_label, bbox->detect_confidence.num, bbox->detect_confidence.den); + if (bbox->classify_count > 0) { + for (int j = 0; j < bbox->classify_count; j++) { + av_log(ctx, AV_LOG_INFO, "\t\tclassify: label: %s, confidence: %d/%d.\n", + bbox->classify_labels[j], bbox->classify_confidences[j].num, bbox->classify_confidences[j].den); + } + } + bbox++; + } +} + static void dump_mastering_display(AVFilterContext *ctx, const AVFrameSideData *sd) { const AVMasteringDisplayMetadata *mastering_display; @@ -494,6 +521,9 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *frame) case AV_FRAME_DATA_REGIONS_OF_INTEREST: dump_roi(ctx, sd); break; + case AV_FRAME_DATA_BOUNDING_BOXES: + dump_boundingbox(ctx, sd); + break; case AV_FRAME_DATA_MASTERING_DISPLAY_METADATA: dump_mastering_display(ctx, sd); break; From patchwork Wed Apr 7 14:17:23 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Guo, Yejun" X-Patchwork-Id: 26800 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 6089E44ADD0 for ; Wed, 7 Apr 2021 17:29:40 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 42A8968A7B6; Wed, 7 Apr 2021 17:29:40 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 5485F68A6D2 for ; Wed, 7 Apr 2021 17:29:32 +0300 (EEST) IronPort-SDR: /unXeHPkbmae6Hhse9NqLAcUuxks/qVtX2V0hX5jwd9F9r9AdE0u5w32eM0NRzyoIzz93wU7Ij 7Wne6/6tAuoA== X-IronPort-AV: E=McAfee;i="6000,8403,9947"; a="278584289" X-IronPort-AV: E=Sophos;i="5.82,203,1613462400"; d="scan'208";a="278584289" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Apr 2021 07:29:10 -0700 IronPort-SDR: NS/lp1juMl++gl+6C0K6Uzbs3IykdrldlH4ISPIsjc7SNRgrPZCBwqv/AFRpMXdBQqOid4CpqC c7c0bUv25q9A== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.82,203,1613462400"; d="scan'208";a="415293109" Received: from yguo18-skl-u1604.sh.intel.com ([10.239.159.53]) by fmsmga008.fm.intel.com with ESMTP; 07 Apr 2021 07:29:09 -0700 From: "Guo, Yejun" To: ffmpeg-devel@ffmpeg.org Date: Wed, 7 Apr 2021 22:17:23 +0800 Message-Id: <20210407141723.11527-6-yejun.guo@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210407141723.11527-1-yejun.guo@intel.com> References: <20210407141723.11527-1-yejun.guo@intel.com> Subject: [FFmpeg-devel] [PATCH V7 6/6] lavfi: add filter dnn_detect for object detection X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: yejun.guo@intel.com MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Below are the example steps to do object detection: 1. download and install l_openvino_toolkit_p_2021.1.110.tgz from https://software.intel.com/content/www/us/en/develop/tools/openvino-toolkit/download.html or, we can get source code (tag 2021.1), build and install. 2. export LD_LIBRARY_PATH with openvino settings, for example: .../deployment_tools/inference_engine/lib/intel64/:.../deployment_tools/inference_engine/external/tbb/lib/ 3. rebuild ffmpeg from source code with configure option: --enable-libopenvino --extra-cflags='-I.../deployment_tools/inference_engine/include/' --extra-ldflags='-L.../deployment_tools/inference_engine/lib/intel64' 4. download model files and test image wget https://github.com/guoyejun/ffmpeg_dnn/raw/main/models/openvino/2021.1/face-detection-adas-0001.bin wget https://github.com/guoyejun/ffmpeg_dnn/raw/main/models/openvino/2021.1/face-detection-adas-0001.xml wget https://github.com/guoyejun/ffmpeg_dnn/raw/main/models/openvino/2021.1/face-detection-adas-0001.label wget https://github.com/guoyejun/ffmpeg_dnn/raw/main/images/cici.jpg 5. run ffmpeg with: ./ffmpeg -i cici.jpg -vf dnn_detect=dnn_backend=openvino:model=face-detection-adas-0001.xml:input=data:output=detection_out:confidence=0.6:labels=face-detection-adas-0001.label,showinfo -f null - We'll see the detect result as below: [Parsed_showinfo_1 @ 0x55978db02dc0] side data - bounding boxes: [Parsed_showinfo_1 @ 0x55978db02dc0] source: face-detection-adas-0001.xml [Parsed_showinfo_1 @ 0x55978db02dc0] index: 0, region: (1005, 813) -> (1086, 905), label: face, confidence: 10000/10000. [Parsed_showinfo_1 @ 0x55978db02dc0] index: 1, region: (888, 839) -> (967, 926), label: face, confidence: 6917/10000. There are two faces detected with confidence 100% and 69.17%. Signed-off-by: Guo, Yejun --- configure | 1 + doc/filters.texi | 40 +++ libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/dnn/dnn_backend_openvino.c | 12 + libavfilter/vf_dnn_detect.c | 423 +++++++++++++++++++++++++ 6 files changed, 478 insertions(+) create mode 100644 libavfilter/vf_dnn_detect.c diff --git a/configure b/configure index d7a3f507e8..cc1013fb1d 100755 --- a/configure +++ b/configure @@ -3555,6 +3555,7 @@ derain_filter_select="dnn" deshake_filter_select="pixelutils" deshake_opencl_filter_deps="opencl" dilation_opencl_filter_deps="opencl" +dnn_detect_filter_select="dnn" dnn_processing_filter_select="dnn" drawtext_filter_deps="libfreetype" drawtext_filter_suggest="libfontconfig libfribidi" diff --git a/doc/filters.texi b/doc/filters.texi index 5e35fa6467..68f17dd563 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -10127,6 +10127,46 @@ ffmpeg -i INPUT -f lavfi -i nullsrc=hd720,geq='r=128+80*(sin(sqrt((X-W/2)*(X-W/2 @end example @end itemize +@section dnn_detect + +Do object detection with deep neural networks. + +The filter accepts the following options: + +@table @option +@item dnn_backend +Specify which DNN backend to use for model loading and execution. This option accepts +only openvino now, tensorflow backends will be added. + +@item model +Set path to model file specifying network architecture and its parameters. +Note that different backends use different file formats. + +@item input +Set the input name of the dnn network. + +@item output +Set the output name of the dnn network. + +@item confidence +Set the confidence threshold (default: 0.5). + +@item labels +Set path to label file specifying the mapping between label id and name. +Each label name is written in one line, tailing spaces and empty lines are skipped. +The first line is the name of label id 0 (usually it is 'background'), +and the second line is the name of label id 1, etc. +The label id is considered as name if the label file is not provided. + +@item backend_configs +Set the configs to be passed into backend + +@item async +use DNN async execution if set (default: set), +roll back to sync execution if the backend does not support async. + +@end table + @anchor{dnn_processing} @section dnn_processing diff --git a/libavfilter/Makefile b/libavfilter/Makefile index b2c254ea67..b77f2276a4 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -245,6 +245,7 @@ OBJS-$(CONFIG_DILATION_FILTER) += vf_neighbor.o OBJS-$(CONFIG_DILATION_OPENCL_FILTER) += vf_neighbor_opencl.o opencl.o \ opencl/neighbor.o OBJS-$(CONFIG_DISPLACE_FILTER) += vf_displace.o framesync.o +OBJS-$(CONFIG_DNN_DETECT_FILTER) += vf_dnn_detect.o OBJS-$(CONFIG_DNN_PROCESSING_FILTER) += vf_dnn_processing.o OBJS-$(CONFIG_DOUBLEWEAVE_FILTER) += vf_weave.o OBJS-$(CONFIG_DRAWBOX_FILTER) += vf_drawbox.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 0872c6e0f2..0d2bf7bbee 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -230,6 +230,7 @@ extern AVFilter ff_vf_detelecine; extern AVFilter ff_vf_dilation; extern AVFilter ff_vf_dilation_opencl; extern AVFilter ff_vf_displace; +extern AVFilter ff_vf_dnn_detect; extern AVFilter ff_vf_dnn_processing; extern AVFilter ff_vf_doubleweave; extern AVFilter ff_vf_drawbox; diff --git a/libavfilter/dnn/dnn_backend_openvino.c b/libavfilter/dnn/dnn_backend_openvino.c index 0757727a9c..94fc3331cb 100644 --- a/libavfilter/dnn/dnn_backend_openvino.c +++ b/libavfilter/dnn/dnn_backend_openvino.c @@ -646,6 +646,12 @@ DNNReturnType ff_dnn_execute_model_ov(const DNNModel *model, const char *input_n return DNN_ERROR; } + if (model->func_type != DFT_PROCESS_FRAME) { + if (!out_frame) { + out_frame = in_frame; + } + } + if (nb_output != 1) { // currently, the filter does not need multiple outputs, // so we just pending the support until we really need it. @@ -699,6 +705,12 @@ DNNReturnType ff_dnn_execute_model_async_ov(const DNNModel *model, const char *i return DNN_ERROR; } + if (model->func_type != DFT_PROCESS_FRAME) { + if (!out_frame) { + out_frame = in_frame; + } + } + if (!ov_model->exe_network) { if (init_model_ov(ov_model, input_name, output_names[0]) != DNN_SUCCESS) { av_log(ctx, AV_LOG_ERROR, "Failed init OpenVINO exectuable network or inference request\n"); diff --git a/libavfilter/vf_dnn_detect.c b/libavfilter/vf_dnn_detect.c new file mode 100644 index 0000000000..1b8b604cfa --- /dev/null +++ b/libavfilter/vf_dnn_detect.c @@ -0,0 +1,423 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file + * implementing an object detecting filter using deep learning networks. + */ + +#include "libavformat/avio.h" +#include "libavutil/opt.h" +#include "libavutil/pixdesc.h" +#include "libavutil/avassert.h" +#include "libavutil/imgutils.h" +#include "filters.h" +#include "dnn_filter_common.h" +#include "formats.h" +#include "internal.h" +#include "libavutil/time.h" +#include "libavutil/avstring.h" +#include "libavutil/boundingbox.h" + +typedef struct DnnDetectContext { + const AVClass *class; + DnnContext dnnctx; + float confidence; + char *labels_filename; + char **labels; + int label_count; +} DnnDetectContext; + +#define OFFSET(x) offsetof(DnnDetectContext, dnnctx.x) +#define OFFSET2(x) offsetof(DnnDetectContext, x) +#define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM +static const AVOption dnn_detect_options[] = { + { "dnn_backend", "DNN backend", OFFSET(backend_type), AV_OPT_TYPE_INT, { .i64 = 2 }, INT_MIN, INT_MAX, FLAGS, "backend" }, +#if (CONFIG_LIBOPENVINO == 1) + { "openvino", "openvino backend flag", 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, 0, 0, FLAGS, "backend" }, +#endif + DNN_COMMON_OPTIONS + { "confidence", "threshold of confidence", OFFSET2(confidence), AV_OPT_TYPE_FLOAT, { .dbl = 0.5 }, 0, 1, FLAGS}, + { "labels", "path to labels file", OFFSET2(labels_filename), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS }, + { NULL } +}; + +AVFILTER_DEFINE_CLASS(dnn_detect); + +static int dnn_detect_post_proc(AVFrame *frame, DNNData *output, uint32_t nb, AVFilterContext *filter_ctx) +{ + DnnDetectContext *ctx = filter_ctx->priv; + float conf_threshold = ctx->confidence; + int proposal_count = output->height; + int detect_size = output->width; + float *detections = output->data; + int nb_bboxes = 0; + AVFrameSideData *sd; + AVBoundingBox *bbox; + AVBoundingBoxHeader *header; + + sd = av_frame_get_side_data(frame, AV_FRAME_DATA_BOUNDING_BOXES); + if (sd) { + av_log(filter_ctx, AV_LOG_ERROR, "already have dnn bounding boxes in side data.\n"); + return -1; + } + + for (int i = 0; i < proposal_count; ++i) { + float conf = detections[i * detect_size + 2]; + if (conf < conf_threshold) { + continue; + } + nb_bboxes++; + } + + if (nb_bboxes == 0) { + av_log(filter_ctx, AV_LOG_VERBOSE, "nothing detected in this frame.\n"); + return 0; + } + + header = av_bbox_create_side_data(frame, nb_bboxes); + if (!header) { + av_log(filter_ctx, AV_LOG_ERROR, "failed to create side data with %d bounding boxes\n", nb_bboxes); + return -1; + } + + av_strlcpy(header->source, ctx->dnnctx.model_filename, sizeof(header->source)); + header->frame_width = frame->width; + header->frame_height = frame->height; + + for (int i = 0; i < proposal_count; ++i) { + int av_unused image_id = (int)detections[i * detect_size + 0]; + int label_id = (int)detections[i * detect_size + 1]; + float conf = detections[i * detect_size + 2]; + float x0 = detections[i * detect_size + 3]; + float y0 = detections[i * detect_size + 4]; + float x1 = detections[i * detect_size + 5]; + float y1 = detections[i * detect_size + 6]; + + bbox = av_get_bounding_box(header, i); + + if (conf < conf_threshold) { + continue; + } + + bbox->left = (int)(x0 * frame->width); + bbox->right = (int)(x1 * frame->width); + bbox->top = (int)(y0 * frame->height); + bbox->bottom = (int)(y1 * frame->height); + + bbox->detect_confidence = av_make_q((int)(conf * 10000), 10000); + bbox->classify_count = 0; + + if (ctx->labels && label_id < ctx->label_count) { + av_strlcpy(bbox->detect_label, ctx->labels[label_id], sizeof(bbox->detect_label)); + } else { + snprintf(bbox->detect_label, sizeof(bbox->detect_label), "%d", label_id); + } + + nb_bboxes--; + if (nb_bboxes == 0) { + break; + } + } + + return 0; +} + +static void free_detect_labels(DnnDetectContext *ctx) +{ + for (int i = 0; i < ctx->label_count; i++) { + av_freep(&ctx->labels[i]); + } + ctx->label_count = 0; + av_freep(&ctx->labels); +} + +static int read_detect_label_file(AVFilterContext *context) +{ + int line_len; + FILE *file; + DnnDetectContext *ctx = context->priv; + + file = av_fopen_utf8(ctx->labels_filename, "r"); + if (!file){ + av_log(context, AV_LOG_ERROR, "failed to open file %s\n", ctx->labels_filename); + return AVERROR(EINVAL); + } + + while (!feof(file)) { + char *label; + char buf[256]; + if (!fgets(buf, 256, file)) { + break; + } + + line_len = strlen(buf); + while (line_len) { + int i = line_len - 1; + if (buf[i] == '\n' || buf[i] == '\r' || buf[i] == ' ') { + buf[i] = '\0'; + line_len--; + } else { + break; + } + } + + if (line_len == 0) // empty line + continue; + + if (line_len >= AV_BBOX_LABEL_NAME_MAX_SIZE) { + av_log(context, AV_LOG_ERROR, "label %s too long\n", buf); + fclose(file); + return AVERROR(EINVAL); + } + + label = av_strdup(buf); + if (!label) { + av_log(context, AV_LOG_ERROR, "failed to allocate memory for label %s\n", buf); + fclose(file); + return AVERROR(ENOMEM); + } + + if (av_dynarray_add_nofree(&ctx->labels, &ctx->label_count, label) < 0) { + av_log(context, AV_LOG_ERROR, "failed to do av_dynarray_add\n"); + fclose(file); + av_freep(&label); + return AVERROR(ENOMEM); + } + } + + fclose(file); + return 0; +} + +static av_cold int dnn_detect_init(AVFilterContext *context) +{ + DnnDetectContext *ctx = context->priv; + int ret = ff_dnn_init(&ctx->dnnctx, DFT_ANALYTICS_DETECT, context); + if (ret < 0) + return ret; + ff_dnn_set_detect_post_proc(&ctx->dnnctx, dnn_detect_post_proc); + + if (ctx->labels_filename) { + return read_detect_label_file(context); + } + return 0; +} + +static int dnn_detect_query_formats(AVFilterContext *context) +{ + static const enum AVPixelFormat pix_fmts[] = { + AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24, + AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAYF32, + AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, + AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P, + AV_PIX_FMT_NV12, + AV_PIX_FMT_NONE + }; + AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts); + return ff_set_common_formats(context, fmts_list); +} + +static int dnn_detect_filter_frame(AVFilterLink *inlink, AVFrame *in) +{ + AVFilterContext *context = inlink->dst; + AVFilterLink *outlink = context->outputs[0]; + DnnDetectContext *ctx = context->priv; + DNNReturnType dnn_result; + + dnn_result = ff_dnn_execute_model(&ctx->dnnctx, in, NULL); + if (dnn_result != DNN_SUCCESS){ + av_log(ctx, AV_LOG_ERROR, "failed to execute model\n"); + av_frame_free(&in); + return AVERROR(EIO); + } + + return ff_filter_frame(outlink, in); +} + +static int dnn_detect_activate_sync(AVFilterContext *filter_ctx) +{ + AVFilterLink *inlink = filter_ctx->inputs[0]; + AVFilterLink *outlink = filter_ctx->outputs[0]; + AVFrame *in = NULL; + int64_t pts; + int ret, status; + int got_frame = 0; + + FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink); + + do { + // drain all input frames + ret = ff_inlink_consume_frame(inlink, &in); + if (ret < 0) + return ret; + if (ret > 0) { + ret = dnn_detect_filter_frame(inlink, in); + if (ret < 0) + return ret; + got_frame = 1; + } + } while (ret > 0); + + // if frame got, schedule to next filter + if (got_frame) + return 0; + + if (ff_inlink_acknowledge_status(inlink, &status, &pts)) { + if (status == AVERROR_EOF) { + ff_outlink_set_status(outlink, status, pts); + return ret; + } + } + + FF_FILTER_FORWARD_WANTED(outlink, inlink); + + return FFERROR_NOT_READY; +} + +static int dnn_detect_flush_frame(AVFilterLink *outlink, int64_t pts, int64_t *out_pts) +{ + DnnDetectContext *ctx = outlink->src->priv; + int ret; + DNNAsyncStatusType async_state; + + ret = ff_dnn_flush(&ctx->dnnctx); + if (ret != DNN_SUCCESS) { + return -1; + } + + do { + AVFrame *in_frame = NULL; + AVFrame *out_frame = NULL; + async_state = ff_dnn_get_async_result(&ctx->dnnctx, &in_frame, &out_frame); + if (out_frame) { + av_assert0(in_frame == out_frame); + ret = ff_filter_frame(outlink, out_frame); + if (ret < 0) + return ret; + if (out_pts) + *out_pts = out_frame->pts + pts; + } + av_usleep(5000); + } while (async_state >= DAST_NOT_READY); + + return 0; +} + +static int dnn_detect_activate_async(AVFilterContext *filter_ctx) +{ + AVFilterLink *inlink = filter_ctx->inputs[0]; + AVFilterLink *outlink = filter_ctx->outputs[0]; + DnnDetectContext *ctx = filter_ctx->priv; + AVFrame *in = NULL; + int64_t pts; + int ret, status; + int got_frame = 0; + int async_state; + + FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink); + + do { + // drain all input frames + ret = ff_inlink_consume_frame(inlink, &in); + if (ret < 0) + return ret; + if (ret > 0) { + if (ff_dnn_execute_model_async(&ctx->dnnctx, in, NULL) != DNN_SUCCESS) { + return AVERROR(EIO); + } + } + } while (ret > 0); + + // drain all processed frames + do { + AVFrame *in_frame = NULL; + AVFrame *out_frame = NULL; + async_state = ff_dnn_get_async_result(&ctx->dnnctx, &in_frame, &out_frame); + if (out_frame) { + av_assert0(in_frame == out_frame); + ret = ff_filter_frame(outlink, out_frame); + if (ret < 0) + return ret; + got_frame = 1; + } + } while (async_state == DAST_SUCCESS); + + // if frame got, schedule to next filter + if (got_frame) + return 0; + + if (ff_inlink_acknowledge_status(inlink, &status, &pts)) { + if (status == AVERROR_EOF) { + int64_t out_pts = pts; + ret = dnn_detect_flush_frame(outlink, pts, &out_pts); + ff_outlink_set_status(outlink, status, out_pts); + return ret; + } + } + + FF_FILTER_FORWARD_WANTED(outlink, inlink); + + return 0; +} + +static int dnn_detect_activate(AVFilterContext *filter_ctx) +{ + DnnDetectContext *ctx = filter_ctx->priv; + + if (ctx->dnnctx.async) + return dnn_detect_activate_async(filter_ctx); + else + return dnn_detect_activate_sync(filter_ctx); +} + +static av_cold void dnn_detect_uninit(AVFilterContext *context) +{ + DnnDetectContext *ctx = context->priv; + ff_dnn_uninit(&ctx->dnnctx); + free_detect_labels(ctx); +} + +static const AVFilterPad dnn_detect_inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + }, + { NULL } +}; + +static const AVFilterPad dnn_detect_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + }, + { NULL } +}; + +AVFilter ff_vf_dnn_detect = { + .name = "dnn_detect", + .description = NULL_IF_CONFIG_SMALL("Apply DNN detect filter to the input."), + .priv_size = sizeof(DnnDetectContext), + .init = dnn_detect_init, + .uninit = dnn_detect_uninit, + .query_formats = dnn_detect_query_formats, + .inputs = dnn_detect_inputs, + .outputs = dnn_detect_outputs, + .priv_class = &dnn_detect_class, + .activate = dnn_detect_activate, +};