python - 以指定的时间间隔录制视频,然后将其保存到OpenCv Python文件中

标签 python opencv video camera vision

这是我的目标。

  • 连续捕获视频,直到'q;被按下
  • 每十秒钟将视频保存到创建的目录文件
  • 继续第二步,直到按下“q”为止

  • 我正在执行以下代码。但是在创建文件时,它正在创建6kb的文件,并且说不能播放。我对opencv和python相当陌生。不知道我在想什么。使用Python 3.6在pycharm上运行此代码。还有

    cv2.imshow('frame',frame)

    十秒钟后停止,但正在后台进行录制并创建文件。
    import numpy as np
    import cv2
    import time
    import os
    import random
    import sys
    
    
    fps=24
    width=864
    height=640
    video_codec=cv2.VideoWriter_fourcc('D','I','V','X')
    
    name = random.randint(0,1000)
    print (name)
    if (os.path.isdir(str(name)) is False):
        name = random.randint(0,1000)
        name=str(name)
    
    name = os.path.join(os.getcwd(), str(name))
    print('ALl logs saved in dir:', name)
    os.mkdir(name)
    
    
    cap = cv2.VideoCapture(0)
    ret=cap.set(3, 864)
    ret=cap.set(4, 480)
    cur_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
    
    
    start=time.time()
    video_file_count = 1
    video_file = os.path.join(name, str(video_file_count) + ".avi")
    print('Capture video saved location : {}'.format(video_file))
    
    
    while(cap.isOpened()):
        start_time = time.time()
        ret, frame = cap.read()
        if ret==True:
            cv2.imshow('frame',frame)
            if (time.time() - start > 10):
                start = time.time()
                video_file_count += 1
                video_file = os.path.join(name, str(video_file_count) + ".avi")
                video_writer = cv2.VideoWriter(video_file,video_codec, fps,(int(cap.get(3)),int(cap.get(4))))
                time.sleep(10)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
        else:
            break
    cap.release()
    cv2.destroyAllWindows()
    

    我想要带有录制视频的文件。生成文件,但是大小为6kb,并且没有任何记录。

    最佳答案

    你快到了!鉴于我了解您的目标是什么,而对您的代码所做的更改却很少,因此这对我有用。

    这会将每帧记录到当前视频中,同时每十秒钟写入一个新的视频文件。

    import numpy as np
    import cv2
    import time
    import os
    import random
    import sys
    
    
    fps = 24
    width = 864
    height = 640
    video_codec = cv2.VideoWriter_fourcc("D", "I", "V", "X")
    
    name = random.randint(0, 1000)
    print(name)
    if os.path.isdir(str(name)) is False:
        name = random.randint(0, 1000)
        name = str(name)
    
    name = os.path.join(os.getcwd(), str(name))
    print("ALl logs saved in dir:", name)
    os.mkdir(name)
    
    
    cap = cv2.VideoCapture(0)
    ret = cap.set(3, 864)
    ret = cap.set(4, 480)
    cur_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
    
    
    start = time.time()
    video_file_count = 1
    video_file = os.path.join(name, str(video_file_count) + ".avi")
    print("Capture video saved location : {}".format(video_file))
    
    # Create a video write before entering the loop
    video_writer = cv2.VideoWriter(
        video_file, video_codec, fps, (int(cap.get(3)), int(cap.get(4)))
    )
    
    while cap.isOpened():
        start_time = time.time()
        ret, frame = cap.read()
        if ret == True:
            cv2.imshow("frame", frame)
            if time.time() - start > 10:
                start = time.time()
                video_file_count += 1
                video_file = os.path.join(name, str(video_file_count) + ".avi")
                video_writer = cv2.VideoWriter(
                    video_file, video_codec, fps, (int(cap.get(3)), int(cap.get(4)))
                )
                # No sleeping! We don't want to sleep, we want to write
                # time.sleep(10)
    
            # Write the frame to the current video writer
            video_writer.write(frame)
            if cv2.waitKey(1) & 0xFF == ord("q"):
                break
        else:
            break
    cap.release()
    cv2.destroyAllWindows()
    

    关于python - 以指定的时间间隔录制视频,然后将其保存到OpenCv Python文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55411269/

    相关文章:

    c - 从 square.c 检测三角形

    python从套接字流接收图像

    javascript - 什么 HTML5 视频监听器可以在视频开头触发事件?

    python - 如何从python中的两个列表生成嵌套列表

    python - 如何在页面上显示查询数量? (谷歌应用程序引擎)

    python - 如何为装饰器编写测试函数?

    python - 使用颜色条从 2d 函数创建矢量图形(svg、eps、pdf) "heatmap"

    opencv - 创建具有透明边框的变形图像

    css - HTML5 视频没有隐藏在媒体查询中

    html - Canvas 到视频在 Safari Lion/Mountain Lion 上非常慢