相机不会使用cv2.VideoCapture(0).release()转动

时间:2018-12-01 17:06:36

标签: python python-2.7 cv2

我编写了这个脚本:

import cv2
import pyaudio
import wave
import threading
import time
import subprocess
import os
import keyboard

class VideoRecorder():  
    # Video class based on openCV 
    def __init__(self):
        self.open = True
        self.fps = 6               # fps should be the minimum constant rate at which the camera can
        self.fourcc = "MJPG"       # capture images (with no decrease in speed over time; testing is required)
        self.frameSize = (640,480) # video formats and sizes also depend and vary according to the camera used
        self.video_filename = "temp_video.avi"
        self.video_cap = cv2.VideoCapture(0)
        self.video_writer = cv2.VideoWriter_fourcc(*self.fourcc)
        self.video_out = cv2.VideoWriter(self.video_filename, self.video_writer, self.fps, self.frameSize)
        self.frame_counts = 1
        self.start_time = time.time()

    # Video starts being recorded 
    def record(self):
        timer_start = time.time()
        timer_current = 0

        while(self.open==True):
            ret, video_frame = self.video_cap.read()

            if self.frame_counts > 10:
                break

            if (ret==True):
                self.video_out.write(video_frame)
                self.frame_counts += 1
                print self.frame_counts
                time.sleep(0.16)

            else:
                #threading.Thread(target=self.stop).start()
                break

                # 0.16 delay -> 6 fps
        time.sleep(1)
        self.video_out.release()
        cv2.VideoCapture(0).release()
        cv2.destroyAllWindows()
        dwhuiadhuiahdwia = raw_input("Testtidhwuia?")


    # Finishes the video recording therefore the thread too
    def stop(self):
        print "You made it"
        if self.open==True:

            self.open=False
            self.video_out.release()
            self.video_cap.release()
            cv2.destroyAllWindows()
            hduwahduiwahdiu = raw_input("Press enter to continue...")

        else: 
            pass


    # Launches the video recording function using a thread        
    def start(self):
        video_thread = threading.Thread(target=self.record)
        video_thread.start()

def start_video_recording():

    global video_thread

    video_thread = VideoRecorder()
    video_thread.start()

def stop_AVrecording():

    frame_counts = video_thread.frame_counts
    elapsed_time = time.time() - video_thread.start_time
    recorded_fps = frame_counts / elapsed_time
    print "total frames " + str(frame_counts)
    print "elapsed time " + str(elapsed_time)
    print "recorded fps " + str(recorded_fps)
    video_thread.stop()

    # Makes sure the threads have finished
    time.sleep(1)
    video_thread.stop()

    print ".."

start_video_recording()
duiwhaiudhwauidhwa = raw_input("hello")

它应该使用摄像机录制大约10秒钟的视频,然后保存它,然后在用户按下Enter时关闭摄像机并关闭脚本。

但这并不能真正起作用。

它将录制视频,并且会保存视频,但是仅当我关闭脚本时摄像机才会关闭(摄像机已打开,但未录制。)

我之所以知道这一点,是因为当提示我按Enter继续时,摄像头旁边的LED不会关闭,但是当我按Enter并关闭脚本时,指示灯会关闭。

我找到了this,,但尚未测试。但是,如果我要使用该解决方案并且它能够正常工作,则必须在我手动运行脚本的每台计算机上都进行此操作。

0 个答案:

没有答案