cv2.VideoCapture打开两个摄像头时后面打开的那个会有1.3-1.5s的延迟,
cap = cv2.VideoCapture()
cap2 = cv2.VideoCapture()
这是由于cap = cv2.VideoCapture()这行代码执行需要1.3-1.5的时间
所以我们先计算它执行的时间,然后乘以fps(每秒的帧数)就得到了第一个摄像头多录的帧数,用一个循环先把这些帧读完,然后再显示两个相机的帧就能达到时间同步了
#coding:utf8
import cv2
import datetime
oldtime=datetime.datetime.now() #开始时间
cap = cv2.VideoCapture('rtsp://密码@ip地址/')
newtime=datetime.datetime.now() #结束时间
print u'相差:%s'%(newtime-oldtime)
print u'相差:%s微秒'%(newtime-oldtime).microseconds
print u'相差:%s秒'%(newtime-oldtime).seconds
t = int((newtime-oldtime).seconds)*25
t+=int(float((newtime-oldtime).microseconds)/1000000*25)
cap2 = cv2.VideoCapture('rtsp://密码@ip地址/')#这里用第二个相机的密码和ip地址替代
print(t)
frameNum = 1
for i in range(t):
ret,frame = cap.read()
#判断是否正常开启
frameNum = 1
n=25
#for i in range(34):
# ret,frame = cap.read()
while(cap.isOpened() and cap2.isOpened()): #& cap2.isOpened()):
ret,frame = cap.read()
ret2,frame2 = cap2.read()
frameNum = frameNum + 1
#print(frameNum%n)
#cv2.imshow('right',frame2)
#cv2.imshow('left',frame)
#1秒27 1秒33 1秒27 1秒40 1秒36 1秒33 1秒31
#每10帧存储一张图片
if(frameNum%n==0):
cv2.imwrite('l'+str((frameNum/n)/100)+str((frameNum/n)/10)+str((frameNum/n)%10)+'.bmp',frame)
cv2.imwrite('r'+str((frameNum/n)/100)+str((frameNum/n)/10)+str((frameNum/n)%10)+'.bmp',frame2)
#if cv2.waitKey(1) == ord('q'):
# break
cap.release()
cap2.release()
cv2.destroyAllWindows()
同步前
同步后