def is_recognized_match(score, threshold, second_score=None, min_confidence=0.35, margin=0.05): """Return True only when the match is both below threshold and clearly stronger than alternatives. This prevents false attendance when the best gallery embedding is only slightly better than the others or when the distance is still too high for a confident match. """ if score is None: return False try: score = float(score) threshold = float(threshold) except (TypeError, ValueError): return False effective_threshold = min(threshold, min_confidence) if score > effective_threshold: return False if second_score is not None: try: second_score = float(second_score) except (TypeError, ValueError): return False if not (score + margin < second_score): return False return True