Skip to content

NVR with realtime local object detection for IP cameras

Add stationary scan duration for LPR (#20444)

Josh Hawkinsa2ad77c36e78
diff --git a/docs/docs/configuration/license_plate_recognition.md b/docs/docs/configuration/license_plate_recognition.md
index e1095ff082..1b5f6aa290 100644
--- a/docs/docs/configuration/license_plate_recognition.md
+++ b/docs/docs/configuration/license_plate_recognition.md
@@ -5,7 +5,7 @@ title: License Plate Recognition (LPR)
Frigate can recognize license plates on vehicles and automatically add the detected characters to the `recognized_license_plate` field or a known name as a `sub_label` to tracked objects of type `car` or `motorcycle`. A common use case may be to read the license plates of cars pulling into a driveway or cars passing by on a street.
-LPR works best when the license plate is clearly visible to the camera. For moving vehicles, Frigate continuously refines the recognition process, keeping the most confident result. However, LPR does not run on stationary vehicles.
+LPR works best when the license plate is clearly visible to the camera. For moving vehicles, Frigate continuously refines the recognition process, keeping the most confident result. When a vehicle becomes stationary, LPR continues to run for a short time after to attempt recognition.
When a plate is recognized, the details are:
diff --git a/frigate/data_processing/common/license_plate/mixin.py b/frigate/data_processing/common/license_plate/mixin.py
index d2083afec3..80a169c258 100644
--- a/frigate/data_processing/common/license_plate/mixin.py
+++ b/frigate/data_processing/common/license_plate/mixin.py
@@ -48,6 +48,9 @@ class LicensePlateProcessingMixin:
MODEL_CACHE_DIR, "paddleocr-onnx", "ppocr_keys_v1.txt"
)
)
+ # process plates that are stationary and have no position changes for 5 seconds
+ self.stationary_scan_duration = 5
+
self.batch_size = 6
# Object config
@@ -1269,22 +1272,40 @@ class LicensePlateProcessingMixin:
)
return
- # don't run for stationary car objects
- if obj_data.get("stationary") == True:
- logger.debug(
- f"{camera}: Not a processing license plate for a stationary car/motorcycle object."
- )
- return
-
- # don't run for objects with no position changes
- # this is the initial state after registering a new tracked object
+ # don't run for non-stationary objects with no position changes to avoid processing uncertain moving objects
+ # zero position_changes is the initial state after registering a new tracked object
# LPR will run 2 frames after detect.min_initialized is reached
- if obj_data.get("position_changes", 0) == 0:
+ if obj_data.get("position_changes", 0) == 0 and not obj_data.get(
+ "stationary", False
+ ):
logger.debug(
- f"{camera}: Plate detected in {self.config.cameras[camera].detect.min_initialized + 1} concurrent frames, LPR frame threshold ({self.config.cameras[camera].detect.min_initialized + 2})"
+ f"{camera}: Skipping LPR for non-stationary {obj_data['label']} object {id} with no position changes. (Detected in {self.config.cameras[camera].detect.min_initialized + 1} concurrent frames, threshold to run is {self.config.cameras[camera].detect.min_initialized + 2} frames)"
)
return
+ # run for stationary objects for a limited time after they become stationary
+ if obj_data.get("stationary") == True:
+ threshold = self.config.cameras[camera].detect.stationary.threshold
+ if obj_data.get("motionless_count", 0) >= threshold:
+ frames_since_stationary = (
+ obj_data.get("motionless_count", 0) - threshold
+ )
+ fps = self.config.cameras[camera].detect.fps
+ time_since_stationary = frames_since_stationary / fps
+
+ # only print this log for a short time to avoid log spam
+ if (
+ self.stationary_scan_duration
+ < time_since_stationary
+ <= self.stationary_scan_duration + 1
+ ):
+ logger.debug(
+ f"{camera}: {obj_data.get('label', 'An')} object {id} has been stationary for > {self.stationary_scan_duration} seconds, skipping LPR."
+ )
+
+ if time_since_stationary > self.stationary_scan_duration:
+ return
+
license_plate: Optional[dict[str, Any]] = None
if "license_plate" not in self.config.cameras[camera].objects.track:
@@ -1424,7 +1445,7 @@ class LicensePlateProcessingMixin:
license_plate_frame,
)
- logger.debug(f"{camera}: Running plate recognition.")
+ logger.debug(f"{camera}: Running plate recognition for id: {id}.")
# run detection, returns results sorted by confidence, best first
start = datetime.datetime.now().timestamp()