0

I wrote the following code to open a video file and the file is in the same directory as the script, moreover the code to write a video feed from the camera to the file is not working!

import numpy as np
import cv2
cap = cv2.VideoCapture('F:/vtest.avi')
print cap.isOpened()
if(cap.isOpened()== False):
    cap.open('F://vtest.avi')
    print cap.isOpened()
while(cap.read()):
    ret,frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    cv2.imshow('frame',gray)
    if cv2.waitKey(1) == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

But the code threw the following errors. I tried moving the ffmpeg.dll file in Python directory but to no avail.

False
False
Traceback (most recent call last):
  File "F:\2.py", line 11, in <module>
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
error: ..\..\..\..\opencv\modules\imgproc\src\color.cpp:3480: error: (-215) scn == 3 || scn == 4 in function cv::cvtColor
  • possible duplicate of [How does one convert a grayscale image to RGB in OpenCV (Python) for visualizing contours after processing an image in binary?](http://stackoverflow.com/questions/21596281/how-does-one-convert-a-grayscale-image-to-rgb-in-opencv-python-for-visualizing) – Samer Jun 22 '15 at 22:17
  • @samer its no duplicate... these two are entirely different problems! – Shikhar Gupta Jun 28 '15 at 08:20

1 Answers1

0

Try this:

import numpy as np
import cv2
cap = cv2.VideoCapture('F:/vtest.avi')
while(cap.isOpened()):
    ret,frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    cv2.imshow('frame',gray)
    if cv2.waitKey(1) == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

Also, place the video in the same directory as this script and check if it works.

Specas
  • 98
  • 1
  • 8