Skip to content

Commit 896397b

Browse files
author
Matevz Morato
committed
Formating
1 parent 4fa1ee4 commit 896397b

File tree

1 file changed

+49
-50
lines changed

1 file changed

+49
-50
lines changed

examples/mixed/frame_sync.cpp

Lines changed: 49 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ int main() {
3434
left->out.link(stereo->left);
3535
right->out.link(stereo->right);
3636

37-
3837
// Script node will sync high-res frames
3938
auto script = pipeline.create<dai::node::Script>();
4039

@@ -43,55 +42,55 @@ int main() {
4342
camRgb->preview.link(script->inputs["rgb_in"]);
4443

4544
script->setScript(R"(
46-
FPS=15
47-
import time
48-
from datetime import timedelta
49-
import math
50-
51-
MS_THRESHOL=math.ceil(500 / FPS) # Timestamp threshold (in miliseconds) under which frames will be considered synced
52-
53-
def check_sync(queues, timestamp):
54-
matching_frames = []
55-
for name, list in queues.items(): # Go through each available stream
56-
# node.warn(f"List {name}, len {str(len(list))}")
57-
for i, msg in enumerate(list): # Go through each frame of this stream
58-
time_diff = abs(msg.getTimestamp() - timestamp)
59-
if time_diff <= timedelta(milliseconds=MS_THRESHOL): # If time diff is below threshold, this frame is considered in-sync
60-
matching_frames.append(i) # Append the position of the synced frame, so we can later remove all older frames
61-
break
62-
63-
if len(matching_frames) == len(queues):
64-
# We have all frames synced. Remove the excess ones
65-
i = 0
66-
for name, list in queues.items():
67-
queues[name] = queues[name][matching_frames[i]:] # Remove older (excess) frames
68-
i+=1
69-
return True
70-
else:
71-
return False # We don't have synced frames yet
72-
73-
names = ['disp', 'rgb']
74-
frames = dict() # Dict where we store all received frames
75-
for name in names:
76-
frames[name] = []
77-
78-
while True:
79-
for name in names:
80-
f = node.io[name+"_in"].tryGet()
81-
if f is not None:
82-
frames[name].append(f) # Save received frame
83-
84-
if check_sync(frames, f.getTimestamp()): # Check if we have any synced frames
85-
# Frames synced!
86-
node.warn(f"Synced frame!")
87-
for name, list in frames.items():
88-
syncedF = list.pop(0) # We have removed older (excess) frames, so at positions 0 in dict we have synced frames
89-
node.warn(f"{name}, ts: {str(syncedF.getTimestamp())}, seq {str(syncedF.getSequenceNum())}")
90-
node.io[name+'_out'].send(syncedF) # Send synced frames to the host
91-
92-
93-
time.sleep(0.001) # Avoid lazy looping
94-
)");
45+
FPS=15
46+
import time
47+
from datetime import timedelta
48+
import math
49+
50+
MS_THRESHOL=math.ceil(500 / FPS) # Timestamp threshold (in miliseconds) under which frames will be considered synced
51+
52+
def check_sync(queues, timestamp):
53+
matching_frames = []
54+
for name, list in queues.items(): # Go through each available stream
55+
# node.warn(f"List {name}, len {str(len(list))}")
56+
for i, msg in enumerate(list): # Go through each frame of this stream
57+
time_diff = abs(msg.getTimestamp() - timestamp)
58+
if time_diff <= timedelta(milliseconds=MS_THRESHOL): # If time diff is below threshold, this frame is considered in-sync
59+
matching_frames.append(i) # Append the position of the synced frame, so we can later remove all older frames
60+
break
61+
62+
if len(matching_frames) == len(queues):
63+
# We have all frames synced. Remove the excess ones
64+
i = 0
65+
for name, list in queues.items():
66+
queues[name] = queues[name][matching_frames[i]:] # Remove older (excess) frames
67+
i+=1
68+
return True
69+
else:
70+
return False # We don't have synced frames yet
71+
72+
names = ['disp', 'rgb']
73+
frames = dict() # Dict where we store all received frames
74+
for name in names:
75+
frames[name] = []
76+
77+
while True:
78+
for name in names:
79+
f = node.io[name+"_in"].tryGet()
80+
if f is not None:
81+
frames[name].append(f) # Save received frame
82+
83+
if check_sync(frames, f.getTimestamp()): # Check if we have any synced frames
84+
# Frames synced!
85+
node.warn(f"Synced frame!")
86+
for name, list in frames.items():
87+
syncedF = list.pop(0) # We have removed older (excess) frames, so at positions 0 in dict we have synced frames
88+
node.warn(f"{name}, ts: {str(syncedF.getTimestamp())}, seq {str(syncedF.getSequenceNum())}")
89+
node.io[name+'_out'].send(syncedF) # Send synced frames to the host
90+
91+
92+
time.sleep(0.001) # Avoid lazy looping
93+
)");
9594

9695
std::vector<std::string> scriptOut{"disp", "rgb"};
9796
for(auto& name : scriptOut) {

0 commit comments

Comments
 (0)