[FFmpeg-devel] [PATCH 10/10] libavfilter: add filter dnn_detect for object detection
Guo, Yejun
yejun.guo at intel.com
Wed Feb 10 06:59:13 EET 2021
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 (2021.1 version), 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/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:conf=0.6,showinfo -f null -
We'll see the detect result as below:
[Parsed_showinfo_1 @ 0x55db3ffb60c0] side data - Dnn bounding boxes:
[Parsed_showinfo_1 @ 0x55db3ffb60c0] index: 0, region: (330/672, 203/384) -> (356/672, 226/384), label: 1, confidence: 10000/10000.
[Parsed_showinfo_1 @ 0x55db3ffb60c0] index: 1, region: (291/672, 209/384) -> (317/672, 231/384), label: 1, confidence: 6917/10000.
There are two faces detected with confidence 100% and 69.17%, and
the input image size of the model is 672x384. The two bounding boxes
in this image are (330, 203)->(356, 226) and (291, 209)->(317, 231).
Since the orignal input image size is 2048x1536, so the two bounding
boxese in the original image are
(330/672*2048=1006, 203/384*1536=812) -> (1085, 904) and
(887, 836) -> (966, 924), and we can check them manually.
Signed-off-by: Guo, Yejun <yejun.guo at intel.com>
Next, we'll add tensorflow backend and update filter vf_drawbox etc
to visualize the detect result.
---
configure | 1 +
doc/filters.texi | 33 +++
libavfilter/Makefile | 1 +
libavfilter/allfilters.c | 1 +
libavfilter/dnn/dnn_backend_openvino.c | 12 +
libavfilter/dnn_filter_common.c | 7 +
libavfilter/dnn_filter_common.h | 1 +
libavfilter/dnn_interface.h | 6 +-
libavfilter/vf_dnn_detect.c | 356 +++++++++++++++++++++++++
9 files changed, 416 insertions(+), 2 deletions(-)
create mode 100644 libavfilter/vf_dnn_detect.c
diff --git a/configure b/configure
index a76c2ec4ae..2d2668571d 100755
--- a/configure
+++ b/configure
@@ -3548,6 +3548,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 079bba9a1e..340402e650 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -10073,6 +10073,39 @@ ffmpeg -i INPUT -f lavfi -i nullsrc=hd720,geq='r=128+80*(sin(sqrt((X-W/2)*(X-W/2
@end example
@end itemize
+ at section dnn_detect
+
+Do object detection with deep neural networks.
+
+The filter accepts the following options:
+
+ at table @option
+ at 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.
+
+ at item model
+Set path to model file specifying network architecture and its parameters.
+Note that different backends use different file formats.
+
+ at item input
+Set the input name of the dnn network.
+
+ at item output
+Set the output name of the dnn network.
+
+ at item conf
+Set the confidence threshold (default: 0.5).
+
+ at item backend_configs
+Set the configs to be passed into backend
+
+ at item async
+use DNN async execution if set (default: set),
+roll back to sync execution if the backend does not support async.
+
+ at end table
+
@anchor{dnn_processing}
@section dnn_processing
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index b43933be64..6c39e7111b 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -244,6 +244,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 73d859ce5e..37bb276685 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -229,6 +229,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 5be053b7f8..928d84b744 100644
--- a/libavfilter/dnn/dnn_backend_openvino.c
+++ b/libavfilter/dnn/dnn_backend_openvino.c
@@ -621,6 +621,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.
@@ -674,6 +680,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;
+ }
+ }
+
task = av_malloc(sizeof(*task));
if (!task) {
av_log(ctx, AV_LOG_ERROR, "unable to alloc memory for task item.\n");
diff --git a/libavfilter/dnn_filter_common.c b/libavfilter/dnn_filter_common.c
index 413adba406..92b696e710 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_proc(DnnContext *ctx, PRE_POST_PROC pre_proc, PRE_POST_PROC post_proc)
+{
+ ctx->model->pre_proc = pre_proc;
+ ctx->model->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..0e88b88bdd 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_proc(DnnContext *ctx, PRE_POST_PROC pre_proc, PRE_POST_PROC 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..90a08129f4 100644
--- a/libavfilter/dnn_interface.h
+++ b/libavfilter/dnn_interface.h
@@ -63,6 +63,8 @@ typedef struct DNNData{
DNNColorOrder order;
} DNNData;
+typedef int (*PRE_POST_PROC)(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);
+ PRE_POST_PROC 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);
+ PRE_POST_PROC post_proc;
} DNNModel;
// Stores pointers to functions for loading, executing, freeing DNN models for one of the backends.
diff --git a/libavfilter/vf_dnn_detect.c b/libavfilter/vf_dnn_detect.c
new file mode 100644
index 0000000000..bac5035ae8
--- /dev/null
+++ b/libavfilter/vf_dnn_detect.c
@@ -0,0 +1,356 @@
+/*
+ * Copyright (c) 2021
+ *
+ * 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/dnn_bbox.h"
+
+typedef struct DnnDetectContext {
+ const AVClass *class;
+ DnnContext dnnctx;
+ float conf;
+ int model_input_width;
+ int model_input_height;
+} 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
+ { "conf", "threshold of confidence", OFFSET2(conf), AV_OPT_TYPE_FLOAT, { .dbl = 0.5 }, 0, 1, FLAGS},
+ { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(dnn_detect);
+
+static int dnn_detect_post_proc(AVFrame *frame, DNNData *output, AVFilterContext *filter_ctx)
+{
+ DnnDetectContext *ctx = filter_ctx->priv;
+ float conf_threshold = ctx->conf;
+ int proposal_count = output->height;
+ int detect_size = output->width;
+ float *detections = output->data;
+ int nb_bbox = 0;
+ AVFrameSideData *sd;
+ AVDnnBoundingBox *bbox;
+
+ sd = av_frame_get_side_data(frame, AV_FRAME_DATA_DNN_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) {
+ float conf = detections[i * detect_size + 2];
+ if (conf < conf_threshold) {
+ continue;
+ }
+ nb_bbox++;
+ }
+
+ if (nb_bbox == 0) {
+ av_log(filter_ctx, AV_LOG_VERBOSE, "nothing detected in this frame.\n");
+ return 0;
+ }
+
+ sd = av_frame_new_side_data(frame, AV_FRAME_DATA_DNN_BBOXES,
+ sizeof(AVDnnBoundingBox) * nb_bbox);
+ if (!sd) {
+ av_log(filter_ctx, AV_LOG_ERROR, "failed to allocate side data for AV_FRAME_DATA_DNN_BBOXES with %d bboxes\n", nb_bbox);
+ return -1;
+ }
+
+ bbox = (AVDnnBoundingBox *)sd->data;
+ for (int i = 0; i < proposal_count; ++i) {
+ int av_unused image_id = (int)detections[i * detect_size + 0];
+ int label = (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];
+
+ if (conf < conf_threshold) {
+ continue;
+ }
+
+ *bbox = (AVDnnBoundingBox) {
+ .self_size = sizeof(*bbox),
+ .model_input_width = ctx->model_input_width,
+ .model_input_height = ctx->model_input_height,
+ .left = (int)(x0 * ctx->model_input_width),
+ .right = (int)(x1 * ctx->model_input_width),
+ .top = (int)(y0 * ctx->model_input_height),
+ .bottom = (int)(y1 * ctx->model_input_height),
+ .detect_label = label,
+ .detect_conf = av_make_q((int)(conf * 10000), 10000),
+ .classify_count = 0,
+ };
+
+ nb_bbox--;
+ if (nb_bbox == 0) {
+ break;
+ }
+ bbox++;
+ }
+
+ return 0;
+}
+
+static av_cold int dnn_detect_init(AVFilterContext *context)
+{
+ DNNReturnType result;
+ DNNData model_input;
+ DnnDetectContext *ctx = context->priv;
+ int ret = ff_dnn_init(&ctx->dnnctx, DFT_ANALYTICS_DETECT, context);
+ if (ret < 0)
+ return ret;
+ ff_dnn_set_proc(&ctx->dnnctx, NULL, dnn_detect_post_proc);
+
+ result = ff_dnn_get_input(&ctx->dnnctx, &model_input);
+ if (result != DNN_SUCCESS) {
+ av_log(ctx, AV_LOG_ERROR, "could not get input from the model.\n");
+ return AVERROR(EIO);
+ }
+
+ ctx->model_input_width = model_input.width;
+ ctx->model_input_height = model_input.height;
+ 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 *ctx)
+{
+ DnnDetectContext *context = ctx->priv;
+ ff_dnn_uninit(&context->dnnctx);
+}
+
+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,
+};
--
2.17.1
More information about the ffmpeg-devel
mailing list