frame_queue_example.py
Go to the documentation of this file.
1 ## License: Apache 2.0. See LICENSE file in root directory.
2 ## Copyright(c) 2019 Intel Corporation. All Rights Reserved.
3 
4 #####################################################
5 ## Frame Queues ##
6 #####################################################
7 
8 # First import the library
9 import pyrealsense2 as rs
10 import time
11 
12 # Implement two "processing" functions, each of which
13 # occassionally lags and takes longer to process a frame.
14 def slow_processing(frame):
15  n = frame.get_frame_number()
16  if n % 20 == 0:
17  time.sleep(1/4)
18  print(n)
19 
20 def slower_processing(frame):
21  n = frame.get_frame_number()
22  if n % 20 == 0:
23  time.sleep(1)
24  print(n)
25 
26 try:
27  # Create a pipeline
28  pipeline = rs.pipeline()
29 
30  # Create a config and configure the pipeline to stream
31  # different resolutions of color and depth streams
32  config = rs.config()
33  config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
34 
35  # Start streaming to the slow processing function.
36  # This stream will lag, causing the occasional frame drop.
37  print("Slow callback")
38  pipeline.start(config)
39  start = time.time()
40  while time.time() - start < 5:
41  frames = pipeline.wait_for_frames()
42  slow_processing(frames)
43  pipeline.stop()
44 
45  # Start streaming to the the slow processing function by way of a frame queue.
46  # This stream will occasionally hiccup, but the frame_queue will prevent frame loss.
47  print("Slow callback + queue")
48  queue = rs.frame_queue(50)
49  pipeline.start(config, queue)
50  start = time.time()
51  while time.time() - start < 5:
52  frames = queue.wait_for_frame()
53  slow_processing(frames)
54  pipeline.stop()
55 
56  # Start streaming to the the slower processing function by way of a frame queue.
57  # This stream will drop frames because the delays are long enough that the backed up
58  # frames use the entire internal frame pool preventing the SDK from creating more frames.
59  print("Slower callback + queue")
60  queue = rs.frame_queue(50)
61  pipeline.start(config, queue)
62  start = time.time()
63  while time.time() - start < 5:
64  frames = queue.wait_for_frame()
65  slower_processing(frames)
66  pipeline.stop()
67 
68  # Start streaming to the slower processing function by way of a keeping frame queue.
69  # This stream will no longer drop frames because the frame queue tells the SDK
70  # to remove the frames it holds from the internal frame queue, allowing the SDK to
71  # allocate space for and create more frames .
72  print("Slower callback + keeping queue")
73  queue = rs.frame_queue(50, keep_frames=True)
74  pipeline.start(config, queue)
75  start = time.time()
76  while time.time() - start < 5:
77  frames = queue.wait_for_frame()
78  slower_processing(frames)
79  pipeline.stop()
80 
81 except Exception as e:
82  print(e)
83 except:
84  print("A different Error")
85 else:
86  print("Done")
static std::string print(const transformation &tf)


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:14