6

I tried playing a video from a file, as given in the tutorials. My program was as follows:

import numpy as np
import cv2

cap = cv2.VideoCapture('output.avi')

while(cap.isOpened()):
    ret, frame = cap.read()
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    cv2.imshow('outVideo',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

But I got the following error:

Traceback (most recent call last):
  File "playVideo.py", line 8, in <module>
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.error: /home/hp/opencv/modules/imgproc/src/color.cpp:7456: error: (-215) scn == 3 || scn == 4 in function ipp_cvtColor

I checked ret and it turned out to be False. So the actual problem is with saving video. I used the following code to save 'output.avi' using VideoWriter function:

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()

I am not able to open 'output.avi', even using VLC

Hari Prasad
  • 71
  • 1
  • 1
  • 3
  • Do you get this error on the first frame or later in the video ? Can you take a look at your `frame` before doing the `cvtColor` ? Is it what you would expect ? – Sunreef May 25 '16 at 09:12
  • @Sunreef I am getting same error but later in the video. Can you suggest something here? Thanks – Dilip Lilaramani Mar 30 '17 at 05:58

3 Answers3

4

First :

check ret value with: ret==True

Second as tutorials said:

Make sure proper versions of ffmpeg or gstreamer is installed. Sometimes, it is a headache to work with Video Capture mostly due to wrong installation of ffmpeg/gstreamer.

from: http://docs.opencv.org/3.1.0/dd/d43/tutorial_py_video_display.html#gsc.tab=0

Finally check the video codec: Can't open video with opencv2

Community
  • 1
  • 1
Destrif
  • 2,104
  • 1
  • 14
  • 22
3

Change the "while"- loop parameter to "ret" - and the order of the cap.read() - ret is True if there is a valid next frame in the video/file stream.

import numpy as np
import cv2

cap = cv2.VideoCapture('output.avi')
ret, frame = cap.read()

while(ret):
    ret, frame = cap.read()
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    cv2.imshow('outVideo',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
    ret, frame = cap.read()

cap.release()
cv2.destroyAllWindows()
Slettal
  • 1,007
  • 13
  • 19
PieterS
  • 31
  • 2
0

I had faced the same error. But the issue was due to a missing package. It wasn't detected while using a jupyter notebook, but it showed up when I run the .py through terminal.

sudo apt-get install python-tk

This solved the error for me, hope it helps somebody else too :)

Udayraj Deshmukh
  • 1,814
  • 1
  • 14
  • 22