【问题标题】:Raspberry pi Video Capture freezes while writing to videoRaspberry pi Video Capture 在写入视频时冻结
【发布时间】:2016-08-04 10:33:31
【问题描述】:

我正在尝试同时显示网络摄像头流以及将视频写入文件。但是在写入时,它会冻结实时流。问题是 OpenCV 使用循环录制视频,程序卡在 OpenCV 循环中,用户无法继续。它不听用户响应。如何同时录制视频并听取用户响应?

我正在使用的代码:

import wx
import vlc
import os
import user
import numpy as np
import time
import cv, cv2

class MainWindow(wx.Panel):

    def __init__(self, parent,capture):  
        wx.Panel.__init__(self, parent)        
        mainSizer = wx.BoxSizer(wx.VERTICAL)

# video    
        videoWarper = wx.StaticBox(self,size=(640,480)        
        videoBoxSizer = wx.StaticBoxSizer(videoWarper, wx.VERTICAL)     
        videoFrame = wx.Panel(self, -1,size=(640,480))        
        capture = ShowCapture(videoFrame, capture)       
        videoBoxSizer.Add(videoFrame,0)       
        mainSizer.Add(videoBoxSizer,0)      

        parent.Centre()        
        self.Show()        
        self.SetSizerAndFit(mainSizer)       

# Panels

# The first panel holds the video and it's all black

        self.videopanel = wx.Panel(self, -1)        
        self.videopanel.SetBackgroundColour(wx.BLACK)

# The second panel holds controls

        ctrlpanel = wx.Panel(self, -1 )      
        self.timeslider = wx.Slider(ctrlpanel, -1, 0, 0, 1000)        
        self.timeslider.SetRange(0, 1000)        
        record = wx.Button(ctrlpanel, label="Record")       
        end = wx.Button(ctrlpanel, label="End")

# Bind controls to events

        self.Bind(wx.EVT_BUTTON, self.OnRecord, record)      
        self.Bind(wx.EVT_BUTTON, self.OnEnd, end)

# Give a pretty layout to the controls

        ctrlbox = wx.BoxSizer(wx.VERTICAL)        
        box = wx.BoxSizer(wx.HORIZONTAL)

# box contains some buttons and the volume controls

        box.Add(record)       
        box.Add(end)

# Merge box to the ctrlsizer

        ctrlbox.Add(box, flag=wx.EXPAND, border=10)        
        ctrlpanel.SetSizer(ctrlbox)

# Put everything togheter

        sizer = wx.BoxSizer(wx.VERTICAL)        
        sizer.Add(ctrlpanel, flag=wx.EXPAND | wx.BOTTOM | wx.TOP, border=10)        
        self.SetSizer(sizer)        
        self.SetMinSize((350, 300))

# VLC player controls

        self.Instance = vlc.Instance()       
        self.player = self.Instance.media_player_new()   

    def OnRecord(self, evt):

        fourcc = cv2.cv.CV_FOURCC('D', 'I', 'V', 'X')     
        out = cv2.VideoWriter('video.avi', fourcc, 8.0, (640, 480))    
        counter = 0     
        while counter < 1:                        
            ret,frame = capture.read()           
            out.write(frame)

    def OnEnd(self, evt):
        out = cv2.VideoWriter('video.avi', fourcc, 8.0, (640, 480))      
        out.release()        
        cv2.destroyAllWindows()

class ShowCapture(wx.Panel):

    def __init__(self, parent, capture, fps=8):

        wx.Panel.__init__(self, parent, wx.ID_ANY, (0,0), (640,480))        
        self.capture = capture       
        ret, frame = capture.read()        
        height, width = frame.shape[:2]        
        parent.SetSize((width, height))        
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)    
        self.bmp = wx.BitmapFromBuffer(width, height, frame)        
        self.timer = wx.Timer(self)       
        self.timer.Start(1000./fps)    
        self.Bind(wx.EVT_PAINT, self.OnPaint)        
        self.Bind(wx.EVT_TIMER, self.NextFrame)

    def OnPaint(self, evt):
        dc = wx.BufferedPaintDC(self)        
        dc.DrawBitmap(self.bmp, 0, 0)

    def NextFrame(self, event):
        ret, frame = self.capture.read()       
        if ret:           
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)            
            self.bmp.CopyFromBuffer(frame)            
            self.Refresh()

capture = cv2.VideoCapture(-1)

app = wx.App(False) 
frame = wx.Frame(None, title='CamGUI')
panel = MainWindow(frame, capture)
frame.Show()
app.MainLoop()

树莓派是否能够并行读取/写入视频或者它会损害 pi 的性能?

希望高手指点:)

【问题讨论】:

    标签: python-2.7 opencv wxpython raspberry-pi2


    【解决方案1】:

    添加timer,这将允许您在应用程序执行期间测试输入和其他更改。
    wx.timer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 2020-10-04
      • 2020-12-31
      • 1970-01-01
      • 2017-08-08
      相关资源
      最近更新 更多