In order to be able to run this example, the PST Tracker has to be initialized first. This can be done by starting the PST-Server and the PST-Client application and making sure the calibration files have been downloaded and a tracking target is available. The tracking target can be the default Reference target or a newly trained or imported target. For more information, please see the Initialization section or check the PST Manual.
When compiling and running this example, please make sure that the required dependencies are copied from the Development\Redist
directory into the build directory.
"""Listener example of the PST SDK
This example shows how to implement the PST SDK using the Tracker class
and how to receive data by implementing a callback function. The example
initializes the PST Tracker and grabs 100 data points.
In order to be able to run this example, the PST Tracker has to be initialized first.
This can be done by starting the PST-Server and the PST-Client application and making
sure the calibration files have been downloaded and a tracking target is available.
The tracking target can be the default Reference target or a newly trained or imported
target. For more information, please see the Initialization section of the PST SDK manual
or check the PST Manual.
Copyright PS-Tech B.V. All Rights Reserved.
"""
import context
import time
import sys
running = True
max_samples = 100
samples = 0
"""Helper function for clear printing of 4x4 matrices."""
def print_matrix(matrix):
idx = 0
for y in range(4):
for x in range(4):
print(str(matrix[x + y * 4]), end="\t")
print("")
print("\n")
"""Implementation of a tracker callback function
Implementation of a tracker callback function. The callback_function
receives the data as soon as it becomes available and prints the tracking
target pose to the command line.
Args:
tracker_data: Object containing tracking information retrieved from tracker
status_message: Status message reported by the tracker.
See Also:
pstech.pstdk.trackerdata.TrackerData
pstech.pstsdk.errors.EStatusMessage
"""
def callback_function(tracker_data, status_message):
global samples
global running
if samples >= max_samples:
running = False
if len(tracker_data.targetlist) > 0:
for target_pose in tracker_data.targetlist:
print("Pose for " + target_pose.name)
print_matrix(target_pose.pose)
samples += 1
"""Helper function to register the exit handler with the application"""
def register_exit_handler():
if sys.platform.startswith("linux"):
import signal
signal.signal(signal.SIGTERM, exit_handler)
signal.signal(signal.SIGHUP, exit_handler)
signal.signal(signal.SIGQUIT, exit_handler)
signal.signal(signal.SIGINT, exit_handler)
elif sys.platform.startswith("win"):
import win32api
win32api.SetConsoleCtrlHandler(exit_handler, True)
"""Implement the exit handler to shut-down the PST Tracker connection on application termination."""
def exit_handler(*args):
global running
pst.Tracker.shutdown()
running = False
return True
def main():
if(len(sys.argv) < 2):
print("\nConfiguration Error: A camera configuration file needs to be specified. This file can be found in the Redist folder of your installation. "
"See the documentation of the Python bindings for more information.")
exit(0)
register_exit_handler()
try:
with pst.Tracker("", "","", sys.argv[1]) as tracker:
if(len(tracker.get_uncalibrated_camera_urls()) > 0):
print("\nNo calibration information could be found in the configuration directory.\n"
"Please use the PST Server and PST Client application to initialize the PST Tracker and create/import a tracking target.\n"
"More information can be found in the Initialization section of the PST SDK manual and the PST Manual.\n"
)
sys.exit()
print("Running PST Server version " + tracker.get_version_info())
tracker.add_tracker_listener(callback_function)
print("Put the Reference card in front of the PST in order to see tracking results.\n")
tracker.start()
print("System check: " + str(tracker.system_check()))
tracker.set_framerate(30)
print("Frame rate set to: " + str(tracker.get_framerate()))
while running:
time.sleep(0.1)
except psterrors.TrackerError as err:
print(err.message)
if __name__ == "__main__":
main()