Skip to content

NVR with realtime local object detection for IP cameras

LPR improvements (#14641)

Josh Hawkinsaf1eaac5ff1e
diff --git a/frigate/embeddings/alpr/alpr.py b/frigate/embeddings/alpr/alpr.py
index b91a50e3a5..16eba99898 100644
--- a/frigate/embeddings/alpr/alpr.py
+++ b/frigate/embeddings/alpr/alpr.py
@@ -13,6 +13,8 @@ from frigate.embeddings.embeddings import Embeddings
logger = logging.getLogger(__name__)
+MIN_PLATE_LENGTH = 3
+
class LicensePlateRecognition:
def __init__(
@@ -197,7 +199,22 @@ class LicensePlateRecognition:
average_confidences[original_idx] = average_confidence
areas[original_idx] = area
- return license_plates, average_confidences, areas
+ # Filter out plates that have a length of less than 3 characters
+ # Sort by area, then by plate length, then by confidence all desc
+ sorted_data = sorted(
+ [
+ (plate, conf, area)
+ for plate, conf, area in zip(
+ license_plates, average_confidences, areas
+ )
+ if len(plate) >= MIN_PLATE_LENGTH
+ ],
+ key=lambda x: (x[2], len(x[0]), x[1]),
+ reverse=True,
+ )
+
+ if sorted_data:
+ return map(list, zip(*sorted_data))
return [], [], []
diff --git a/frigate/embeddings/maintainer.py b/frigate/embeddings/maintainer.py
index a10357fc3a..ba4328b044 100644
--- a/frigate/embeddings/maintainer.py
+++ b/frigate/embeddings/maintainer.py
@@ -630,7 +630,11 @@ class EmbeddingMaintainer(threading.Thread):
logger.debug("No text detected")
return
- top_plate, top_char_confidences = license_plates[0], confidences[0]
+ top_plate, top_char_confidences, top_area = (
+ license_plates[0],
+ confidences[0],
+ areas[0],
+ )
avg_confidence = (
(sum(top_char_confidences) / len(top_char_confidences))
if top_char_confidences
@@ -641,6 +645,7 @@ class EmbeddingMaintainer(threading.Thread):
if id in self.detected_license_plates:
prev_plate = self.detected_license_plates[id]["plate"]
prev_char_confidences = self.detected_license_plates[id]["char_confidences"]
+ prev_area = self.detected_license_plates[id]["area"]
prev_avg_confidence = (
(sum(prev_char_confidences) / len(prev_char_confidences))
if prev_char_confidences
@@ -650,6 +655,7 @@ class EmbeddingMaintainer(threading.Thread):
# Define conditions for keeping the previous plate
shorter_than_previous = len(top_plate) < len(prev_plate)
lower_avg_confidence = avg_confidence <= prev_avg_confidence
+ smaller_area = top_area < prev_area
# Compare character-by-character confidence where possible
min_length = min(len(top_plate), len(prev_plate))
@@ -660,13 +666,13 @@ class EmbeddingMaintainer(threading.Thread):
)
worse_char_confidences = char_confidence_comparison >= min_length / 2
- if shorter_than_previous or (
+ if (shorter_than_previous or smaller_area) and (
lower_avg_confidence and worse_char_confidences
):
logger.debug(
f"Keeping previous plate. New plate stats: "
- f"length={len(top_plate)}, avg_conf={avg_confidence:.2f} "
- f"vs Previous: length={len(prev_plate)}, avg_conf={prev_avg_confidence:.2f}"
+ f"length={len(top_plate)}, avg_conf={avg_confidence:.2f}, area={top_area} "
+ f"vs Previous: length={len(prev_plate)}, avg_conf={prev_avg_confidence:.2f}, area={prev_area}"
)
return
@@ -699,6 +705,7 @@ class EmbeddingMaintainer(threading.Thread):
self.detected_license_plates[id] = {
"plate": top_plate,
"char_confidences": top_char_confidences,
+ "area": top_area,
}
def _create_thumbnail(self, yuv_frame, box, height=500) -> Optional[bytes]: