0

I noticed I started having this issue as soon as I built the opencv_contrib module, because before my code was working and now I cannot access my web-cam with opencv even with the simple face detection program. Instead I get this error:

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.error: /io/opencv/modules/imgproc/src/color.cpp:10638: error: (-215) scn == 3 || scn == 4 in function cvtColor

But for a simple program to open Camera it just exits i am with the opinion that it reads false for ret at line ret, frame = cam.read

This is my code for launching: web-cam

import cv2

cv2.namedWindow("preview")
cam = cv2.VideoCapture(0)

if cam.isOpened():  # try to get the first frame
    ret, frame = cam.read()
else:
    ret = False

while ret:
    cv2.imshow("preview", frame)
    ret, frame = cam.read()
    key = cv2.waitKey(20)
    if key == 27: 
        break

cv2.destroyWindow("preview")
cam.release()






  In[5] print (cv2.__version__)
     3.3.0
  In[6] webcam = cv2.VideoCapture(0)
        ret,frame = webcam.read()
        print (ret)
    False
Durodola Opemipo
  • 319
  • 2
  • 12

1 Answers1

0

This may not be the best solution but I had to remove the compiled cv2.so file in /usr/local/lib/python2.7and /usr/local/lib/python3.5/dist-packages and started my installation from scratch from my build directory:

  1. cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules -D BUILD_EXAMPLES=ON ..
  2. make -j7
  3. make install
  4. sudo ldconfig
Durodola Opemipo
  • 319
  • 2
  • 12