PST SDK  6.0.0.0-272350a
images.py
1 """Images example of the PST SDK
2 
3 This example shows how to enable image transfer on the PST Tracker and how to use
4 the PST SDK to retrieve images. Images are 8 bit grayscale and are stored as an
5 unsigned byte array without memory alignment or padding.
6 
7 Copyright PS-Tech B.V. All Rights Reserved.
8 """
9 import context
10 import time
11 import sys
12 import pstech.pstsdk.tracker as pst
13 import pstech.pstsdk.errors as psterrors
14 
15 # Control variable for main loop
16 running = True
17 
18 # Number of data points to grab before application termination
19 max_samples = 100
20 
21 # Global number of samples
22 samples = 0
23 
24 """Implementation of the pst.Listener class to receive tracking data."""
25 class MyListener(pst.Listener):
26 
27  """Implementation of a tracker data callback function
28 
29  Implementation of a tracker data callback function. The on_tracker_data
30  function receives the data as soon as it becomes available.
31 
32  Args:
33  tracker_data: Object containing tracking information retrieved from tracker
34  status_message: Status message reported by the tracker.
35 
36  See Also:
37  pstech.pstdk.trackerdata.TrackerData
38  pstech.pstsdk.errors.EStatusMessage
39  """
40  def on_tracker_data(self, tracker_data, status_message):
41  global samples
42  global running
43 
44  if samples >= max_samples:
45  running = False
46 
47  samples += 1
48  # Do something here with the received data
49 
50 """Helper function to register the exit handler with the application"""
51 def register_exit_handler():
52  if sys.platform.startswith("linux"):
53  import signal
54  signal.signal(signal.SIGTERM, exit_handler)
55  signal.signal(signal.SIGHUP, exit_handler)
56  signal.signal(signal.SIGQUIT, exit_handler)
57  signal.signal(signal.SIGINT, exit_handler)
58  elif sys.platform.startswith("win"):
59  import win32api
60  win32api.SetConsoleCtrlHandler(exit_handler, True)
61 
62 """Implement the exit handler to shut-down the PST Tracker connection on application termination."""
63 def exit_handler(*args):
64  global running
65  pst.Tracker.shutdown()
66  running = False
67  return True
68 
69 def main():
70  if(len(sys.argv) < 2):
71  print("\nConfiguration Error: A camera configuration file needs to be specified. This file can be found in the Redist folder of your installation. "
72  "See the documentation of the Python bindings for more information.")
73  exit(0)
74 
75  # Register exit_handler for proper shutdown
76  register_exit_handler()
77 
78  try:
79  # Use Context Manager to prevent improper Tracker shutdown on errors.
80  # Create an instance of the Tracker object using the default configuration path and file names.
81  with pst.Tracker("", "","", sys.argv[1]) as tracker:
82 
83  # Print version number of the tracker server being used.
84  print("Running PST Server version " + tracker.get_version_info())
85 
86  # Create listener with callback functions for data and/or mode updates.
87  listener = MyListener()
88 
89  # Register the listener object to the tracker server.
90  tracker.add_tracker_listener(listener)
91 
92  # Start the tracker server.
93  tracker.start()
94 
95  # Perform a system check to see if the tracker server is running OK and print the result.
96  print("System check: " + str(tracker.system_check()))
97  print("***************************\n")
98 
99  # Set the frame rate to 60 Hz.
100  tracker.set_framerate(60)
101  print("Current frame rate: " + str(tracker.get_framerate()))
102 
103  # In order to start receiving images, enable image transfer. When image transfer is disabled,
104  # the vector of images returned by Tracker.get_image() will be empty.
105  tracker.enable_image_transfer()
106 
107  # The standard PST trackers will run at a reduced frame rate of 30 Hz when image transfer is enabled.
108  # However, since this frame rate is temporary for as long as image transfer is enabled, that frame rate
109  # will not be reported as the current frame rate.
110  print("Enabled image transfer. Current frame rate: " + str(tracker.get_framerate()))
111  print("***************************\n")
112 
113  # Try to capture 100 images.
114  for i in range(100):
115  # Try to get the last grabbed image.
116  # Note that enabling image transfer takes some time. While image transfer is being enabled,
117  # the images list in the Image object will be empty.
118  image = tracker.get_image()
119 
120  if image is not None:
121  print("Retrieval operation successful!\n")
122  print("Retrieved " + str(len(image.images)) + " image(s) of size: " + str(image.width) + " X " + str(image.height) + "\n")
123  # Do something with the image
124  else:
125  print("Retrieval operation unsuccessful!\n")
126 
127  # Don't request images too fast, wait for around 1/60 seconds.
128  time.sleep(0.016)
129 
130  # Wait for 5 seconds, since this is > 4 seconds, image transfer will be disabled automatically.
131  print("Waiting 5 seconds for image transfer to automatically be disabled...\n")
132  time.sleep(5)
133 
134  # Try to grab one image. Since image retrieval timed out, it should return an empty image vector.
135  image = tracker.get_image()
136  if image is not None:
137  print("Retrieval operation successful!\n")
138  print("Retrieved " + str(len(image.images)) + " image(s) of size: " + str(image.width) + " X " + str(image.height) + "\n")
139  else:
140  print("Retrieval operation unsuccessful!\n")
141 
142  while running:
143  time.sleep(0.1)
144 
145  except psterrors.TrackerError as err:
146  # Catch TrackerError and print error messages.
147  print(err.message)
148 
149 if __name__ == "__main__":
150  main()