我是python的初学者,也是opencv的初学者。我想用opencv视频捕捉功能拍摄一段时间的视频,比如10秒然后系统应该进入睡眠模式一段时间让我们说6秒。然后它需要10秒的视频。两个视频应该保存在单独的文件中。我的问题是,当我调用第一个功能时它会保存第一个视频但是在调用video2功能时它不会保存视频,因为代码排列不当。我是新用户。
import cv2
import time
def delay(delay_time):
timeout = delay_time # [seconds]
timeout_start = time.time()
print(time.strftime("%c"))
while time.time() < timeout_start + timeout:
test = 0
if test == 3:
break
test -= 1
print(time.strftime("%c"))
def video1():
cap = cv2.VideoCapture(0)
if (cap.isOpened() == False):
print("Unable to read camera feed")
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
out = cv2.VideoWriter('Data/output1.avi',
cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (frame_width, frame_height))
while time.time() < time_start + closing_time:
ret, frame = cap.read()
if ret == True:
out.write(frame)
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
out.release()
def video2():
cap = cv2.VideoCapture(0)
if (cap.isOpened() == False):
print("Unable to read camera feed")
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
out = cv2.VideoWriter('Data/output2.avi',
cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (frame_width, frame_height))
while time.time() < time_start + closing_time:
ret, frame = cap.read()
if ret == True:
out.write(frame)
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
cv2.destroyAllWindows()
print(time.strftime("%c"))
video1()
delay(6)
print(time.strftime("%c"))
video2()
答案 0 :(得分:1)
您的代码有很多缩进问题,我甚至无法指向1个位置,并说这是一个问题。所以我在这里重新安排了你的代码。您甚至没有在代码中定义time_start
或closing_time
。但我假设你的真实代码有它们,而问题只是第二个输出不正确的视频。那是因为您必须在time_start
和video1
的开头定义video2
。如果您没有重新定义time_start
,那么当您video2
while time.time() < time_start + closing_time:
时,此import cv2
import time
def delay(delay_time):
timeout = delay_time # [seconds]
timeout_start = time.time()
print(time.strftime("%c"))
while time.time() < timeout_start + timeout:
test = 0
if test == 3:
break
test -= 1
print(time.strftime("%c"))
def video1():
time_start = time.time()
cap = cv2.VideoCapture(0)
if (cap.isOpened() == False):
print("Unable to read camera feed")
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
print(frame_width,frame_height)
out = cv2.VideoWriter('output1.avi',
cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (frame_width, frame_height))
while time.time() < time_start + closing_time:
ret, frame = cap.read()
if ret == True:
out.write(frame)
#cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
out.release()
def video2():
time_start = time.time()
cap = cv2.VideoCapture(0)
if (cap.isOpened() == False):
print("Unable to read camera feed")
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
print(frame_width,frame_height)
out = cv2.VideoWriter('output2.avi',
cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (frame_width, frame_height))
while time.time() < time_start + closing_time:
ret, frame = cap.read()
if ret == True:
out.write(frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
out.release()
print(time.strftime("%c"))
closing_time = 10
video1()
delay(6)
print(time.strftime("%c"))
video2()
# this loop is here because sometimes cv2 has problems destroying windows
# if you call it once. so to be safe I call it 10 times
for _ in range(10):
cv2.destroyAllWindows()
已经用尽且将为假。因此没有真正的视频写入文件。
colorStand <- colorRampPalette(colors = c("blue", "white", "red"))
heatmaply(data, margins = c(60,100,40,20), colors=colorStand,
limits = c(-2.5,2.5),showticklabels = c(T,F))
答案 1 :(得分:0)
这应该可以解决你在问题中提出的问题,代码如下: `
import cv2
import time
import numpy as np
cap=cv2.VideoCapture(0)
fourcc=cv2.VideoWriter_fourcc(*'XVID')
out=cv2.VideoWriter('output1.avi', fourcc, 20.0, (640, 48))
start_time=time.time()
while True:
ret, frame=cap.read()
out.write(frame)
cv2.imshow('frame', frame)
if time.time()-start_time>=10:
break
cap.release()
out.release()
time.sleep(6)
cap2=cv2.VideoCapture(0)
fourcc2=cv2.VideoWriter_fourcc(*'XVID')
out2=cv2.VideoWriter('output2.avi', fourcc2, 20.0, (640, 48))
start_time2=time.time()
while True:
ret, frame=cap2.read()
out2.write(frame)
cv2.imshow('frame', frame)
if time.time()-start_time2>=10:
break
cap2.release()
out.release()
`