Skip to content

Commit f444be6

Browse files
Semmer2guoyejun
authored andcommitted
libavfilter: vf_drawbox filter support draw box with detection bounding boxes in side_data
This feature can be used with dnn detection by setting vf_drawbox's option box_source=side_data_detection_bboxes, for example: ./ffmpeg -i face.jpeg -vf dnn_detect=dnn_backend=openvino:model=face-detection-adas-0001.xml:\ input=data:output=detection_out:labels=face-detection-adas-0001.label,\ drawbox=box_source=side_data_detection_bboxes -y face_detect.jpeg Signed-off-by: Ting Fu <ting.fu@intel.com>
1 parent 9921ae8 commit f444be6

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

doc/filters.texi

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10356,6 +10356,14 @@ The x and y offset coordinates where the box is drawn.
1035610356
@item h
1035710357
The width and height of the drawn box.
1035810358

10359+
@item box_source
10360+
Box source can be set as side_data_detection_bboxes if you want to use box data in
10361+
detection bboxes of side data.
10362+
10363+
If @var{box_source} is set, the @var{x}, @var{y}, @var{width} and @var{height} will be ignored and
10364+
still use box data in detection bboxes of side data. So please do not use this parameter if you were
10365+
not sure about the box source.
10366+
1035910367
@item t
1036010368
The thickness of the drawn box.
1036110369

libavfilter/vf_drawbox.c

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "libavutil/eval.h"
3232
#include "libavutil/pixdesc.h"
3333
#include "libavutil/parseutils.h"
34+
#include "libavutil/detection_bbox.h"
3435
#include "avfilter.h"
3536
#include "formats.h"
3637
#include "internal.h"
@@ -79,8 +80,10 @@ typedef struct DrawBoxContext {
7980
char *x_expr, *y_expr; ///< expression for x and y
8081
char *w_expr, *h_expr; ///< expression for width and height
8182
char *t_expr; ///< expression for thickness
83+
char *box_source_string; ///< string for box data source
8284
int have_alpha;
8385
int replace;
86+
enum AVFrameSideDataType box_source;
8487
} DrawBoxContext;
8588

8689
static const int NUM_EXPR_EVALS = 5;
@@ -140,11 +143,30 @@ static void draw_region(AVFrame *frame, DrawBoxContext *ctx, int left, int top,
140143
}
141144
}
142145

146+
static enum AVFrameSideDataType box_source_string_parse(const char *box_source_string)
147+
{
148+
av_assert0(box_source_string);
149+
if (!strcmp(box_source_string, "side_data_detection_bboxes")) {
150+
return AV_FRAME_DATA_DETECTION_BBOXES;
151+
} else {
152+
// will support side_data_regions_of_interest next
153+
return AVERROR(EINVAL);
154+
}
155+
}
156+
143157
static av_cold int init(AVFilterContext *ctx)
144158
{
145159
DrawBoxContext *s = ctx->priv;
146160
uint8_t rgba_color[4];
147161

162+
if (s->box_source_string) {
163+
s->box_source = box_source_string_parse(s->box_source_string);
164+
if ((int)s->box_source < 0) {
165+
av_log(ctx, AV_LOG_ERROR, "Error box source: %s\n",s->box_source_string);
166+
return AVERROR(EINVAL);
167+
}
168+
}
169+
148170
if (!strcmp(s->color_str, "invert"))
149171
s->invert_color = 1;
150172
else if (av_parse_color(rgba_color, s->color_str, -1, ctx) < 0)
@@ -272,9 +294,34 @@ static av_pure av_always_inline int pixel_belongs_to_box(DrawBoxContext *s, int
272294
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
273295
{
274296
DrawBoxContext *s = inlink->dst->priv;
297+
const AVDetectionBBoxHeader *header = NULL;
298+
const AVDetectionBBox *bbox;
299+
AVFrameSideData *sd;
300+
int loop = 1;
301+
302+
if (s->box_source == AV_FRAME_DATA_DETECTION_BBOXES) {
303+
sd = av_frame_get_side_data(frame, AV_FRAME_DATA_DETECTION_BBOXES);
304+
if (sd) {
305+
header = (AVDetectionBBoxHeader *)sd->data;
306+
loop = header->nb_bboxes;
307+
} else {
308+
av_log(s, AV_LOG_WARNING, "No detection bboxes.\n");
309+
return ff_filter_frame(inlink->dst->outputs[0], frame);
310+
}
311+
}
275312

276-
draw_region(frame, s, FFMAX(s->x, 0), FFMAX(s->y, 0), FFMIN(s->x + s->w, frame->width),
277-
FFMIN(s->y + s->h, frame->height), pixel_belongs_to_box);
313+
for (int i = 0; i < loop; i++) {
314+
if (header) {
315+
bbox = av_get_detection_bbox(header, i);
316+
s->y = bbox->y;
317+
s->x = bbox->x;
318+
s->h = bbox->h;
319+
s->w = bbox->w;
320+
}
321+
322+
draw_region(frame, s, FFMAX(s->x, 0), FFMAX(s->y, 0), FFMIN(s->x + s->w, frame->width),
323+
FFMIN(s->y + s->h, frame->height), pixel_belongs_to_box);
324+
}
278325

279326
return ff_filter_frame(inlink->dst->outputs[0], frame);
280327
}
@@ -329,6 +376,7 @@ static const AVOption drawbox_options[] = {
329376
{ "thickness", "set the box thickness", OFFSET(t_expr), AV_OPT_TYPE_STRING, { .str="3" }, 0, 0, FLAGS },
330377
{ "t", "set the box thickness", OFFSET(t_expr), AV_OPT_TYPE_STRING, { .str="3" }, 0, 0, FLAGS },
331378
{ "replace", "replace color & alpha", OFFSET(replace), AV_OPT_TYPE_BOOL, { .i64=0 }, 0, 1, FLAGS },
379+
{ "box_source", "use datas from bounding box in side data", OFFSET(box_source_string), AV_OPT_TYPE_STRING, { .str=NULL }, 0, 1, FLAGS },
332380
{ NULL }
333381
};
334382

0 commit comments

Comments
 (0)