Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autotracker: minor efficiency tweaks #7163

Merged
merged 1 commit into from Jul 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
89 changes: 31 additions & 58 deletions frigate/ptz/autotrack.py
Expand Up @@ -119,7 +119,7 @@ def __init__(
self.config = config

def run(self):
while not self.stop_event.is_set():
while not self.stop_event.wait(1):
for camera_name, cam in self.config.cameras.items():
if cam.onvif.autotracking.enabled:
self.ptz_autotracker.camera_maintenance(camera_name)
Expand All @@ -128,7 +128,7 @@ def run(self):
if self.ptz_autotracker.tracked_object.get(camera_name):
self.ptz_autotracker.tracked_object[camera_name] = None
self.ptz_autotracker.tracked_object_previous[camera_name] = None
time.sleep(1)

logger.info("Exiting autotracker...")


Expand Down Expand Up @@ -199,66 +199,39 @@ def _autotracker_setup(self, cam, camera_name):
def _process_move_queue(self, camera):
while True:
try:
if self.move_queues[camera].qsize() > 1:
# Accumulate values since last moved
pan = 0
tilt = 0

while not self.move_queues[camera].empty():
frame_time, queued_pan, queued_tilt = self.move_queues[
camera
].queue[0]

# if we're receiving move requests during a PTZ move, ignore them
if ptz_moving_at_frame_time(
frame_time,
self.ptz_metrics[camera]["ptz_start_time"].value,
self.ptz_metrics[camera]["ptz_stop_time"].value,
):
self.move_queues[camera].get()

# instead of dequeueing this might be a good place to preemptively move based
# on an estimate - for fast moving objects, etc.
logger.debug(
f"Move queue: PTZ moving, dequeueing move request - frame time: {frame_time}, queued pan: {queued_pan}, queued tilt: {queued_tilt}, final pan: {pan}, final tilt: {tilt}"
)

else:
# TODO: this may need rethinking
logger.debug(
f"Move queue: PTZ NOT moving, frame time: {frame_time}, queued pan: {queued_pan}, queued tilt: {queued_tilt}, final pan: {pan}, final tilt: {tilt}"
)
_, queued_pan, queued_tilt = self.move_queues[camera].get()

# If exceeding the movement range, keep it in the queue and move now
if (
abs(pan + queued_pan) > 1.0
or abs(tilt + queued_tilt) > 1.0
):
logger.debug("Pan or tilt value exceeds 1.0")
break

pan += queued_pan
tilt += queued_tilt

else:
move_data = self.move_queues[camera].get()
frame_time, pan, tilt = move_data
move_data = self.move_queues[camera].get()
frame_time, pan, tilt = move_data

# if we're receiving move requests during a PTZ move, ignore them
if ptz_moving_at_frame_time(
frame_time,
self.ptz_metrics[camera]["ptz_start_time"].value,
self.ptz_metrics[camera]["ptz_stop_time"].value,
):
# instead of dequeueing this might be a good place to preemptively move based
# on an estimate - for fast moving objects, etc.
logger.debug(
f"Move queue: PTZ moving, dequeueing move request - frame time: {frame_time}, final pan: {pan}, final tilt: {tilt}"
)
continue

# on some cameras with cheaper motors it seems like small values can cause jerky movement
# TODO: double check, might not need this
if abs(pan) > 0.02 or abs(tilt) > 0.02:
self.onvif._move_relative(camera, pan, tilt, 1)
else:
logger.debug(f"Not moving, pan and tilt too small: {pan}, {tilt}")

# Wait until the camera finishes moving
while not self.ptz_metrics[camera]["ptz_stopped"].is_set():
# check if ptz is moving
self.onvif.get_camera_status(camera)
# on some cameras with cheaper motors it seems like small values can cause jerky movement
# TODO: double check, might not need this
if abs(pan) > 0.02 or abs(tilt) > 0.02:
self.onvif._move_relative(camera, pan, tilt, 1)
else:
logger.debug(
f"Not moving, pan and tilt too small: {pan}, {tilt}"
)

# Wait until the camera finishes moving
while not self.ptz_metrics[camera]["ptz_stopped"].is_set():
# check if ptz is moving
self.onvif.get_camera_status(camera)

except queue.Empty:
time.sleep(0.1)
continue

def _enqueue_move(self, camera, frame_time, pan, tilt):
move_data = (frame_time, pan, tilt)
Expand Down