From patchwork Fri Apr 30 03:07:08 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Fu, Ting" X-Patchwork-Id: 27536 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a11:4023:0:0:0:0 with SMTP id ky35csp2147003pxb; Thu, 29 Apr 2021 20:17:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwNfVn0wpvDDda6aigA28s7Mj92I5+mEPL4/IvUXuRPyzsLRjtddP+TZG/O9SrKnTxLeQU9 X-Received: by 2002:a17:907:1183:: with SMTP id uz3mr1788910ejb.264.1619752633292; Thu, 29 Apr 2021 20:17:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619752633; cv=none; d=google.com; s=arc-20160816; b=bVB2quarXWAoCPEqB0Or1Pjf+Sc5ZrQwHbSYIjTIXLB34d2ZjFRJYMDaS/fgK73HEt oTdLlqLXv33A50t2bdTuiscQ68CGX9QQmmlV3JbEhTmQ+Tn9rNPWQGFtKbSY79Qqe3jF TgjiCkelSKUsleBqhRxPoqQCm7x3XdvCU+2viGpJjiEe0x/SGIDyvLbK0ksZC8lNBLYo fNiIUJ5usqNxGbZXZ2fJ7q6J8Z6DQ/3eObfot64ZQrDh9feVjqeYxM/VsE3ZJh5H32s7 N8UWbjdYDRlhUGLV2rSqzEq3LmA6JaIrLKmwfsOr0LyDzpLb1B7KAbpbl6tabBdfZt0V 9idQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:message-id:date:to:from:ironport-sdr :ironport-sdr:delivered-to; bh=V6cq06/7eVVd1pReW4LfwY9HddFJzcRYSr0dRaEGEcs=; b=H1COeP9Ys5RHEacDYg2EP6sbwcFJKyJm/HeU5FJDL2B031nbSpW63ArPJA3ucFmWtD xLLpFii6XlEvWKsRQcBoY1iGTcF9YCxXsN4w2CxBW+4oEkdVJRw3tQJONIaXMJrg0pxg UKaNmVzpI6cZqPFQqSb9SwmjsRAtoC1E6gqS4mEBlv5qwEg1Q+N/7yAJjpjAmixJh6w9 ftVk4gsbptkSKnFU3/R+7Dpx/yu97IaH+aODuFIWZowrfZReGKcfjFcIG/E8ZnTxrhe+ 8u1fZpKtJkiOsbADQJQ8nrbyE8BJ75Bxe8We70PEJXNJWgfFPyjBQ3kP9Kks1Qw0o7g/ DIMA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id a42si622927edf.438.2021.04.29.20.17.12; Thu, 29 Apr 2021 20:17:13 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 342E4689C0A; Fri, 30 Apr 2021 06:17:08 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id ADCA6689C0A for ; Fri, 30 Apr 2021 06:16:59 +0300 (EEST) IronPort-SDR: ztY4vQ3j15p94QgLp64OZ9V5K+14Q0J/K6Xkx7+vqfUAHcOtehoHmUAUDwvXBLiyWxAMcW5HOa BSufSi4dxb+Q== X-IronPort-AV: E=McAfee;i="6200,9189,9969"; a="184659709" X-IronPort-AV: E=Sophos;i="5.82,260,1613462400"; d="scan'208";a="184659709" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Apr 2021 20:16:56 -0700 IronPort-SDR: l+mRiV2XGImiB3LG6QseOY81alss5Hr3Ygxm903VxcmqP1xGM+ZvhrquAuB0kUATT6H3Qf/8Q7 RoDgyFjVqOlw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.82,260,1613462400"; d="scan'208";a="459152496" Received: from semmer-ubuntu.sh.intel.com ([10.239.159.83]) by fmsmga002.fm.intel.com with ESMTP; 29 Apr 2021 20:16:55 -0700 From: Ting Fu To: ffmpeg-devel@ffmpeg.org Date: Fri, 30 Apr 2021 11:07:08 +0800 Message-Id: <20210430030711.30216-1-ting.fu@intel.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH 1/4] dnn: add DCO_RGB color order to enum DNNColorOrder X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: RUxWxzRj96BB Adding DCO_RGB color order to DNNColorOrder, since tensorflow model needs this kind of color oder as input. Signed-off-by: Ting Fu --- libavfilter/dnn/dnn_backend_tf.c | 1 + libavfilter/dnn/dnn_io_proc.c | 14 +++++++++++--- libavfilter/dnn_interface.h | 1 + 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/libavfilter/dnn/dnn_backend_tf.c b/libavfilter/dnn/dnn_backend_tf.c index 076dd3d6a9..f70e3d4659 100644 --- a/libavfilter/dnn/dnn_backend_tf.c +++ b/libavfilter/dnn/dnn_backend_tf.c @@ -143,6 +143,7 @@ static DNNReturnType get_input_tf(void *model, DNNData *input, const char *input tf_output.index = 0; input->dt = TF_OperationOutputType(tf_output); + input->order = DCO_RGB; status = TF_NewStatus(); TF_GraphGetTensorShape(tf_model->graph, tf_output, dims, 4, status); diff --git a/libavfilter/dnn/dnn_io_proc.c b/libavfilter/dnn/dnn_io_proc.c index e104cc5064..5f6ce36b96 100644 --- a/libavfilter/dnn/dnn_io_proc.c +++ b/libavfilter/dnn/dnn_io_proc.c @@ -167,11 +167,19 @@ static DNNReturnType proc_from_frame_to_dnn_frameprocessing(AVFrame *frame, DNND static enum AVPixelFormat get_pixel_format(DNNData *data) { - if (data->dt == DNN_UINT8 && data->order == DCO_BGR) { - return AV_PIX_FMT_BGR24; + if (data->dt == DNN_UINT8) { + switch (data->order) { + case DCO_BGR: + return AV_PIX_FMT_BGR24; + case DCO_RGB: + return AV_PIX_FMT_RGB24; + default: + av_assert0(!"unsupported data pixel format.\n"); + return AV_PIX_FMT_BGR24; + } } - av_assert0(!"not supported yet.\n"); + av_assert0(!"unsupported data type.\n"); return AV_PIX_FMT_BGR24; } diff --git a/libavfilter/dnn_interface.h b/libavfilter/dnn_interface.h index ae5a488341..92c3b0fc6e 100644 --- a/libavfilter/dnn_interface.h +++ b/libavfilter/dnn_interface.h @@ -39,6 +39,7 @@ typedef enum {DNN_FLOAT = 1, DNN_UINT8 = 4} DNNDataType; typedef enum { DCO_NONE, DCO_BGR, + DCO_RGB, } DNNColorOrder; typedef enum { From patchwork Fri Apr 30 03:07:09 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Fu, Ting" X-Patchwork-Id: 27535 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a11:4023:0:0:0:0 with SMTP id ky35csp2147121pxb; Thu, 29 Apr 2021 20:17:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzs/RQbKrULL27dLiRpmxnzAzmwU//0JfSQXPqtKevmFRJBMtUofVSv4odFODC7Jbjhuway X-Received: by 2002:a17:906:5811:: with SMTP id m17mr1731093ejq.221.1619752644365; Thu, 29 Apr 2021 20:17:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619752644; cv=none; d=google.com; s=arc-20160816; b=J+kHWQWKL21Fh0N/4v2UykLlTEGsmnwvKgg0gMyPkHLQDTPbU/dPDzXbhVGlRKkRqy JkzqjL9T6ztt22+5ucydWMgIqnv1LardcTjVhoo36H5G1WJeCl2hxnwoBqafP79GbRYm CsonybrJyE+2liybi82D0rRlhgw9NVF+ie7qFnto1Rj0Criq0vX4N/lZiD8DTED5cwli tIB6nKUJJT70SQPGATyBaa7WzN5hOOeuHDZVbvEY7XT736INJriRSPCllmDoU3Vm7ujx 0R04R9yETtpUbV9CD8UBoVZrBHDSVBhBZIKDpSeu9qQ/XxWRrBYpnlvKuaDgtvh2oZYq /UCA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:date :to:from:ironport-sdr:ironport-sdr:delivered-to; bh=0DndEAXtfDWmziaYdRYMHGe10Txpl4QM4iZB+1WS+gI=; b=LnpkNEcNNmdkEZWWx3akdYR+ksQl1JxF2EaGMMBu0j+RndLksfkB2dSHUnZNkMgfzf XhHCUfsptxzQEUXMVgBhJ8hQUwdHVXJK28gTxUEM9fRwZ/3ygm5kFnV6DH5TOEs0zCot BaR9IyHWEqecpIqJeo0npCEdl+tK+YAyoxybYLt0Ayk+W4BkF9sGsXuj3Mu2YJ7sXJES 3jcbBFXkE5tdkFUNO5KG6E+um2CGgAuFJvWYS1Vww7CqoMFhIIIH7FA2GT9LMYt2Q7MC GMRXyMSedDzuw2Jg0gDMT88jq3JHF2Kqomq3AV3DPt5y0UiIBD90HzENf9WObdRFY0qk 4tdQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id hk2si1714381ejb.128.2021.04.29.20.17.24; Thu, 29 Apr 2021 20:17:24 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 39C8268A2B1; Fri, 30 Apr 2021 06:17:13 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9D6426807AA for ; Fri, 30 Apr 2021 06:17:06 +0300 (EEST) IronPort-SDR: QTSex/YD3TwB0SgVhdO/SuC1k8AMnSIzH9ppQPLAl7xN7u8O60uhlbFXCGPMonAST2/cj9BIPr oW4UP4C5nCxg== X-IronPort-AV: E=McAfee;i="6200,9189,9969"; a="184659710" X-IronPort-AV: E=Sophos;i="5.82,260,1613462400"; d="scan'208";a="184659710" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Apr 2021 20:16:57 -0700 IronPort-SDR: G7tctzc07GXeLnq65KTx0fY6W+FtK1hr6/mxdgk3lSnYlBc6aoplQ6kGPTdcLfFczniJfEelh4 WdhH+Y7K80VA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.82,260,1613462400"; d="scan'208";a="459152502" Received: from semmer-ubuntu.sh.intel.com ([10.239.159.83]) by fmsmga002.fm.intel.com with ESMTP; 29 Apr 2021 20:16:56 -0700 From: Ting Fu To: ffmpeg-devel@ffmpeg.org Date: Fri, 30 Apr 2021 11:07:09 +0800 Message-Id: <20210430030711.30216-2-ting.fu@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210430030711.30216-1-ting.fu@intel.com> References: <20210430030711.30216-1-ting.fu@intel.com> Subject: [FFmpeg-devel] [PATCH 2/4] lavfi/dnn_backend_tensorflow: add multiple outputs support X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: Heb170h0Wszq Signed-off-by: Ting Fu --- libavfilter/dnn/dnn_backend_tf.c | 49 ++++++++++++++++---------------- libavfilter/dnn_filter_common.c | 45 +++++++++++++++++++++++++---- libavfilter/dnn_filter_common.h | 6 ++-- libavfilter/vf_derain.c | 2 +- libavfilter/vf_sr.c | 2 +- 5 files changed, 71 insertions(+), 33 deletions(-) diff --git a/libavfilter/dnn/dnn_backend_tf.c b/libavfilter/dnn/dnn_backend_tf.c index f70e3d4659..5c85b562c4 100644 --- a/libavfilter/dnn/dnn_backend_tf.c +++ b/libavfilter/dnn/dnn_backend_tf.c @@ -155,7 +155,7 @@ static DNNReturnType get_input_tf(void *model, DNNData *input, const char *input TF_DeleteStatus(status); // currently only NHWC is supported - av_assert0(dims[0] == 1); + av_assert0(dims[0] == 1 || dims[0] == -1); input->height = dims[1]; input->width = dims[2]; input->channels = dims[3]; @@ -707,7 +707,7 @@ static DNNReturnType execute_model_tf(const DNNModel *model, const char *input_n TF_Output *tf_outputs; TFModel *tf_model = model->model; TFContext *ctx = &tf_model->ctx; - DNNData input, output; + DNNData input, *outputs; TF_Tensor **output_tensors; TF_Output tf_input; TF_Tensor *input_tensor; @@ -738,14 +738,6 @@ static DNNReturnType execute_model_tf(const DNNModel *model, const char *input_n } } - if (nb_output != 1) { - // currently, the filter does not need multiple outputs, - // so we just pending the support until we really need it. - TF_DeleteTensor(input_tensor); - avpriv_report_missing_feature(ctx, "multiple outputs"); - return DNN_ERROR; - } - tf_outputs = av_malloc_array(nb_output, sizeof(*tf_outputs)); if (tf_outputs == NULL) { TF_DeleteTensor(input_tensor); @@ -785,23 +777,31 @@ static DNNReturnType execute_model_tf(const DNNModel *model, const char *input_n return DNN_ERROR; } + outputs = av_malloc_array(nb_output, sizeof(*outputs)); + if (!outputs) { + TF_DeleteTensor(input_tensor); + av_freep(&tf_outputs); + av_freep(&output_tensors); + av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for *outputs\n"); \ + return DNN_ERROR; + } + for (uint32_t i = 0; i < nb_output; ++i) { - output.height = TF_Dim(output_tensors[i], 1); - output.width = TF_Dim(output_tensors[i], 2); - output.channels = TF_Dim(output_tensors[i], 3); - output.data = TF_TensorData(output_tensors[i]); - output.dt = TF_TensorType(output_tensors[i]); - - if (do_ioproc) { - 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); - } + outputs[i].height = TF_Dim(output_tensors[i], 1); + outputs[i].width = TF_Dim(output_tensors[i], 2); + outputs[i].channels = TF_Dim(output_tensors[i], 3); + outputs[i].data = TF_TensorData(output_tensors[i]); + outputs[i].dt = TF_TensorType(output_tensors[i]); + } + if (do_ioproc) { + if (tf_model->model->frame_post_proc != NULL) { + tf_model->model->frame_post_proc(out_frame, outputs, tf_model->model->filter_ctx); } else { - out_frame->width = output.width; - out_frame->height = output.height; + ff_proc_from_dnn_to_frame(out_frame, outputs, ctx); } + } else { + out_frame->width = outputs[0].width; + out_frame->height = outputs[0].height; } for (uint32_t i = 0; i < nb_output; ++i) { @@ -812,6 +812,7 @@ static DNNReturnType execute_model_tf(const DNNModel *model, const char *input_n TF_DeleteTensor(input_tensor); av_freep(&output_tensors); av_freep(&tf_outputs); + av_freep(&outputs); return DNN_SUCCESS; } diff --git a/libavfilter/dnn_filter_common.c b/libavfilter/dnn_filter_common.c index 1b922455a3..4cbfdbf52a 100644 --- a/libavfilter/dnn_filter_common.c +++ b/libavfilter/dnn_filter_common.c @@ -17,6 +17,39 @@ */ #include "dnn_filter_common.h" +#include "libavutil/avstring.h" + +#define MAX_SUPPORTED_OUTPUTS_NB 4 + +static char **separate_output_names(const char *expr, const char *val_sep, int *separated_nb) +{ + char *val, **parsed_vals = NULL; + int val_num = 0; + if (!expr || !val_sep || !separated_nb) { + return NULL; + } + + parsed_vals = av_mallocz_array(MAX_SUPPORTED_OUTPUTS_NB, sizeof(*parsed_vals)); + if (!parsed_vals) { + return NULL; + } + + do { + val = av_get_token(&expr, val_sep); + if(val) { + parsed_vals[val_num] = val; + val_num++; + } + if (*expr) { + expr++; + } + } while(*expr); + + parsed_vals[val_num] = NULL; + *separated_nb = val_num; + + return parsed_vals; +} int ff_dnn_init(DnnContext *ctx, DNNFunctionType func_type, AVFilterContext *filter_ctx) { @@ -28,8 +61,10 @@ int ff_dnn_init(DnnContext *ctx, DNNFunctionType func_type, AVFilterContext *fil av_log(filter_ctx, AV_LOG_ERROR, "input name of the model network is not specified\n"); return AVERROR(EINVAL); } - if (!ctx->model_outputname) { - av_log(filter_ctx, AV_LOG_ERROR, "output name of the model network is not specified\n"); + + ctx->model_outputnames = separate_output_names(ctx->model_outputnames_string, "&", &ctx->nb_outputs); + if (!ctx->model_outputnames) { + av_log(filter_ctx, AV_LOG_ERROR, "could not parse model output names\n"); return AVERROR(EINVAL); } @@ -85,19 +120,19 @@ 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) { return ctx->model->get_output(ctx->model->model, ctx->model_inputname, input_width, input_height, - ctx->model_outputname, output_width, output_height); + (const char *)ctx->model_outputnames[0], output_width, output_height); } DNNReturnType ff_dnn_execute_model(DnnContext *ctx, AVFrame *in_frame, AVFrame *out_frame) { return (ctx->dnn_module->execute_model)(ctx->model, ctx->model_inputname, in_frame, - (const char **)&ctx->model_outputname, 1, out_frame); + (const char **)ctx->model_outputnames, ctx->nb_outputs, out_frame); } DNNReturnType ff_dnn_execute_model_async(DnnContext *ctx, AVFrame *in_frame, AVFrame *out_frame) { return (ctx->dnn_module->execute_model_async)(ctx->model, ctx->model_inputname, in_frame, - (const char **)&ctx->model_outputname, 1, out_frame); + (const char **)ctx->model_outputnames, ctx->nb_outputs, out_frame); } DNNAsyncStatusType ff_dnn_get_async_result(DnnContext *ctx, AVFrame **in_frame, AVFrame **out_frame) diff --git a/libavfilter/dnn_filter_common.h b/libavfilter/dnn_filter_common.h index 8deb18b39a..586d2afed4 100644 --- a/libavfilter/dnn_filter_common.h +++ b/libavfilter/dnn_filter_common.h @@ -30,10 +30,12 @@ typedef struct DnnContext { char *model_filename; DNNBackendType backend_type; char *model_inputname; - char *model_outputname; + char *model_outputnames_string; + uint32_t nb_outputs; char *backend_options; int async; + char **model_outputnames; DNNModule *dnn_module; DNNModel *model; } DnnContext; @@ -41,7 +43,7 @@ typedef struct DnnContext { #define DNN_COMMON_OPTIONS \ { "model", "path to model file", OFFSET(model_filename), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },\ { "input", "input name of the model", OFFSET(model_inputname), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },\ - { "output", "output name of the model", OFFSET(model_outputname), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },\ + { "output", "output name of the model", OFFSET(model_outputnames_string), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },\ { "backend_configs", "backend configs", OFFSET(backend_options), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },\ { "options", "backend configs", OFFSET(backend_options), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },\ { "async", "use DNN async inference", OFFSET(async), AV_OPT_TYPE_BOOL, { .i64 = 1}, 0, 1, FLAGS}, diff --git a/libavfilter/vf_derain.c b/libavfilter/vf_derain.c index 76c4ef414f..5037f3a5f7 100644 --- a/libavfilter/vf_derain.c +++ b/libavfilter/vf_derain.c @@ -50,7 +50,7 @@ static const AVOption derain_options[] = { #endif { "model", "path to model file", OFFSET(dnnctx.model_filename), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS }, { "input", "input name of the model", OFFSET(dnnctx.model_inputname), AV_OPT_TYPE_STRING, { .str = "x" }, 0, 0, FLAGS }, - { "output", "output name of the model", OFFSET(dnnctx.model_outputname), AV_OPT_TYPE_STRING, { .str = "y" }, 0, 0, FLAGS }, + { "output", "output name of the model", OFFSET(dnnctx.model_outputnames_string), AV_OPT_TYPE_STRING, { .str = "y" }, 0, 0, FLAGS }, { NULL } }; diff --git a/libavfilter/vf_sr.c b/libavfilter/vf_sr.c index 4360439ca6..f930b38748 100644 --- a/libavfilter/vf_sr.c +++ b/libavfilter/vf_sr.c @@ -54,7 +54,7 @@ static const AVOption sr_options[] = { { "scale_factor", "scale factor for SRCNN model", OFFSET(scale_factor), AV_OPT_TYPE_INT, { .i64 = 2 }, 2, 4, FLAGS }, { "model", "path to model file specifying network architecture and its parameters", OFFSET(dnnctx.model_filename), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS }, { "input", "input name of the model", OFFSET(dnnctx.model_inputname), AV_OPT_TYPE_STRING, { .str = "x" }, 0, 0, FLAGS }, - { "output", "output name of the model", OFFSET(dnnctx.model_outputname), AV_OPT_TYPE_STRING, { .str = "y" }, 0, 0, FLAGS }, + { "output", "output name of the model", OFFSET(dnnctx.model_outputnames_string), AV_OPT_TYPE_STRING, { .str = "y" }, 0, 0, FLAGS }, { NULL } }; From patchwork Fri Apr 30 03:07:10 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Fu, Ting" X-Patchwork-Id: 27534 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a11:4023:0:0:0:0 with SMTP id ky35csp2147261pxb; Thu, 29 Apr 2021 20:17:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzba+as5MaD8uFVTaPiL0ZBQO8Nt7DcMsbLKogJnO5NlJfEqfotqWdBp/lxB2EbUZfDPe1h X-Received: by 2002:a17:906:93f7:: with SMTP id yl23mr1724136ejb.407.1619752654695; Thu, 29 Apr 2021 20:17:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619752654; cv=none; d=google.com; s=arc-20160816; b=Ch/dN0WPORVr3H/0xyJKbU4cLCLDv/aAsa3zNvROnYxw2VAxUIIAzB6uNN0yofIkwK YZbaYKBTK7tGcODla7IU+xL7llCg4XtsSVEpv8KIyJ8OUuxEw5EcSaCHKmI1pEyfmVIz JRapmnOB2HFGQ1bqszIRSNxPR5PFOiAzDbsB5MH9W1N/FrARLKt+udowwbTJh/vOzdLz Dftp+hbinbxnw72bQAaJxpewLFXXOhyejW29sDFFP4exloTBaH11S49xzjJaAaxSqHZ7 iHEzChapUeFEXOL1c29JdMtJvH11q9dZR+RhS250ijZm8TQkqd94RSgjly6u4qprh7Iw BejA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:date :to:from:ironport-sdr:ironport-sdr:delivered-to; bh=/zEcuBKcu5Lc0u+/AqRQJf35z8MVN55YvQpdbJU7FYg=; b=m3mU8WJbbsPiahfjvNYIkIwci9URCLyQvQ2DvTFUdhRi6hSRulSKQlcjMIQfHfADdW fTxkrfp5zTjONklO7vuqVWfVqQsDiZff02b9HtJ/ydJlzVfbO+OTcjurtabsGJZmCJkZ NP4DnSJ2fZ/NDXlbkJNpCyiGfs/EUv23NKkutlso3LchbpS4bfEHWVvIBbc7iLKoFpf8 NianvTRdvEWKMYeJJayXI1/s9ZMFzsfDUs3hP0kljL8/4VWuipRGf1fS/HhsvGuQQQku 8mjmtIF2fKCffNjPiWUo/3i6yeeBly5nIfBnJyPlWb45ob2vbxlWi4tyMwbFLBRTELIF c+cg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id r26si1766880ejc.505.2021.04.29.20.17.34; Thu, 29 Apr 2021 20:17:34 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 37F3A68A2CC; Fri, 30 Apr 2021 06:17:15 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id EDA3F68A2AE for ; Fri, 30 Apr 2021 06:17:07 +0300 (EEST) IronPort-SDR: ahyfgubMzNdjZDOI8PJuHHbO00a/BWFrzhQ1v5woMPZEcsGt1u/LlkadDl7NF8jUcz3W8MqZrY nM7f8g/OrFEg== X-IronPort-AV: E=McAfee;i="6200,9189,9969"; a="184659711" X-IronPort-AV: E=Sophos;i="5.82,260,1613462400"; d="scan'208";a="184659711" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Apr 2021 20:16:58 -0700 IronPort-SDR: vJUsd4HPIezZOfRuMQW3nFoA0EgCUw9gadASG9dBtsvEZ1E+OV91Typ0/MwMJ5ZL+qgThfkEhc OV7MsisQhBkQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.82,260,1613462400"; d="scan'208";a="459152508" Received: from semmer-ubuntu.sh.intel.com ([10.239.159.83]) by fmsmga002.fm.intel.com with ESMTP; 29 Apr 2021 20:16:57 -0700 From: Ting Fu To: ffmpeg-devel@ffmpeg.org Date: Fri, 30 Apr 2021 11:07:10 +0800 Message-Id: <20210430030711.30216-3-ting.fu@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210430030711.30216-1-ting.fu@intel.com> References: <20210430030711.30216-1-ting.fu@intel.com> Subject: [FFmpeg-devel] [PATCH 3/4] lavfi/dnn_backend_tensorflow: support detect model X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: C9H1jykqTK3S Signed-off-by: Ting Fu --- libavfilter/dnn/dnn_backend_tf.c | 39 ++++++++++++++++++++++++++------ libavfilter/vf_dnn_detect.c | 32 +++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 8 deletions(-) diff --git a/libavfilter/dnn/dnn_backend_tf.c b/libavfilter/dnn/dnn_backend_tf.c index 5c85b562c4..8fb2ae8583 100644 --- a/libavfilter/dnn/dnn_backend_tf.c +++ b/libavfilter/dnn/dnn_backend_tf.c @@ -793,15 +793,40 @@ static DNNReturnType execute_model_tf(const DNNModel *model, const char *input_n outputs[i].data = TF_TensorData(output_tensors[i]); outputs[i].dt = TF_TensorType(output_tensors[i]); } - if (do_ioproc) { - if (tf_model->model->frame_post_proc != NULL) { - tf_model->model->frame_post_proc(out_frame, outputs, tf_model->model->filter_ctx); + switch (model->func_type) { + case DFT_PROCESS_FRAME: + //it only support 1 output if it's frame in & frame out + if (do_ioproc) { + if (tf_model->model->frame_post_proc != NULL) { + tf_model->model->frame_post_proc(out_frame, outputs, tf_model->model->filter_ctx); + } else { + ff_proc_from_dnn_to_frame(out_frame, outputs, ctx); + } } else { - ff_proc_from_dnn_to_frame(out_frame, outputs, ctx); + out_frame->width = outputs[0].width; + out_frame->height = outputs[0].height; + } + break; + case DFT_ANALYTICS_DETECT: + if (!model->detect_post_proc) { + av_log(ctx, AV_LOG_ERROR, "Detect filter needs provide post proc\n"); + return DNN_ERROR; + } + model->detect_post_proc(out_frame, outputs, nb_output, model->filter_ctx); + break; + default: + for (uint32_t i = 0; i < nb_output; ++i) { + if (output_tensors[i]) { + TF_DeleteTensor(output_tensors[i]); + } } - } else { - out_frame->width = outputs[0].width; - out_frame->height = outputs[0].height; + TF_DeleteTensor(input_tensor); + av_freep(&output_tensors); + av_freep(&tf_outputs); + av_freep(&outputs); + + av_log(ctx, AV_LOG_ERROR, "Tensorflow backend does not support this kind of dnn filter now\n"); + return DNN_ERROR; } for (uint32_t i = 0; i < nb_output; ++i) { diff --git a/libavfilter/vf_dnn_detect.c b/libavfilter/vf_dnn_detect.c index 1dbe4f29a4..7d39acb653 100644 --- a/libavfilter/vf_dnn_detect.c +++ b/libavfilter/vf_dnn_detect.c @@ -203,10 +203,40 @@ static int read_detect_label_file(AVFilterContext *context) return 0; } +static int check_output_nb(DnnDetectContext *ctx, DNNBackendType backend_type, int output_nb) +{ + switch(backend_type) { + case DNN_TF: + if (output_nb != 4) { + av_log(ctx, AV_LOG_ERROR, "Only support tensorflow detect model with 4 outputs, \ + but get %d instead\n", output_nb); + return AVERROR(EINVAL); + } + return 0; + case DNN_OV: + if (output_nb != 1) { + av_log(ctx, AV_LOG_ERROR, "Dnn detect filter with openvino backend needs 1 output only, \ + but get %d instead\n", output_nb); + return AVERROR(EINVAL); + } + return 0; + default: + avpriv_report_missing_feature(ctx, "Dnn detect filter does not support current backend\n"); + return AVERROR(EINVAL); + } + 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); + DnnContext *dnn_ctx = &ctx->dnnctx; + int ret; + + ret = ff_dnn_init(&ctx->dnnctx, DFT_ANALYTICS_DETECT, context); + if (ret < 0) + return ret; + ret = check_output_nb(ctx, dnn_ctx->backend_type, dnn_ctx->nb_outputs); if (ret < 0) return ret; ff_dnn_set_detect_post_proc(&ctx->dnnctx, dnn_detect_post_proc); From patchwork Fri Apr 30 03:07:11 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Fu, Ting" X-Patchwork-Id: 27537 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a11:4023:0:0:0:0 with SMTP id ky35csp2147403pxb; Thu, 29 Apr 2021 20:17:44 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzaKs+KDdzBoo5eAxL0nQp9Bl1Cld3W6vumk6rjMzfS3hd2OuxL5HMcrYadjUfT5Fq7DCcg X-Received: by 2002:a17:906:b28e:: with SMTP id q14mr1706574ejz.528.1619752664627; Thu, 29 Apr 2021 20:17:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619752664; cv=none; d=google.com; s=arc-20160816; b=t/w//kQiGbcnuOVQyEc8E3NuCYmO5NGtX8RcnTfTLVKuPUMSWadEc0R0G0eKGxlZiU OM6W7D5fMGnikoel1VibUP0V/+YwKVjDTY2dwjGhtbhHoGxWnHqmu6kwzwTgwNZMI072 bPym5YfCWiQBN4UhKFl6/T143+bmY4LwsUoJeiI8Tfmwx5uJGBHdDcnYNYS876P5eE6r qsVAYmniP3X7zqKAWAPI3+77piFrlYpOTHTA6w/mwj4JxBPxW3zSsfFWuUGkBjgJOrAI S8UgbRiVymDSVjs20xPQR7uNySN7c+c2+2NY1h/5lsMB3tYQwHeR117V9kt+yjfSdbos /FjQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:date :to:from:ironport-sdr:ironport-sdr:delivered-to; bh=JNCtRzTD5qlR+ulMJNMdrT9v2ETcj0LbWb9wpqQ7Frg=; b=mRJ1n+6Zok415s5DL8gOxYHvkZFKwHyg0Z9qvOnFegZOEs0Gvd08FnC7vGZPgEXR5y ahsiihEg/9YsJ8vZQLbDmpnky2BAMgFfv5cR2D4y1nyOrZzF/JjGTJwNmdH/l2P18s82 n+Sd8ysgJiW9znQT3V8NWe6IUFfRFmW+9juXmve46vo/KWLsobpwRRc84RSkM2Xdmhqw IhaZ20dYHbSmJoDbZSTvq4//qkCQzMHa3hAOTzxORkFIWySEIi+7VUk5xHhLhq/aO9DA /0OUyN0MbH3IaFCXYRC/IG40r9b5YYcxvT85SJZ4RjM5UI+3gPjNT/8jcAckBkj3uK1+ RV7g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id hc43si2170105ejc.21.2021.04.29.20.17.44; Thu, 29 Apr 2021 20:17:44 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 773D968A2F4; Fri, 30 Apr 2021 06:17:19 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id C249468A295 for ; Fri, 30 Apr 2021 06:17:12 +0300 (EEST) IronPort-SDR: Wa5usnzWytr/6N2iG2jSiNwKJ+zfujStPT4XEN3dc5PilqRGhumQWZaJ/RDH+EXLTSLpA9qwA+ GcVP4ldsxMQA== X-IronPort-AV: E=McAfee;i="6200,9189,9969"; a="184659712" X-IronPort-AV: E=Sophos;i="5.82,260,1613462400"; d="scan'208";a="184659712" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Apr 2021 20:16:58 -0700 IronPort-SDR: Sq5DSatATUrCo1UP3UeQBkNLpCk6ER8tuWlLozJv+YqzMDs71O9o/g1yHeUjQpchhyhuFCPSKG LIraEfjWguqg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.82,260,1613462400"; d="scan'208";a="459152512" Received: from semmer-ubuntu.sh.intel.com ([10.239.159.83]) by fmsmga002.fm.intel.com with ESMTP; 29 Apr 2021 20:16:58 -0700 From: Ting Fu To: ffmpeg-devel@ffmpeg.org Date: Fri, 30 Apr 2021 11:07:11 +0800 Message-Id: <20210430030711.30216-4-ting.fu@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210430030711.30216-1-ting.fu@intel.com> References: <20210430030711.30216-1-ting.fu@intel.com> Subject: [FFmpeg-devel] [PATCH 4/4] dnn/vf_dnn_detect: add tensorflow output parse support X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 8EkAcqrGVH9y Testing model is tensorflow offical model in github repo, please refer https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf1_detection_zoo.md to download the detect model as you need. For example, local testing was carried on with 'ssd_mobilenet_v2_coco_2018_03_29.tar.gz', and used one image of dog in https://github.com/tensorflow/models/blob/master/research/object_detection/test_images/image1.jpg Testing command is: ./ffmpeg -i image1.jpg -vf dnn_detect=dnn_backend=tensorflow:input=image_tensor:output=\ "num_detections&detection_scores&detection_classes&detection_boxes":model=ssd_mobilenet_v2_coco.pb,\ showinfo -f null - We will see the result similar as below: [Parsed_showinfo_1 @ 0x33e65f0] side data - detection bounding boxes: [Parsed_showinfo_1 @ 0x33e65f0] source: ssd_mobilenet_v2_coco.pb [Parsed_showinfo_1 @ 0x33e65f0] index: 0, region: (382, 60) -> (1005, 593), label: 18, confidence: 9834/10000. [Parsed_showinfo_1 @ 0x33e65f0] index: 1, region: (12, 8) -> (328, 549), label: 18, confidence: 8555/10000. [Parsed_showinfo_1 @ 0x33e65f0] index: 2, region: (293, 7) -> (682, 458), label: 1, confidence: 8033/10000. [Parsed_showinfo_1 @ 0x33e65f0] index: 3, region: (342, 0) -> (690, 325), label: 1, confidence: 5878/10000. There are two boxes of dog with cores 94.05% & 93.45% and two boxes of person with scores 80.33% & 58.78%. Signed-off-by: Ting Fu --- libavfilter/vf_dnn_detect.c | 95 ++++++++++++++++++++++++++++++++++++- 1 file changed, 94 insertions(+), 1 deletion(-) diff --git a/libavfilter/vf_dnn_detect.c b/libavfilter/vf_dnn_detect.c index 7d39acb653..818b53a052 100644 --- a/libavfilter/vf_dnn_detect.c +++ b/libavfilter/vf_dnn_detect.c @@ -48,6 +48,9 @@ typedef struct DnnDetectContext { #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_LIBTENSORFLOW == 1) + { "tensorflow", "tensorflow backend flag", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, FLAGS, "backend" }, +#endif #if (CONFIG_LIBOPENVINO == 1) { "openvino", "openvino backend flag", 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, 0, 0, FLAGS, "backend" }, #endif @@ -59,7 +62,7 @@ static const AVOption dnn_detect_options[] = { AVFILTER_DEFINE_CLASS(dnn_detect); -static int dnn_detect_post_proc(AVFrame *frame, DNNData *output, uint32_t nb, AVFilterContext *filter_ctx) +static int dnn_detect_post_proc_ov(AVFrame *frame, DNNData *output, AVFilterContext *filter_ctx) { DnnDetectContext *ctx = filter_ctx->priv; float conf_threshold = ctx->confidence; @@ -136,6 +139,96 @@ static int dnn_detect_post_proc(AVFrame *frame, DNNData *output, uint32_t nb, AV return 0; } +static int dnn_detect_post_proc_tf(AVFrame *frame, DNNData *output, AVFilterContext *filter_ctx) +{ + DnnDetectContext *ctx = filter_ctx->priv; + int proposal_count; + float conf_threshold = ctx->confidence; + float *conf, *position, *label_id, x0, y0, x1, y1; + int nb_bboxes = 0; + AVFrameSideData *sd; + AVDetectionBBox *bbox; + AVDetectionBBoxHeader *header; + + proposal_count = *(float *)(output[0].data); + conf = output[1].data; + position = output[3].data; + label_id = output[2].data; + + sd = av_frame_get_side_data(frame, AV_FRAME_DATA_DETECTION_BBOXES); + 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) { + if (conf[i] < 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_detection_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)); + + for (int i = 0; i < proposal_count; ++i) { + y0 = position[i * 4]; + x0 = position[i * 4 + 1]; + y1 = position[i * 4 + 2]; + x1 = position[i * 4 + 3]; + + bbox = av_get_detection_bbox(header, i); + + if (conf[i] < conf_threshold) { + continue; + } + + bbox->x = (int)(x0 * frame->width); + bbox->w = (int)(x1 * frame->width) - bbox->x; + bbox->y = (int)(y0 * frame->height); + bbox->h = (int)(y1 * frame->height) - bbox->y; + + bbox->detect_confidence = av_make_q((int)(conf[i] * 10000), 10000); + bbox->classify_count = 0; + + if (ctx->labels && label_id[i] < ctx->label_count) { + av_strlcpy(bbox->detect_label, ctx->labels[(int)label_id[i]], sizeof(bbox->detect_label)); + } else { + snprintf(bbox->detect_label, sizeof(bbox->detect_label), "%d", (int)label_id[i]); + } + + nb_bboxes--; + if (nb_bboxes == 0) { + break; + } + } + return 0; +} + +static int dnn_detect_post_proc(AVFrame *frame, DNNData *output, uint32_t nb, AVFilterContext *filter_ctx) +{ + DnnDetectContext *ctx = filter_ctx->priv; + DnnContext *dnn_ctx = &ctx->dnnctx; + switch (dnn_ctx->backend_type) { + case DNN_OV: + return dnn_detect_post_proc_ov(frame, output, filter_ctx); + case DNN_TF: + return dnn_detect_post_proc_tf(frame, output, filter_ctx); + default: + avpriv_report_missing_feature(filter_ctx, "Current dnn backend do not support detect filter\n"); + return AVERROR(EINVAL); + } +} + static void free_detect_labels(DnnDetectContext *ctx) { for (int i = 0; i < ctx->label_count; i++) {