PST SDK  5.2.0.0-0eac0f6
trackingtarget.py
1 """Tracking target example of the PST SDK
2 
3 This example shows how to work with tracking targets using the PST SDK.
4 Note that at this moment tracking targets can not be trained or
5 imported using the PST SDK. In order to add new tracking targets, please
6 use the PST Client together with the pstech.pstsdk.tracker.Tracker.enable_shared_memory()
7 function, or use the stand-alone PST Server to configure the tracking targets.
8 
9 Copyright PS-Tech B.V. All Rights Reserved.
10 """
11 import context
12 import time
13 import sys
14 import pstech.pstsdk.tracker as pst
15 import pstech.pstsdk.errors as psterrors
16 
17 # Control variable for main loop
18 running = True
19 
20 # Number of data points to grab before application termination
21 max_samples = 100
22 
23 # Global number of samples
24 samples = 0
25 
26 """Implementation of a tracker callback function
27 
28 Implementation of a tracker callback function. The callback_function
29 receives the data as soon as it becomes available.
30 
31 Args:
32  tracker_data: Object containing tracking information retrieved from tracker
33  status_message: Status message reported by the tracker.
34 
35 See Also:
36  pstech.pstdk.trackerdata.TrackerData
37  pstech.pstsdk.errors.EStatusMessage
38 """
39 def callback_function(tracker_data, status_message):
40  # Do something with the tracker data
41  global samples
42  global running
43 
44  if samples >= max_samples:
45  running = False
46 
47  for target_pose in tracker_data.targetlist:
48  print("Detected " + target_pose.name)
49 
50  samples += 1
51 
52 """Helper function to register the exit handler with the application"""
53 def register_exit_handler():
54  if sys.platform.startswith("linux"):
55  import signal
56  signal.signal(signal.SIGTERM, exit_handler)
57  signal.signal(signal.SIGHUP, exit_handler)
58  signal.signal(signal.SIGQUIT, exit_handler)
59  signal.signal(signal.SIGINT, exit_handler)
60  elif sys.platform.startswith("win"):
61  import win32api
62  win32api.SetConsoleCtrlHandler(exit_handler, True)
63 
64 """Implement the exit handler to shut-down the PST Tracker connection on application termination."""
65 def exit_handler(*args):
66  global running
67  pst.Tracker.shutdown()
68  running = False
69  return True
70 
71 def main():
72  if(len(sys.argv) < 2):
73  print("\nConfiguration Error: A camera configuration file needs to be specified. This file can be found in the Redist folder of your installation. "
74  "See the documentation of the Python bindings for more information.")
75  exit(0)
76 
77  # Register exit_handler for proper shutdown
78  register_exit_handler()
79 
80  try:
81  # Use Context Manager to prevent improper Tracker shutdown on errors.
82  # Create an instance of the Tracker object using the default configuration path and file names.
83  with pst.Tracker("", "","", sys.argv[1]) as tracker:
84 
85  # Print version number of the tracker server being used.
86  print("Running PST Server version " + tracker.get_version_info())
87 
88  # Register the listener object to the tracker server.
89  tracker.add_tracker_listener(callback_function)
90 
91  print("Put the Reference card in front of the PST in order to see tracking results.\n")
92 
93  # Start the tracker server.
94  tracker.start()
95 
96  # Perform a system check to see if the tracker server is running OK and print the result.
97  print("System check: " + str(tracker.system_check()))
98 
99  # Retrieve the list of registered tracking targets and print their names and current status (active or not).
100  targets = tracker.get_target_list()
101  print("Found " + str(len(targets)) + " tracking targets:")
102  for target_status in targets:
103  print(target_status.name + "\t" + str(target_status.status))
104  print("")
105 
106  # Enable the Reference target. Note that this will cause a TrackerError in case the Reference
107  # target has not been created. The Reference target can be created using the PST Client.
108  tracker.set_target_status("Reference", True)
109 
110  # Get the 3D marker positions making up the Reference device and display them.
111  # Note that this will cause a TrackerError in case the Reference target has not been created.
112  markers_list = tracker.get_target_markers("Reference")
113  print("3D marker positions making up the Reference target:")
114  for marker in markers_list:
115  print("x: " + str(marker[0]) + "\ty: " + str(marker[1]) + "\tz: " + str(marker[2]))
116  print("")
117 
118  # Main loop, wait for auto-termination.
119  while running:
120  time.sleep(0.1)
121 
122  except psterrors.TrackerError as err:
123  # Catch TrackerError exceptions and print error messages.
124  print(err.message)
125 
126 if __name__ == "__main__":
127  main()