事情是這樣的大綱應該做的工作:
import cv2
import numpy as np
import time
# ...
CAMERA_1 = 0
CAMERA_2 = 1
def load_timestamps(camera):
pass # Implement this
def get_next_frame(camera):
pass # Implement this
# ...
timestamps = [
load_timestamps(CAMERA_1)
, load_timestamps(CAMERA_2)
]
start_time = None
frame_index = [0, 0]
video_done = [False, False]
# Make those default blank images the size of your video...
# Adjust number of channels if you're using grayscale
image = [np.zeros((100,100,3),np.uint8)] * 2
while True:
real_time = time.time()
if start_time is None: # First iteration only
start_time = real_time
current_ts = real_time - start_time
print("ts=%0.3f s" % current_ts)
# Advance both cameras up to current_ts
for i in [CAMERA_1, CAMERA_2]:
while True:
# Get timestamp of the frame at current position
current_index = frame_index[i]
if current_index >= len(timestamps[i]):
video_done[i] = True
break # End of the video
video_ts = timestamps[i][current_index]
if current_ts < video_ts:
break # The frame is in future, stop
# The frame is in the past or present
print(" camera %d: frame %d" % (i+1, frame_index[i]))
image[i] = get_next_frame(i)
frame_index[i] += 1
cv2.imshow("Output", np.hstack([image[0], image[1]]))
cv2.waitKey(20) # Max 50 FPS, but probably slower
if video_done[CAMERA_1] and video_done[CAMERA_2]:
break
我們有一個定期運行的循環(大約每20ms,使用睡眠cv2.waitKey(...)
)。
在每次迭代中,我們測量第一次迭代的時間偏移量,這給了我們一個基於0的時間戳。
然後,我們通過閱讀框架並將索引推進到我們的時間戳列表中來「查找」每個視頻,直到兩個視頻都趕上爲止。
最後,我們並排顯示來自每個視頻的最新幀。
例
讓我們假設我們具有3幀爲照相機1在時間戳
[ 1.0, 3.0, 5.0 ]
![camera_1](https://i.stack.imgur.com/e0BMm.png)
和相機3幀2在時間戳
[ 2.0, 3.0, 4.0 ]
![enter image description here](https://i.stack.imgur.com/HmHhT.png)
我們設置等待時間爲0.5秒,以保持這一短。
跟蹤我們得到以下輸出該程序:
ts=0.000 s
ts=0.500 s
ts=1.000 s
camera 1: frame 0
ts=1.500 s
ts=2.000 s
camera 2: frame 0
ts=2.500 s
ts=3.000 s
camera 1: frame 1
camera 2: frame 1
ts=3.500 s
ts=4.000 s
camera 2: frame 2
ts=4.500 s
ts=5.000 s
camera 1: frame 2
和視頻,以採取各步驟快照看起來像
![enter image description here](https://i.stack.imgur.com/Oe0Us.png)
注:在t第一幀= 0.0秒和在t = 5.0s的最後一幀。
可能的改進
- 當有幀之間的較長的間隙,等待更長的時間
- 只有當圖像/幀索引中的至少一個上的定時器事件改變
- 等待代替更新顯示的睡眠(很可能需要自定義的圖形用戶界面)
我是否正確理解你想要播放它們 實時」?與之相同,在顯示攝像機1的第二幀之前應經過約33.32秒的實時時間。 –
是的,在下一幀進入之前幀1應該保持33.32秒 –
如果渲染足夠快,則對時間戳進行排序並僅更新下一個圖像(其他相機的一個未更新)。不是,使用固定的渲染時間戳,並選擇兩個攝像機的最近的(或將來最近的)幀。 – Micka