History

libavfilter/vf_dnn_detect: Add model_type option.

There are many kinds of detection DNN model and they have different preprocess and postprocess methods. To support more models, "model_type" option is added to help to choose preprocess and postprocess function.

Wenbin Chen committed 2 years ago

Commit caa5d121 parent 2020ef91 file · +35 −7Browse files

Unified · Split

libavfilter/vf_dnn_detect.c+35−7
@@ -31,6 +31,10 @@ · 30 unmodified lines
3131 #include "libavutil/avstring.h"
3232 #include "libavutil/detection_bbox.h"
3333
34+typedef enum {
35+ DDMT_SSD
36+} DNNDetectionModelType;
37+
3438 typedef struct DnnDetectContext {
3539 const AVClass *class;
3640 DnnContext dnnctx;
@@ -38,6 +42,7 @@ typedef struct DnnDetectContext { · 1 unmodified line
3842 char *labels_filename;
3943 char **labels;
4044 int label_count;
45+ DNNDetectionModelType model_type;
4146 } DnnDetectContext;
4247
4348 #define OFFSET(x) offsetof(DnnDetectContext, dnnctx.x)
@@ -54,12 +59,14 @@ static const AVOption dnn_detect_options[] = { · 10 unmodified lines
5459 DNN_COMMON_OPTIONS
5560 { "confidence", "threshold of confidence", OFFSET2(confidence), AV_OPT_TYPE_FLOAT, { .dbl = 0.5 }, 0, 1, FLAGS},
5661 { "labels", "path to labels file", OFFSET2(labels_filename), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },
62+ { "model_type", "DNN detection model type", OFFSET2(model_type), AV_OPT_TYPE_INT, { .i64 = DDMT_SSD }, INT_MIN, INT_MAX, FLAGS, "model_type" },
63+ { "ssd", "output shape [1, 1, N, 7]", 0, AV_OPT_TYPE_CONST, { .i64 = DDMT_SSD }, 0, 0, FLAGS, "model_type" },
5764 { NULL }
5865 };
5966
6067 AVFILTER_DEFINE_CLASS(dnn_detect);
6168
62−static int dnn_detect_post_proc_ov(AVFrame *frame, DNNData *output, AVFilterContext *filter_ctx)
69+static int dnn_detect_post_proc_ssd(AVFrame *frame, DNNData *output, AVFilterContext *filter_ctx)
6370 {
6471 DnnDetectContext *ctx = filter_ctx->priv;
6572 float conf_threshold = ctx->confidence;
@@ -67,14 +74,12 @@ static int dnn_detect_post_proc_ov(AVFrame *frame, DNNData *output, AVFilterCont · 1 unmodified line
6774 int detect_size = output->width;
6875 float *detections = output->data;
6976 int nb_bboxes = 0;
70− AVFrameSideData *sd;
71− AVDetectionBBox *bbox;
7277 AVDetectionBBoxHeader *header;
78+ AVDetectionBBox *bbox;
7379
74− sd = av_frame_get_side_data(frame, AV_FRAME_DATA_DETECTION_BBOXES);
75− if (sd) {
76− av_log(filter_ctx, AV_LOG_ERROR, "already have bounding boxes in side data.\n");
77− return -1;
80+ if (output->width != 7) {
81+ av_log(filter_ctx, AV_LOG_ERROR, "Model output shape doesn't match ssd requirement.\n");
82+ return AVERROR(EINVAL);
7883 }
7984
8085 for (int i = 0; i < proposal_count; ++i) {
@@ -135,6 +140,29 @@ static int dnn_detect_post_proc_ov(AVFrame *frame, DNNData *output, AVFilterCont · 54 unmodified lines
135140 return 0;
136141 }
137142
143+static int dnn_detect_post_proc_ov(AVFrame *frame, DNNData *output, AVFilterContext *filter_ctx)
144+{
145+ AVFrameSideData *sd;
146+ DnnDetectContext *ctx = filter_ctx->priv;
147+ int ret = 0;
148+
149+ sd = av_frame_get_side_data(frame, AV_FRAME_DATA_DETECTION_BBOXES);
150+ if (sd) {
151+ av_log(filter_ctx, AV_LOG_ERROR, "already have bounding boxes in side data.\n");
152+ return -1;
153+ }
154+
155+ switch (ctx->model_type) {
156+ case DDMT_SSD:
157+ ret = dnn_detect_post_proc_ssd(frame, output, filter_ctx);
158+ if (ret < 0)
159+ return ret;
160+ break;
161+ }
162+
163+ return 0;
164+}
165+
138166 static int dnn_detect_post_proc_tf(AVFrame *frame, DNNData *output, AVFilterContext *filter_ctx)
139167 {
140168 DnnDetectContext *ctx = filter_ctx->priv;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t