2016-04-11 20 views
1

我記錄了2個攝像機的視頻,這樣我就可以在文本文件中爲每個幀獲取記錄的視頻輸入的時間戳。根據存儲在文本文件中的時間戳顯示來自2個獨立視頻的幀

現在我想要在一個框架中並排顯示兩個相機的幀,但要根據存儲在文本文件中的時間。我應該怎麼做呢?

下面是從照相機1個照相機1 相機2

Camera1    Camera2 
0.200000047   0.000000000 
33.515000047  33.315000000 

現在根據時間戳文件,幀記錄的視頻樣本的時間戳數據1應在0.2秒是可見並且相機2的幀1應在0.0秒(視頻開始)處可見。第2幀攝像機2然後以33.315秒來到,並且第2幀的攝像機1以33.315000047秒來到。

我陷入'如何在特定時間顯示幀?'

+0

我是否正確理解你想要播放它們 實時」?與之相同,在顯示攝像機1的第二幀之前應經過約33.32秒的實時時間。 –

+0

是的,在下一幀進入之前幀1應該保持33.32秒 –

+1

如果渲染足夠快,則對時間戳進行排序並僅更新下一個圖像(其他相機的一個未更新)。不是,使用固定的渲染時間戳,並選擇兩個攝像機的最近的(或將來最近的)幀。 – Micka

回答

0

事情是這樣的大綱應該做的工作:

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

相機3幀2在時間戳

[ 2.0, 3.0, 4.0 ] 

enter image description here

我們設置等待時間爲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

注:在t第一幀= 0.0秒和在t = 5.0s的最後一幀。


可能的改進

  • 當有幀之間的較長的間隙,等待更長的時間
  • 只有當圖像/幀索引中的至少一個上的定時器事件改變
  • 等待代替更新顯示的睡眠(很可能需要自定義的圖形用戶界面)
+0

get_frame(i)在做什麼?我試着在對代碼 'cap = cv2.VideoCapture('cam1.mp4')' 'cap.set(cv.CV_CAP_PROP_POS_FRAMES,frame_index)' 'ret,frame = cap.read() ' 'return frame' –

相關問題