每次我们在openCV中触发时,保存带有不同文件名的视频
这是我要实现的过程:
实时流是从网络摄像头捕获的,图像帧存储在特定的文件夹中。
现在,如果我当时在该文件夹中触发框架,则应将其转换为视频,并通过名称保存。 Video1.mp4。
现在再次按下触发器,应将另一个视频保存为Video2.mp4。
我在这里附上了代码。如果我按R,则将一个视频保存为A0.MP4。但是,如果我再次按下,似乎什么都没有发生。
def frametovideo(img_array):
for i in range(len(img_array)):
out.write(img_array[i])
如果名称 ==“ main ”:
img_array = []
videono = 0
cap = cv2.VideoCapture(0)
for filename in glob.glob('./output/*.jpg'):
img = cv2.imread(filename)
height, width, layers = img.shape
size = (width,height)
img_array.append(img)
path = 'a' + str(videono) + '.mp4'
out = cv2.VideoWriter(path,cv2.VideoWriter_fourcc(*'mp4v'), 15, size)
while True:
ret, frame = cap.read()
if ret == True:
cv2.imshow("frame",frame)
k = cv2.waitKey(5) & 0xff
if k == ord('r'):
frametovideo(img_array)
videono += 1
This is the process I am trying to achieve :
Live Stream is captured from webcam and the Image frames are stored in a particular folder.
Now, If I give a trigger the frames in that folder at that moment should be converted into a video and get saved with name eg. video1.mp4.
Now again if I press a trigger another video should be saved as video2.mp4.
I have attached the code here . If I press R , it is saving one video as a0.mp4 . But If I press again, nothing seems to happen.
def frametovideo(img_array):
for i in range(len(img_array)):
out.write(img_array[i])
if name == "main":
img_array = []
videono = 0
cap = cv2.VideoCapture(0)
for filename in glob.glob('./output/*.jpg'):
img = cv2.imread(filename)
height, width, layers = img.shape
size = (width,height)
img_array.append(img)
path = 'a' + str(videono) + '.mp4'
out = cv2.VideoWriter(path,cv2.VideoWriter_fourcc(*'mp4v'), 15, size)
while True:
ret, frame = cap.read()
if ret == True:
cv2.imshow("frame",frame)
k = cv2.waitKey(5) & 0xff
if k == ord('r'):
frametovideo(img_array)
videono += 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不了解如何保存文件名请勿使用运算符。使用的字符串格式 python 3.8 或更高版本。试试下面这个。其实你可以修改按键。
顺便说一句,看看第 27-34 行。正确的方法是
if __name__ == "__main__":
You do not understanding how to save filenames Do not used operator. Used string format python 3.8 or later. Try this in below. Actually, you can modified key press.
Btw,look in line #27-34. Correct way to do this
if __name__ == "__main__":