0

When I run the command python faceDetection.py I get the error-

VIDEOIO ERROR: V4L: can't open camera by index 0
Traceback (most recent call last):
  File "faceDetection.py", line 21, in <module>
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.error: OpenCV(4.0.0-alpha) /home/pi/opencv/modules/imgproc/src/color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

The code for face Detection.py is

import numpy as np
import cv2
faceCascade = cv2.CascadeClassifier('Cascades/haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
cap.set(3,640) # set Width
cap.set(4,480) # set Height
while True:
    ret, img = cap.read()
    img = cv2.flip(img, -1)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = faceCascade.detectMultiScale(
        gray,     
        scaleFactor=1.2,
        minNeighbors=5,     
        minSize=(20, 20)
    )
    for (x,y,w,h) in faces:
        cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = img[y:y+h, x:x+w]  
    cv2.imshow('video',img)
    k = cv2.waitKey(30) & 0xff
    if k == 27: # press 'ESC' to quit
        break
cap.release()
cv2.destroyAllWindows()

Please help....

0

Check camera is enabled in your Raspberry Pi Preferences.

See more in official documentation.

0

I think your camera is not enabled for openCV to use.

run this line of code before starting your program.

sudo modprobe bcm2835-v4l2

This will enable your camera to be available to openCV programs.

To make this automatic, try adding the line to crontab so that the pi will execute the code automatically every time it reboots.

0

I recommend to use OpenCV 3 than 4 because OpenCV 4 is in beta and OpenCV 3 is a stable version. To install OpenCV in your Raspbian Stretch, just follow this tutorial in PyImageSearch in this link. I tried this and its work but building OpenCV takes two hours in faster micro sd card.

I Hope It Helps You!

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.