2016-11-16 89 views
0

我使用以下代碼捕獲Raspicam上的運動,問題是cv2.VideoWriter()似乎無法在Pi上運行,我已閱讀有關此問題,我已經安裝了v4l驅動程序,但它仍然不工作。cv2.VideoWriter()與Raspicam無法在Raspberry Pi上工作

如果有人有另一種方法來捕捉視頻或解決方案,他會對我有巨大的幫助。

from picamera.array import PiRGBArray 
from picamera import PiCamera 
import time 
import cv2 
import numpy 
from datetime import datetime, timedelta 

MOTION_RECORD_TIME = timedelta(seconds = 3) 


def have_motion(frame1, frame2): 
    if frame1 is None or frame2 is None: 
     return False 
    delta = cv2.absdiff(frame1, frame2) 
    thresh = cv2.threshold(delta, 25, 255, cv2.THRESH_BINARY)[1] 
    return numpy.sum(thresh) > 0 

camera = PiCamera() 
camera.resolution = (640, 480) 
camera.framerate = 32 
rawCapture = PiRGBArray(camera, size=(640, 480)) 
time.sleep(0.1) 

prev_frame = None 
last_motion = None 
motion_filename = None 
motion_file = None 

frame_size = camera.resolution 
fourcc = cv2.cv.CV_FOURCC(*"XVID") 

for frame in camera.capture_continuous(rawCapture, format="bgr",use_video_port=True): 
    image = frame.array 



    frame_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 
    frame_gray = cv2.GaussianBlur(frame_gray, (21, 21), 0) 



    if have_motion(prev_frame, frame_gray): 
     if motion_file is None: 
      now = datetime.now() 
      motion_filename = now.strftime("%Y_%m_%d_%H_%M_%S_MOTION.h264") 
      motion_file = cv2.VideoWriter(motion_filename, fourcc, 20.0, frame_size) 
      last_motion = time.time() 
     print "Motion!", last_motion 

    if motion_file is not None: 
     motion_file.write(image) 
     print 'Saving...' 
     print now 
     print time.time() - last_motion 
     if time.time() - last_motion > 3: 
      motion_file.release() 
      motion_file = None 
      print 'Saved' 
      print motion_filename 
      break 


    prev_frame = frame_gray 
    cv2.imshow('frame', image) 
    rawCapture.truncate(0) 

    if cv2.waitKey(1) & 0xFF == ord('q'): 
       cv2.destroyAllWindows() 
       break 
+0

您是否嘗試了不同的編解碼器和文件結尾?對我來說,它用cpp編寫我的應用程序。 –

+0

@ al-eax什麼編解碼器爲你工作? – alex190030

回答

0

我解決了這個改變的編解碼器從

fourcc = cv2.cv.CV_FOURCC(*"XVID") 

fourcc = cv2.cv.CV_FOURCC(*"MPV4") 

,並通過改變

motion_filename = now.strftime("%Y_%m_%d_%H_%M_%S_MOTION.h264") 

motion_filename = now.strftime("%Y_%m_%d_%H_%M_%S_MOTION.avi")