[FFmpeg-devel,v5,3/3] vf_find_rect.c: improve the find object detect performance

Submitted by lance.lmwang@gmail.com on June 12, 2019, 10:57 a.m.

Details

Message ID 20190612105731.43622-3-lance.lmwang@gmail.com
State New
Headers show

Commit Message

lance.lmwang@gmail.com June 12, 2019, 10:57 a.m.
From: Limin Wang <lance.lmwang@gmail.com>

Test with my samples with same test command, the detect function is OK, however the benchmark result is from 16fps to 400fps

Signed-off-by: Limin Wang <lance.lmwang@gmail.com>
---
 libavfilter/vf_find_rect.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

Patch hide | download patch | download mbox

diff --git a/libavfilter/vf_find_rect.c b/libavfilter/vf_find_rect.c
index ed15885bc2..d7da224c10 100644
--- a/libavfilter/vf_find_rect.c
+++ b/libavfilter/vf_find_rect.c
@@ -32,6 +32,7 @@ 
 #include "lswsutils.h"
 
 #define MAX_MIPMAPS 5
+#define MIN_THRESHOLD (0.05)
 
 typedef struct FOCContext {
     AVClass *class;
@@ -42,6 +43,7 @@  typedef struct FOCContext {
     int xmin, ymin, xmax, ymax;
     char *obj_filename;
     int last_x, last_y;
+    float last_best_score;
     AVFrame *obj_frame;
     AVFrame *needle_frame[MAX_MIPMAPS];
     AVFrame *haystack_frame[MAX_MIPMAPS];
@@ -190,8 +192,14 @@  static int filter_frame(AVFilterLink *inlink, AVFrame *in)
                         FFMIN(foc->ymax, foc->last_y + 8),
                         &best_x, &best_y, 1.0);
 
-    best_score = search(foc, 0, foc->mipmaps - 1, foc->xmin, foc->xmax, foc->ymin, foc->ymax,
+    if (FFABS(best_score - foc->last_best_score) > MIN_THRESHOLD && best_score > MIN_THRESHOLD) {
+        best_score = search(foc, 0, foc->mipmaps - 1, foc->xmin, foc->xmax, foc->ymin, foc->ymax,
                         &best_x, &best_y, best_score);
+    } else {
+        /* prefer to use last x and y for best xy to avoid small shift of object */
+        best_x = foc->last_x;
+        best_y = foc->last_y;
+    }
 
     for (i=0; i<MAX_MIPMAPS; i++) {
         av_frame_free(&foc->haystack_frame[i]);
@@ -204,6 +212,7 @@  static int filter_frame(AVFilterLink *inlink, AVFrame *in)
     av_log(ctx, AV_LOG_DEBUG, "Found at %d %d score %f\n", best_x, best_y, best_score);
     foc->last_x = best_x;
     foc->last_y = best_y;
+    foc->last_best_score = best_score;
 
     av_frame_make_writable(in);
 
@@ -271,6 +280,10 @@  static av_cold int init(AVFilterContext *ctx)
         }
     }
 
+    foc->last_x = 0;
+    foc->last_y = 0;
+    foc->last_best_score = 1;
+
     return 0;
 error:
     av_freep(&tmp_data[0]);