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