2015-03-28 16 views
9

Sto cercando di salvare il video ma non funziona. Ho seguito le istruzioni dalla documentazione di openCV.openCV video saving in python

import numpy as np 
import cv2 

cap = cv2.VideoCapture(0) 

fourcc = cv2.VideoWriter_fourcc(*'XVID') 
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640,480)) 

while(cap.isOpened()): 
    ret, frame = cap.read() 
    if ret==True: 
     frame = cv2.flip(frame,0) 


     out.write(frame) 

     cv2.imshow('frame',frame) 
     if cv2.waitKey(1) & 0xFF == ord('q'): 
      break 
    else: 
     break 
cap.release() 

out.release() 

cv2.destroyAllWindows() 

Cosa c'è che non va?

risposta

8

provare questo. sta funzionando per me.

import numpy as np 
import cv2 

cap = cv2.VideoCapture(0) 

# Define the codec and create VideoWriter object 
#fourcc = cv2.cv.CV_FOURCC(*'DIVX') 
#out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480)) 
out = cv2.VideoWriter('output.avi', -1, 20.0, (640,480)) 

while(cap.isOpened()): 
    ret, frame = cap.read() 
    if ret==True: 
     frame = cv2.flip(frame,0) 

     # write the flipped frame 
     out.write(frame) 

     cv2.imshow('frame',frame) 
     if cv2.waitKey(1) & 0xFF == ord('q'): 
      break 
    else: 
     break 

# Release everything if job is finished 
cap.release() 
out.release() 
cv2.destroyAllWindows() 
+0

funziona. ma il video è ruotato di 180 gradi. :( –

+6

questo perché ha fatto un capovolgimento – Tarun

+1

Non è lavoro per me. Crea solo un file video vuoto (con dimensione 5,6 kB). –

0

Ho avuto lo stesso problema e quindi ho provato questo:

frame = cv2.flip(frame,180) 

invece di

frame= cv2.flip(frame,0) 

e sta funzionando.

2

Nuru risposta funziona davvero, unica cosa è rimuovere questa riga frame = cv2.flip(frame,0) sotto if ret==True: ciclo che sarà in uscita il file video senza lanciare

1

Ho anche affrontato lo stesso problema, ma ha funzionato quando ho usato 'MJPG' invece di 'XVID'

ho usato

fourcc = cv2.VideoWriter_fourcc(*'MJPG') 

invece di

fourcc = cv2.VideoWriter_fourcc(*'XVID') 
0

Ad esempio:

fourcc = cv2.VideoWriter_fourcc(*'MJPG') 
out_corner = cv2.VideoWriter('img_corner_1.avi',fourcc, 20.0, (640, 480)) 

In quel luogo, devono definire X, Y come larghezza e altezza

Ma, quando si crea un'immagine (un'immagine vuota per esempio) è necessario definire Y, X come altezza e larghezza:

img_corner = np.zeros((480, 640, 3), np.uint8) 
0

jveitchmichaelis a https://github.com/ContinuumIO/anaconda-issues/issues/223 ha fornito una risposta esauriente. Qui ho copiato la sua risposta:

The documentation in OpenCV says (hidden away) that you can only write to avi using OpenCV3. Whether that's true or not I've not been able to determine, but I've been unable to write to anything else.

However, OpenCV is mainly a computer vision library, not a video stream, codec and write one. Therefore, the developers tried to keep 

this part as simple as possible. Due to this OpenCV for video containers supports only the avi extension, its first version.

From: http://docs.opencv.org/3.1.0/d7/d9e/tutorial_video_write.html

My setup: I built OpenCV 3 from source using MSVC 2015, including ffmpeg. I've also downloaded and installed XVID and openh264 from Cisco, which I added to my PATH. I'm running Anaconda Python 3. I also downloaded a recent build of ffmpeg and added the bin folder to my path, though that shouldn't make a difference as its baked into OpenCV.

I'm running in Win 10 64-bit.

This code seems to work fine on my computer. It will generate a video containing random static:

writer = cv2.VideoWriter("output.avi", cv2.VideoWriter_fourcc(*"MJPG"), 30,(640,480))

for frame in range(1000): writer.write(np.random.randint(0, 255, (480,640,3)).astype('uint8'))

writer.release()

Some things I've learned through trial and error:

Only use '.avi', it's just a container, the codec is the important thing. 
Be careful with specifying frame sizes. In the constructor you need to pass the frame size as (column, row) e.g. 640x480. However the 

array you pass in, is indexed as (row, column). See in the above example how it's switched? If your input image has a different size to the VideoWriter, it will fail (often silently) Only pass in 8 bit images, manually cast your arrays if you have to (.astype('uint8')) In fact, never mind, just always cast. Even if you load in images using cv2.imread, you need to cast to uint8... MJPG will fail if you don't pass in a 3 channel, 8-bit image. I get an assertion failure for this at least. XVID also requires a 3 channel image but fails silently if you don't do this. H264 seems to be fine with a single channel image If you need raw output, say from a machine vision camera, you can use 'DIB '. 'RAW ' or an empty codec sometimes works. Oddly if I use DIB, I get an ffmpeg error, but the video is saved fine. If I use RAW, there isn't an error, but Windows Video player won't open it. All are fine in VLC.

In the end I think the key point is that OpenCV is not designed to be a video capture library - it doesn't even support sound. VideoWriter is useful, but 99% of the time you're better off saving all your images into a folder and using ffmpeg to turn them into a useful video.

0

Nel mio caso, ho scoperto che la dimensione di scrittore deve abbinato con la dimensione della cornice sia da fotocamera o file. In modo che legga prima le dimensioni del fotogramma e applichino alle impostazioni del writer come sotto.

(grabbed, frame) = camera.read() 
fshape = frame.shape 
fheight = fshape[0] 
fwidth = fshape[1] 
print fwidth , fheight 
fourcc = cv2.VideoWriter_fourcc(*'XVID') 
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (fwidth,fheight)) 
Problemi correlati