From patchwork Wed May 13 14:42:35 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lance Wang X-Patchwork-Id: 19664 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 8AD6144A624 for ; Wed, 13 May 2020 17:42:52 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 67CFF68803F; Wed, 13 May 2020 17:42:52 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pg1-f181.google.com (mail-pg1-f181.google.com [209.85.215.181]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id B5E7668077C for ; Wed, 13 May 2020 17:42:45 +0300 (EEST) Received: by mail-pg1-f181.google.com with SMTP id j21so7869086pgb.7 for ; Wed, 13 May 2020 07:42:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=nA9XUqTj/fcTg8BvSKMoLz/OrLVySt7FmYjv0/SMGbI=; b=tOfBxCZb4OGpJt8WYiuFc8DUzkZBXUZv30OlaCIvuWIe0tWEPtMXMh2mc8ebph23X+ vYitInLtpP8sUce9MZeynN0pgZTPIcXbIi31KA2f9gCMmCRNmdmCDQeQpifchSL8zryI S084q2FYiUsZk3tNaTLJyVlpe6D037Q+C8cfvGYYPQYbTKqCVRlRsl+7Y2CdGy7MMpm5 493doByh/kLsnBlXPCvtUbCqhjTpB/DD7Qi44a0AU92JuaAN+r6Y5M/zVlMMSPLZHbGy MlH/sczjDm8fXUPgn35Rm7hhM4zF245A0rWcOy7MqeZHWmYWlr7GHAyTxCeVCZRAiMDh GZ7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=nA9XUqTj/fcTg8BvSKMoLz/OrLVySt7FmYjv0/SMGbI=; b=R62IY2LiT6zr3NWq7NIxeAGMwU4CKa64Y5eno3JwBYZNQKqllkionakfg25taDtAgP xoZcdgbu8PIjXfphcqNw5Bg9cPOzuD/UU04wSissry3h4ZJeSEtCgp1YCW0d1kWP/0pb ET30ltax6wz9we2s9PA/plKVWMzfnanYNIAmyCfkjUq/Q/Y0uQVi7Zm6MyYc0kFn+iUf 73yUBq4xwVNL2bsoUUSqf1jYh9GeiklMG+VP3kcA2UTFxItPJioI1+TeJMB3Pv9Lr53I K26qE98AOt60jW+mhVmq6SE64ptdGiN2yX4jFJwSBa53heNuJ8EjD6XcAtobnM1hwL0j Hx8A== X-Gm-Message-State: AOAM531lsPP9QO7DGZ6YQC2AtjLMZn8J8F4Dv1i9tKqSi3RuTz8E83cX bY1DrNjjNWEu2s4kMQDbtj1HmTUx X-Google-Smtp-Source: ABdhPJyNUzXUiM93aHHSbjP+z2+wJCgncgWZEGzYfXocdbGhrAgdXzd1f2RHX6N9DoP97iGLmhvsjA== X-Received: by 2002:aa7:95a6:: with SMTP id a6mr8442012pfk.67.1589380963265; Wed, 13 May 2020 07:42:43 -0700 (PDT) Received: from vpn2.localdomain ([161.117.202.209]) by smtp.gmail.com with ESMTPSA id c184sm791410pfc.57.2020.05.13.07.42.41 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 13 May 2020 07:42:42 -0700 (PDT) From: lance.lmwang@gmail.com To: ffmpeg-devel@ffmpeg.org Date: Wed, 13 May 2020 22:42:35 +0800 Message-Id: <1589380957-23603-1-git-send-email-lance.lmwang@gmail.com> X-Mailer: git-send-email 1.8.3.1 Subject: [FFmpeg-devel] [PATCH 1/3] avfilter/vf_libopencv: add opencv HaarCascade classifier simple face detection filter X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 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 Cc: Limin Wang MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Limin Wang Signed-off-by: Limin Wang --- Have tested with opencv 2.4.13 and 3.4.10 with static link configure | 1 + doc/filters.texi | 29 ++++++++ libavfilter/vf_libopencv.c | 164 ++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 191 insertions(+), 3 deletions(-) diff --git a/configure b/configure index a45c0fb..99d008a 100755 --- a/configure +++ b/configure @@ -2123,6 +2123,7 @@ HEADERS_LIST=" machine_ioctl_meteor_h malloc_h opencv2_core_core_c_h + opencv2_objdetect_objdetect_c_h OpenGL_gl3_h poll_h sys_param_h diff --git a/doc/filters.texi b/doc/filters.texi index d19fd34..e50dcf3 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -14176,6 +14176,35 @@ other parameters is 0. These parameters correspond to the parameters assigned to the libopencv function @code{cvSmooth}. +@subsection facedetect +Face detection using Haar Feature-based Cascade Classifiers. + +The filter takes the following parameters: +@var{xml_model}|@var{qoffset}. + +@var{xml_model} is the path of pre-trained classifiers, The C API still +does not support the newer cascade format, please use the old format +haarcascade_frontalface_alt.xml which type_id is opencv-haar-classifier. + +@var{qoffset} +If you want export the detected faces by ROI side data in frame, please set the +parameters, See also the @ref{addroi} filter. The range of qoffset is from [-1.0, 1.0] + +By default the filter will report these metadata values if face are +detected: +@table @option +@item lavfi.facedetect.nb_faces +Display the detected face number + +@item lavfi.facedetect.face_id.x, lavfi.facedetect.face_id.y +Display x and y of every faces, face_id is the face index which is range +from [0, nb_faces-1] + +@item lavfi.facedetect.face_id.w, lavfi.facedetect.face_id.h +Display width and height of every faces, face_id is the face index +which is range from [0, nb_faces-1] +@end table + @section oscilloscope 2D Video Oscilloscope. diff --git a/libavfilter/vf_libopencv.c b/libavfilter/vf_libopencv.c index 8128030..b7a9282 100644 --- a/libavfilter/vf_libopencv.c +++ b/libavfilter/vf_libopencv.c @@ -1,5 +1,6 @@ /* * Copyright (c) 2010 Stefano Sabatini + * Copyright (c) 2020 Limin Wang * * This file is part of FFmpeg. * @@ -27,10 +28,16 @@ #if HAVE_OPENCV2_CORE_CORE_C_H #include #include +#if HAVE_OPENCV2_OBJECTDETECT_OBJECTDETECT_C_H +#include +#else +#include +#endif #else #include #include #endif + #include "libavutil/avstring.h" #include "libavutil/common.h" #include "libavutil/file.h" @@ -82,6 +89,7 @@ typedef struct OCVContext { int (*init)(AVFilterContext *ctx, const char *args); void (*uninit)(AVFilterContext *ctx); void (*end_frame_filter)(AVFilterContext *ctx, IplImage *inimg, IplImage *outimg); + void (*update_metadata)(AVFilterContext *ctx, AVFrame *frame); void *priv; } OCVContext; @@ -326,18 +334,152 @@ static void erode_end_frame_filter(AVFilterContext *ctx, IplImage *inimg, IplIma cvErode(inimg, outimg, dilate->kernel, dilate->nb_iterations); } +typedef struct FaceDetectContext { + char *xml_model; + CvHaarClassifierCascade* cascade; + CvMemStorage* storage; + int nb_faces; + CvSeq *faces; + int add_roi; + AVRational qoffset; +} FaceDetectContext; + +static av_cold int facedetect_init(AVFilterContext *ctx, const char *args) +{ + OCVContext *s = ctx->priv; + FaceDetectContext *facedetect = s->priv; + const char *buf = args; + double qoffset; + + if (args) { + facedetect->xml_model = av_get_token(&buf, "|"); + if (!facedetect->xml_model) { + av_log(ctx, AV_LOG_ERROR, "failed to get %s, %s\n", args, facedetect->xml_model); + return AVERROR(EINVAL); + } + + if (buf && sscanf(buf, "|%lf", &qoffset) == 1) { + if (qoffset < -1.0 || qoffset > 1.0) { + av_log(ctx, AV_LOG_ERROR, "failed to get valid qoffset(%f))\n", qoffset); + return AVERROR(EINVAL); + } + facedetect->add_roi = 1; + facedetect->qoffset = av_d2q(qoffset, 255); + } + } else { + av_log(ctx, AV_LOG_ERROR, "failed to get haarcascade_frontalface_alt.xml model file\n"); + return AVERROR(EINVAL); + } + + av_log(ctx, AV_LOG_VERBOSE, "xml_model: %s add_roi: %d qoffset: %d/%d\n", + facedetect->xml_model, facedetect->add_roi, facedetect->qoffset.num, facedetect->qoffset.den); + + facedetect->storage = cvCreateMemStorage(0); + if (!facedetect->storage) { + av_log(ctx, AV_LOG_ERROR, "cvCreateMemStorage() failed\n"); + return AVERROR(EINVAL); + } + cvClearMemStorage(facedetect->storage); + + facedetect->cascade = (CvHaarClassifierCascade*)cvLoad( facedetect->xml_model, NULL, NULL, NULL ); + if (!facedetect->cascade) { + av_log(ctx, AV_LOG_ERROR, "failed to load classifier cascade: %s \n", facedetect->xml_model); + return AVERROR(EINVAL); + } + + return 0; +} + +static av_cold void facedetect_uninit(AVFilterContext *ctx) +{ + OCVContext *s = ctx->priv; + FaceDetectContext *facedetect = s->priv; + + if (facedetect->cascade) + cvReleaseHaarClassifierCascade(&facedetect->cascade); + if (facedetect->storage) + cvReleaseMemStorage(&facedetect->storage); +} + +static void set_meta_int(AVDictionary **metadata, const char *key, int idx, int d) +{ + char value[128]; + char key2[128]; + + snprintf(value, sizeof(value), "%d", d); + snprintf(key2, sizeof(key2), "lavfi.facedetect.%d.%s", idx, key); + av_dict_set(metadata, key2, value, 0); +} + +static void facedetect_end_frame_filter(AVFilterContext *ctx, IplImage *inimg, IplImage *outimg) +{ + OCVContext *s = ctx->priv; + FaceDetectContext *facedetect = s->priv; + + facedetect->faces = cvHaarDetectObjects(inimg, facedetect->cascade, facedetect->storage, + 1.25, 3, CV_HAAR_DO_CANNY_PRUNING, + cvSize(inimg->width/16,inimg->height/16), cvSize(0,0)); + + facedetect->nb_faces = facedetect->faces ? facedetect->faces->total : 0; +} + +static void facedetect_update_metadata(AVFilterContext *ctx, AVFrame *out) +{ + OCVContext *s = ctx->priv; + FaceDetectContext *facedetect = s->priv; + AVRegionOfInterest *roi; + AVFrameSideData *sd; + AVBufferRef *roi_buf; + int i; + + if (facedetect->add_roi && facedetect->nb_faces > 0) { + sd = av_frame_new_side_data(out, AV_FRAME_DATA_REGIONS_OF_INTEREST, + facedetect->nb_faces * sizeof(AVRegionOfInterest)); + if (!sd) { + return AVERROR(ENOMEM); + } + roi = (AVRegionOfInterest*)sd->data; + for(i = 0; i < facedetect->nb_faces; i++ ) { + CvRect *r = (CvRect*) cvGetSeqElem(facedetect->faces, i); + + roi[i] = (AVRegionOfInterest) { + .self_size = sizeof(*roi), + .top = r->y, + .bottom = r->y + r->height, + .left = r->x, + .right = r->x + r->width, + .qoffset = facedetect->qoffset, + }; + } + } + + if (facedetect->nb_faces > 0) + av_dict_set_int(&out->metadata, "lavfi.facedetect.nb_faces", facedetect->nb_faces, 0); + + for(i = 0; i < facedetect->nb_faces; i++ ) { + CvRect *r = (CvRect*) cvGetSeqElem(facedetect->faces, i); + + set_meta_int(&out->metadata, "x", i, r->x); + set_meta_int(&out->metadata, "y", i, r->y); + set_meta_int(&out->metadata, "w", i, r->width); + set_meta_int(&out->metadata, "h", i, r->height); + } +} + typedef struct OCVFilterEntry { const char *name; size_t priv_size; int (*init)(AVFilterContext *ctx, const char *args); void (*uninit)(AVFilterContext *ctx); void (*end_frame_filter)(AVFilterContext *ctx, IplImage *inimg, IplImage *outimg); + void (*update_metadata)(AVFilterContext *ctx, AVFrame *frame); } OCVFilterEntry; static const OCVFilterEntry ocv_filter_entries[] = { - { "dilate", sizeof(DilateContext), dilate_init, dilate_uninit, dilate_end_frame_filter }, - { "erode", sizeof(DilateContext), dilate_init, dilate_uninit, erode_end_frame_filter }, - { "smooth", sizeof(SmoothContext), smooth_init, NULL, smooth_end_frame_filter }, + { "dilate", sizeof(DilateContext), dilate_init, dilate_uninit, dilate_end_frame_filter, NULL }, + { "erode", sizeof(DilateContext), dilate_init, dilate_uninit, erode_end_frame_filter, NULL }, + { "smooth", sizeof(SmoothContext), smooth_init, NULL, smooth_end_frame_filter, NULL }, + { "facedetect", sizeof(FaceDetectContext), facedetect_init, facedetect_uninit, facedetect_end_frame_filter, facedetect_update_metadata }, }; static av_cold int init(AVFilterContext *ctx) @@ -355,6 +497,7 @@ static av_cold int init(AVFilterContext *ctx) s->init = entry->init; s->uninit = entry->uninit; s->end_frame_filter = entry->end_frame_filter; + s->update_metadata = entry->update_metadata; if (!(s->priv = av_mallocz(entry->priv_size))) return AVERROR(ENOMEM); @@ -383,18 +526,33 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) AVFrame *out; IplImage inimg, outimg; + /* facedetect filter will passthrought the input frame */ + if (strcmp(s->name, "facedetect")) { out = ff_get_video_buffer(outlink, outlink->w, outlink->h); if (!out) { av_frame_free(&in); return AVERROR(ENOMEM); } av_frame_copy_props(out, in); + } else { + out = in; + } fill_iplimage_from_frame(&inimg , in , inlink->format); + + if (strcmp(s->name, "facedetect")) { fill_iplimage_from_frame(&outimg, out, inlink->format); s->end_frame_filter(ctx, &inimg, &outimg); fill_frame_from_iplimage(out, &outimg, inlink->format); + } else { + s->end_frame_filter(ctx, &inimg, NULL); + } + + if (s->update_metadata) { + s->update_metadata(ctx, out); + } + if (out != in) av_frame_free(&in); return ff_filter_frame(outlink, out); From patchwork Wed May 13 14:42:36 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lance Wang X-Patchwork-Id: 19665 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id C7B2C44A624 for ; Wed, 13 May 2020 17:42:53 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id AE870687FEC; Wed, 13 May 2020 17:42:53 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pl1-f196.google.com (mail-pl1-f196.google.com [209.85.214.196]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 0ABE568048D for ; Wed, 13 May 2020 17:42:47 +0300 (EEST) Received: by mail-pl1-f196.google.com with SMTP id u15so2949769plm.2 for ; Wed, 13 May 2020 07:42:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=97j+KiIpfzQRK6nI91KDawLEh0gZkDv+NwbWGh10UqU=; b=A28MnUqmjD3ftSeZzjk5JtH+BNzq+HEOXaftZfo+xWC/oHTsTb3l1lhDGLHsjYbjeZ cpRHNiyIUxpFsUklLqWF9triYUxcCuV37a9qUR29X6LUAjGMCY1zKtAl9Pq19XbWfylp dQBGjU876wKWLdJvmfvKCIGQ/zoQ+ofYqrf7qWk62CbyPbmaz7+1i9hmsknFAsp7acSX M4tLRMk/lFIVvGMDX/8+7sKNNfcvGWb8M1lIJCu942Tooj+zQTf4xPhMytKsGegv1ZHv /ocjUoPdhcKZpNbm2P0rSYFbcvlERZvhWvqeOh1tKTnJdfUn58az8Ht32dC1amXeGQnR +GCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=97j+KiIpfzQRK6nI91KDawLEh0gZkDv+NwbWGh10UqU=; b=FEJ0g50I3WKG4euNIc0AM5I/jRP5ZNffrZDdls7Fa6ovVfgeqIjxH8JX/Ob8nlPv76 atD0RUeYQjko1oPEIKnavT5ADrW/gJz2eDTorOBn48Y4l10EJ1OGFshvnOBRpvYt/m0O pR3+hdzldi/gQu/c/A4YeOeB20FmECZ2aVDFr5EgFuvM10uYgnk1Sp/cHxEtE8A4sOYy VfEV7NWtI+V0PPAZuk5Y0w8oiDcaDVYyTeXB4gPUt6Y+el7lZwNKBIjea4FoqNJGOBfp tuEcyOwa+84thrYpTvKEVe6RLCVeZEj4JNs7k6pkxIHE+gd3s9NYm2sHaV+mv6OBFpyY XnXw== X-Gm-Message-State: AGi0PubKsSxeLM1+grI1gPOzUWHBflNb8+Fa8zSsAxNBWB4XJD8L+W/B fjzKPSsh+vzXZcJpMTp//HeMkjvu X-Google-Smtp-Source: APiQypLoxD8ffTBT6fFhU1wZqo7KXlCr9p/QbsJGFEEJjC6zW9jzxW5AJ1UQL7eS4MA1Kw2LfBmHTw== X-Received: by 2002:a17:90a:8c98:: with SMTP id b24mr25678870pjo.226.1589380964922; Wed, 13 May 2020 07:42:44 -0700 (PDT) Received: from vpn2.localdomain ([161.117.202.209]) by smtp.gmail.com with ESMTPSA id c184sm791410pfc.57.2020.05.13.07.42.43 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 13 May 2020 07:42:44 -0700 (PDT) From: lance.lmwang@gmail.com To: ffmpeg-devel@ffmpeg.org Date: Wed, 13 May 2020 22:42:36 +0800 Message-Id: <1589380957-23603-2-git-send-email-lance.lmwang@gmail.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1589380957-23603-1-git-send-email-lance.lmwang@gmail.com> References: <1589380957-23603-1-git-send-email-lance.lmwang@gmail.com> Subject: [FFmpeg-devel] [PATCH 2/3] avfilter/vf_libopencv: reindent after last commit X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 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 Cc: Limin Wang MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Limin Wang Signed-off-by: Limin Wang --- libavfilter/vf_libopencv.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/libavfilter/vf_libopencv.c b/libavfilter/vf_libopencv.c index b7a9282..c8512cc 100644 --- a/libavfilter/vf_libopencv.c +++ b/libavfilter/vf_libopencv.c @@ -528,12 +528,12 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) /* facedetect filter will passthrought the input frame */ if (strcmp(s->name, "facedetect")) { - out = ff_get_video_buffer(outlink, outlink->w, outlink->h); - if (!out) { - av_frame_free(&in); - return AVERROR(ENOMEM); - } - av_frame_copy_props(out, in); + out = ff_get_video_buffer(outlink, outlink->w, outlink->h); + if (!out) { + av_frame_free(&in); + return AVERROR(ENOMEM); + } + av_frame_copy_props(out, in); } else { out = in; } @@ -541,9 +541,9 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) fill_iplimage_from_frame(&inimg , in , inlink->format); if (strcmp(s->name, "facedetect")) { - fill_iplimage_from_frame(&outimg, out, inlink->format); - s->end_frame_filter(ctx, &inimg, &outimg); - fill_frame_from_iplimage(out, &outimg, inlink->format); + fill_iplimage_from_frame(&outimg, out, inlink->format); + s->end_frame_filter(ctx, &inimg, &outimg); + fill_frame_from_iplimage(out, &outimg, inlink->format); } else { s->end_frame_filter(ctx, &inimg, NULL); } @@ -553,7 +553,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) } if (out != in) - av_frame_free(&in); + av_frame_free(&in); return ff_filter_frame(outlink, out); } From patchwork Wed May 13 14:42:37 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lance Wang X-Patchwork-Id: 19666 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id E1D1844A624 for ; Wed, 13 May 2020 17:42:54 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C841A6898C0; Wed, 13 May 2020 17:42:54 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pf1-f194.google.com (mail-pf1-f194.google.com [209.85.210.194]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 74E44687F7D for ; Wed, 13 May 2020 17:42:48 +0300 (EEST) Received: by mail-pf1-f194.google.com with SMTP id y25so8092814pfn.5 for ; Wed, 13 May 2020 07:42:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=7f4WYiLWcUNolIgLGdaKnzN2bmxJfu2q36a4H5Uz37g=; b=iHKoZ/dLxgvRBsL1WPz9RhTUFYay1QvlGju3T/BSQ0R77O61382MzRso7Wlt+X6npn +RbM6cKGWtR96Du9stKgQezoct6oa9rXyii495PK7zJmuIuEz8fq+2aAcisKfmxwzNdr JQWm5qIpySTuwUlKYotTWGcWH/hnZdefck3UhquR8lVU4UHCY4MwJ/Bt57Acg7bjoK6w LC5V7JwR6ufY6fIMDGSybu/IMZFIscTTU/oRKJktX8Uc4XAKzJWumv8lYQtjyUXef9w7 XtWMVr2Z1cxqJ2CnOSZZpuAuYXO6lkok/7OM2fnENl8S60h7zpOoTpXHVBVyOFdqUFzL dS/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=7f4WYiLWcUNolIgLGdaKnzN2bmxJfu2q36a4H5Uz37g=; b=Ypw8ez3u6IgL7mu0gosOPkSHRMb5+mtctkfe5IpM+nBv3+z37U3hM3nGeersMlmWKs RnLw302hcq29Qcnzv/aIT3+S670BGGH6gTHqElCT4b2Qyyk/dEJFL+DsaAFU5UHInwyp IH46TW/fROWbMWOoXWdW8tu4hyiyUr6T16dAJHOnco4Onqgq+JXbIFa0GM4DK43LwHw9 YDsEE8aDbYxdMvV59sNqJpxKQ4w0ZNkxVpYuk26W8+9J4HPxRL677draeQrfQDhot4AW 5WtRCj30/6vY23BWL9Eq6XEhuX0IqbxHB3s1YNMsdw1MqHSXY/fOp5BKQYVcGdEEDp3+ JBOw== X-Gm-Message-State: AOAM530ml7L9qlZkLvUZldFZuevSGSv9tATkKHKbcLhD2gI53792UEdq QqJW6v1SGobLPok+N82MoxDwSzGi X-Google-Smtp-Source: ABdhPJy5HDdWKbspg9Eh2IofPYEdMw/LcivCu4yZneRzEif6Aa4GWWEqbBzQPeOPNSd5N1heFz4vPQ== X-Received: by 2002:a62:fc52:: with SMTP id e79mr2926051pfh.297.1589380966568; Wed, 13 May 2020 07:42:46 -0700 (PDT) Received: from vpn2.localdomain ([161.117.202.209]) by smtp.gmail.com with ESMTPSA id c184sm791410pfc.57.2020.05.13.07.42.45 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 13 May 2020 07:42:46 -0700 (PDT) From: lance.lmwang@gmail.com To: ffmpeg-devel@ffmpeg.org Date: Wed, 13 May 2020 22:42:37 +0800 Message-Id: <1589380957-23603-3-git-send-email-lance.lmwang@gmail.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1589380957-23603-1-git-send-email-lance.lmwang@gmail.com> References: <1589380957-23603-1-git-send-email-lance.lmwang@gmail.com> Subject: [FFmpeg-devel] [PATCH 3/3] avfilter/vf_drawbox: support draw specific face by facedetect metadata X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 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 Cc: Limin Wang MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Limin Wang Signed-off-by: Limin Wang --- doc/filters.texi | 10 ++++++++++ libavfilter/vf_drawbox.c | 27 +++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/doc/filters.texi b/doc/filters.texi index e50dcf3..a1a4e81 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -9336,6 +9336,10 @@ See below for the list of accepted constants. Applicable if the input has alpha. With value @code{1}, the pixels of the painted box will overwrite the video's color and alpha pixels. Default is @code{0}, which composites the box onto the input, leaving the video's alpha intact. + +@item face +Draw the box by the facedetect metadata for the specific face. + @end table The parameters for @var{x}, @var{y}, @var{w} and @var{h} and @var{t} are expressions containing the @@ -9404,6 +9408,12 @@ Draw a 2-pixel red 2.40:1 mask: @example drawbox=x=-t:y=0.5*(ih-iw/2.4)-t:w=iw+t*2:h=iw/2.4+t*2:t=2:c=red @end example + +@item +draw the box with red color for the first face by metadata if its postion is detected: +@example +ocv=filter_name=facedetect:filter_params=facedetect=./haarcascade_frontalface_alt.xml,drawbox=face=0:color=red +@end example @end itemize @subsection Commands diff --git a/libavfilter/vf_drawbox.c b/libavfilter/vf_drawbox.c index 21d520e..239a149 100644 --- a/libavfilter/vf_drawbox.c +++ b/libavfilter/vf_drawbox.c @@ -81,6 +81,7 @@ typedef struct DrawBoxContext { char *t_expr; ///< expression for thickness int have_alpha; int replace; + int face; } DrawBoxContext; static const int NUM_EXPR_EVALS = 5; @@ -220,6 +221,31 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *frame) int plane, x, y, xb = s->x, yb = s->y; unsigned char *row[4]; + if (s->face >= 0) { + AVDictionaryEntry *ex, *ey, *ew, *eh; + char key2[128]; + AVDictionary *metadata = frame->metadata; + + snprintf(key2, sizeof(key2), "lavfi.facedetect.%d.%s", s->face, "x"); + ex = av_dict_get(metadata, key2, NULL, AV_DICT_MATCH_CASE); + + snprintf(key2, sizeof(key2), "lavfi.facedetect.%d.%s", s->face, "y"); + ey = av_dict_get(metadata, key2, NULL, AV_DICT_MATCH_CASE); + + snprintf(key2, sizeof(key2), "lavfi.facedetect.%d.%s", s->face, "w"); + ew = av_dict_get(metadata, key2, NULL, AV_DICT_MATCH_CASE); + + snprintf(key2, sizeof(key2), "lavfi.facedetect.%d.%s", s->face, "h"); + eh = av_dict_get(metadata, key2, NULL, AV_DICT_MATCH_CASE); + + if (ex && ey && ew && eh) { + xb = s->x = strtol(ex->value, NULL, 10); + yb = s->y = strtol(ey->value, NULL, 10); + s->w = strtol(ew->value, NULL, 10); + s->h = strtol(eh->value, NULL, 10); + } + } + if (s->have_alpha && s->replace) { for (y = FFMAX(yb, 0); y < frame->height && y < (yb + s->h); y++) { row[0] = frame->data[0] + y * frame->linesize[0]; @@ -323,6 +349,7 @@ static const AVOption drawbox_options[] = { { "thickness", "set the box thickness", OFFSET(t_expr), AV_OPT_TYPE_STRING, { .str="3" }, 0, 0, FLAGS }, { "t", "set the box thickness", OFFSET(t_expr), AV_OPT_TYPE_STRING, { .str="3" }, 0, 0, FLAGS }, { "replace", "replace color & alpha", OFFSET(replace), AV_OPT_TYPE_BOOL, { .i64=0 }, 0, 1, FLAGS }, + { "face", "set which face to draw with metadata", OFFSET(face), AV_OPT_TYPE_INT, { .i64=-1 }, -1, 256, FLAGS }, { NULL } };