>From 499de7be409ba7668defac9214a8343344bd21ee Mon Sep 17 00:00:00 2001
From: MaximilianKaindl <m.kaindl0208@gmail.com>
Date: Wed, 12 Mar 2025 10:10:58 +0100
Subject: [PATCH v2 FFmpeg 21/20] libavfilter: classify fix category post_processing
 with very low temperature
To: ffmpeg-devel@ffmpeg.org

Signed-off-by: MaximilianKaindl <m.kaindl0208@gmail.com>
---
 libavfilter/avf_dnn_classify.c | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)

diff --git a/libavfilter/avf_dnn_classify.c b/libavfilter/avf_dnn_classify.c
index 7b469e3af0..ade55858ad 100644
--- a/libavfilter/avf_dnn_classify.c
+++ b/libavfilter/avf_dnn_classify.c
@@ -34,6 +34,7 @@
 #include "libavutil/opt.h"
 #include "libavutil/time.h"
 #include "video.h"
+#include <float.h>

 /*
     Labels that are being used to classify the image
@@ -579,7 +580,7 @@ static int get_category_total_label_count(CategoryClassifcationContext *cat_ctx,
 static CategoryContext *get_best_category(CategoriesContext *categories_ctx, float *probabilities)
 {
     CategoryContext *best_category = NULL;
-    float best_probability = -1.0f;
+    float best_probability = FLT_MIN;
     int prob_offset = 0;

     // Calculate total probability for each category
@@ -592,7 +593,7 @@ static CategoryContext *get_best_category(CategoriesContext *categories_ctx, flo
             category->total_probability += probabilities[prob_offset + label_idx];
         }

-        if (category->total_probability > best_probability) {
+        if (category->total_probability > best_probability && category->total_probability > 0.0f) {
             best_probability = category->total_probability;
             best_category = category;
         }
@@ -761,7 +762,8 @@ static int post_proc_clxp_categories(AVFrame *frame, DNNData *output, uint32_t b
         av_freep(&ctx_labels);
         return AVERROR(ENOMEM);
     }
-
+
+    int category_count = 0;
     // Process each context
     for (int ctx_idx = 0; ctx_idx < cat_class_ctx->num_contexts; ctx_idx++) {
         CategoriesContext *categories_ctx = cat_class_ctx->category_units[ctx_idx];
@@ -773,20 +775,22 @@ static int post_proc_clxp_categories(AVFrame *frame, DNNData *output, uint32_t b
         // Find best category in context
         best_category = get_best_category(categories_ctx, probabilities + prob_offset);
         if (!best_category || !best_category->name) {
-            av_log(filter_ctx, AV_LOG_ERROR, "Invalid best category at context %d\n", ctx_idx);
+            // No category classification found
             continue;
         }

         // Copy category name instead of assigning pointer
-        av_strlcpy(ctx_labels[ctx_idx], best_category->name, AV_DETECTION_BBOX_LABEL_NAME_MAX_SIZE);
-        ctx_probabilities[ctx_idx] = best_category->total_probability;
+        av_strlcpy(ctx_labels[category_count], best_category->name, AV_DETECTION_BBOX_LABEL_NAME_MAX_SIZE);
+        ctx_probabilities[category_count] = best_category->total_probability;

         prob_offset += categories_ctx->label_count;
+        category_count++;
+    }
+    if(category_count > 0){
+        // Fill bbox with best categories
+        ret = fill_detection_bbox_with_best_labels(ctx_labels, ctx_probabilities, cat_class_ctx->num_contexts, bbox,
+            AV_NUM_DETECTION_BBOX_CLASSIFY, ctx->confidence);
     }
-
-    // Fill bbox with best labels
-    ret = fill_detection_bbox_with_best_labels(ctx_labels, ctx_probabilities, cat_class_ctx->num_contexts, bbox,
-                                                AV_NUM_DETECTION_BBOX_CLASSIFY, ctx->confidence);

     // Clean up
     for (int i = 0; i < cat_class_ctx->num_contexts; i++) {
-- 
2.34.1

