每次我们在openCV中触发时,保存带有不同文件名的视频

发布于 2025-01-20 00:43:51 字数 1025 浏览 2 评论 0原文

这是我要实现的过程:

  1. 实时流是从网络摄像头捕获的,图像帧存储在特定的文件夹中。

  2. 现在,如果我当时在该文件夹中触发框架,则应将其转换为视频,并通过名称保存。 Video1.mp4。

  3. 现在再次按下触发器,应将另一个视频保存为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 :

  1. Live Stream is captured from webcam and the Image frames are stored in a particular folder.

  2. 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.

  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暗恋未遂 2025-01-27 00:43:52

您不了解如何保存文件名请勿使用运算符。使用的字符串格式 python 3.8 或更高版本。试试下面这个。其实你可以修改按键。

import cv2
import os


i = 1

wait = 0


video = cv2.VideoCapture(0)

while video.isOpened():
    ret, img = video.read()

    cv2.imshow('live video', img)

    # wait for user to press any key
    key = cv2.waitKey(100)

    # wait variable is to calculate waiting time
    wait = wait+100

    if key == ord('q'):
        break
    # when it reaches to 5000 milliseconds
    # we will save that frame in given folder
    if wait == 5000:
        filename = f'Frame_{str(i)}.jpg'
        
        # Save the images in given path
        cv2.imwrite(filename, img)
        i += 1
        wait = 0

# close the camera
video.release()

# close open windows
cv2.destroyAllWindows()

顺便说一句,看看第 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.

import cv2
import os


i = 1

wait = 0


video = cv2.VideoCapture(0)

while video.isOpened():
    ret, img = video.read()

    cv2.imshow('live video', img)

    # wait for user to press any key
    key = cv2.waitKey(100)

    # wait variable is to calculate waiting time
    wait = wait+100

    if key == ord('q'):
        break
    # when it reaches to 5000 milliseconds
    # we will save that frame in given folder
    if wait == 5000:
        filename = f'Frame_{str(i)}.jpg'
        
        # Save the images in given path
        cv2.imwrite(filename, img)
        i += 1
        wait = 0

# close the camera
video.release()

# close open windows
cv2.destroyAllWindows()

Btw,look in line #27-34. Correct way to do this if __name__ == "__main__":

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文