0

次のコードを使用してRaspicamでモーションをキャプチャすると、cv2.VideoWriter()がPiで動作しないようですが、私はこの問題について読んできましたが、まだv4lドライバをインストールしましたが、まだ動作していません。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

Iは

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

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

を変えることによって

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

からコーデックを変更することによりこれを解決

motion_filename = now.strftime("%Y_%m_%d_%H_%M_%S_MOTION.avi") 
関連する問題