FFmpeg: libavfilter/vf_dnn_classify.c Source File
Go to the documentation of this file. 1 /*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 /**
20 * @file
21 * implementing an classification filter using deep learning networks.
22 */
23
33
43
44 #define OFFSET(x) offsetof(DnnClassifyContext, dnnctx.x)
45 #define OFFSET2(x) offsetof(DnnClassifyContext, x)
46 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
49 #if (CONFIG_LIBOPENVINO == 1)
51 #endif
57 };
58
60
62 {
64 float conf_threshold =
ctx->confidence;
67 float *classifications;
68 uint32_t label_id;
69 float confidence;
72 if (output_size <= 0) {
73 return -1;
74 }
75
77 if (!sd) {
79 return -1;
80 }
82
83 if (bbox_index == 0) {
86 }
87
88 classifications =
output->data;
89 label_id = 0;
90 confidence= classifications[0];
91 for (
int i = 1;
i < output_size;
i++) {
92 if (classifications[
i] > confidence) {
94 confidence= classifications[
i];
95 }
96 }
97
98 if (confidence < conf_threshold) {
99 return 0;
100 }
101
104
105 if (
ctx->labels && label_id < ctx->label_count) {
107 } else {
109 }
110
112
113 return 0;
114 }
115
117 {
118 for (
int i = 0;
i <
ctx->label_count;
i++) {
120 }
121 ctx->label_count = 0;
123 }
124
126 {
127 int line_len;
128 FILE *file;
130
132 if (!file){
135 }
136
137 while (!feof(file)) {
138 char *label;
139 char buf[256];
140 if (!fgets(buf, 256, file)) {
141 break;
142 }
143
144 line_len = strlen(buf);
145 while (line_len) {
146 int i = line_len - 1;
147 if (buf[
i] ==
'\n' || buf[
i] ==
'\r' || buf[
i] ==
' ') {
149 line_len--;
150 } else {
151 break;
152 }
153 }
154
155 if (line_len == 0) // empty line
156 continue;
157
160 fclose(file);
162 }
163
165 if (!label) {
167 fclose(file);
169 }
170
173 fclose(file);
176 }
177 }
178
179 fclose(file);
180 return 0;
181 }
182
184 {
190
191 if (
ctx->labels_filename) {
193 }
194 return 0;
195 }
196
204 };
205
207 {
211
214 return -1;
215 }
216
217 do {
225 if (out_pts)
226 *out_pts = in_frame->
pts +
pts;
227 }
230
231 return 0;
232 }
233
235 {
242 int got_frame = 0;
243 int async_state;
244
246
247 do {
248 // drain all input frames
255 }
256 }
258
259 // drain all processed frames
260 do {
268 got_frame = 1;
269 }
271
272 // if frame got, schedule to next filter
273 if (got_frame)
274 return 0;
275
282 }
283 }
284
286
287 return 0;
288 }
289
291 {
295 }
296
298 .
name =
"dnn_classify",
306 .priv_class = &dnn_classify_class,
308 };
static int dnn_classify_flush_frame(AVFilterLink *outlink, int64_t pts, int64_t *out_pts)
static void free_classify_labels(DnnClassifyContext *ctx)
AVPixelFormat
Pixel format.
Filter the word "frame" indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVFrameSideData * av_frame_get_side_data(const AVFrame *frame, enum AVFrameSideDataType type)
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
#define AVERROR_EOF
End of file.
#define FILTER_PIXFMTS_ARRAY(array)
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce output
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
This structure describes decoded (raw) audio or video data.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
AVFILTER_DEFINE_CLASS(dnn_classify)
const char * name
Filter name.
A link between two filters.
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link's FIFO and update the link's stats.
#define AV_DETECTION_BBOX_LABEL_NAME_MAX_SIZE
void * priv
private data for use by the filter
static int dnn_classify_post_proc(AVFrame *frame, DNNData *output, uint32_t bbox_index, AVFilterContext *filter_ctx)
static FilteringContext * filter_ctx
static av_always_inline AVDetectionBBox * av_get_detection_bbox(const AVDetectionBBoxHeader *header, unsigned int idx)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static const AVOption dnn_classify_options[]
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
AVRational classify_confidences[AV_NUM_DETECTION_BBOX_CLASSIFY]
DNNAsyncStatusType ff_dnn_get_result(DnnContext *ctx, AVFrame **in_frame, AVFrame **out_frame)
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
int av_usleep(unsigned usec)
Sleep for a period of time.
#define AV_PIX_FMT_GRAYF32
#define FILTER_INPUTS(array)
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option keep it simple and lowercase description are in without and describe what they for example set the foo of the bar offset is the offset of the field in your context
Describe the class of an AVClass context structure.
char classify_labels[AV_NUM_DETECTION_BBOX_CLASSIFY][AV_DETECTION_BBOX_LABEL_NAME_MAX_SIZE]
int ff_dnn_execute_model_classification(DnnContext *ctx, AVFrame *in_frame, AVFrame *out_frame, const char *target)
static av_cold int dnn_classify_init(AVFilterContext *context)
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
int ff_dnn_flush(DnnContext *ctx)
int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
Test and acknowledge the change of status on the link.
#define DNN_COMMON_OPTIONS
int(* init)(AVBSFContext *ctx)
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
static AVRational av_make_q(int num, int den)
Create an AVRational.
AVFilterContext * src
source filter
static const uint8_t header[24]
FF_FILTER_FORWARD_WANTED(outlink, inlink)
const AVFilter ff_vf_dnn_classify
static void uninit(AVBSFContext *ctx)
#define i(width, name, range_min, range_max)
static av_cold void dnn_classify_uninit(AVFilterContext *context)
FILE * avpriv_fopen_utf8(const char *path, const char *mode)
Open a file using a UTF-8 filename.
@ AV_PIX_FMT_NV12
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
size_t av_strlcat(char *dst, const char *src, size_t size)
Append the string src to the string dst, but to a total length of no more than size - 1 bytes,...
int av_dynarray_add_nofree(void *tab_ptr, int *nb_ptr, void *elem)
Add an element to a dynamic array.
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
static enum AVPixelFormat pix_fmts[]
char * av_strdup(const char *s)
Duplicate a string.
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Structure to hold side data for an AVFrame.
#define FILTER_OUTPUTS(array)
int ff_dnn_init(DnnContext *ctx, DNNFunctionType func_type, AVFilterContext *filter_ctx)
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
static int dnn_classify_activate(AVFilterContext *filter_ctx)
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
void ff_dnn_uninit(DnnContext *ctx)
static int read_classify_label_file(AVFilterContext *context)
int ff_dnn_set_classify_post_proc(DnnContext *ctx, ClassifyPostProc post_proc)
@ AV_FRAME_DATA_DETECTION_BBOXES
Bounding boxes for object detection and classification, as described by AVDetectionBBoxHeader.
Generated on Thu Sep 26 2024 23:15:36 for FFmpeg by
doxygen
1.8.17