1

Below is the relevant code

import cv2 as cv
import numpy as np

video = cv.VideoCapture(0) #tells obj to use built in camera\

#create a face cascade object 
face_cascade  = 
cv.CascadeClassifier(r"C:\Users\xxxxxxx\AppData\Roaming\Python\Python36\site- 
packages\cv2\data\haarcascade_frontalcatface.xml")

a = 1
#create loop to display a video
while True:
    a = a + 1
    check, frame = video.read()
    print(frame)

    #converts to a gray scale img
    gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)

    #create the faces
    faces = face_cascade.detectMultiScale(gray, scaleFactor=1.5, minNeighbors=5)

    for(x, y, w, h) in faces:
        print(x, y, w, h)

    #show the image
    cv.imshow('capturing', gray)

    key = cv.waitKey(1) #gen a new frame every 1ms

    if key == ord('q'): #once you enter 'q' the loop will be exited
        break

print(a) #this will print the number of frames

#captures the first frame
video.release() #end the web cam

#destroys the windows when you are not defined
cv.destroyAllWindows()

The code displays a video captured from my webcam camera. Despite that, OpevCV doesn't seem to be processing any frames as all the frames look like this

 [[0 0 0]
  [0 0 0]
  [0 0 0]
  ...
  [0 0 0]
  [0 0 0]
  [0 0 0]]]

which I assume means that they are empty.

This I believe is preventing the algorithm from being able to detect my face in the frame. I have a feeling that the issue lies in the ffmpeg codec, but I'm not entirely sure how to proceed even if that is the case.

OS: Windows 10 Language: Python

EDIT: The Frame is not empty but all the values in the array seem to be '0'

Why is the frame empty and how can I get OpenCV to detect my face in the frame?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Ikechukwu Anude
  • 346
  • 1
  • 4
  • 16
  • 1
    I would suggest rewording your question---all zeros is not an empty frame, but a completely black frame. An "empty" frame would be more akin to getting `None` for your `frame`, instead of a black image. Are you getting this for every frame? How are you opening the video capture device? You've left that part of your code out. – alkasm Mar 20 '19 at 21:30
  • @AlexanderReynolds That first part is correct and I will reword it. But this is the extent of the code that I wrote for this project. I do not know much about what is going on in the backend. – Ikechukwu Anude Mar 21 '19 at 04:06
  • Sorry, your code section scrolls for like 3 lines and I didn't realize it scrolled, thought you left the top bit out. Are you sure that `0` corresponds to your webcam? If you have multiple cameras connected to your computer, it might be `1` or `2`...etc. Otherwise, be sure that you can grab webcam feed on your computer outside of OpenCV and that it looks correct. If everything is fine but OpenCV just has black images, I would look into re-installing OpenCV, building from source if possible. – alkasm Mar 21 '19 at 18:19
  • @AlexanderReynolds I think it does because when I run my code, it opens up the front facing in built camera. – Ikechukwu Anude Mar 23 '19 at 04:38

4 Answers4

2

Sometimes the image cannot be read, so cv2 return an empty matrix, you need to make sure that the frame isn't empty.

...
while True:
    a += 1
    check, frame = video.read()
    if not check or frame.empty():
        continue
    ...
Alakazam
  • 461
  • 5
  • 14
2

It seems that this happens if you are using Windows
I came up with the solution after having this warning

[ WARN:0] global C:\projects\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (674) SourceReaderCB::~SourceReaderCB terminating async callback

I found the answer here

video = cv.VideoCapture(0, cv2.CAP_DSHOW)

According to the documentation cv2.CAP_DSHOW refers to Direct Show which is a multimedia framework and API to perform various operations with media files or streams.

There's an example in the documentation

Brydenr
  • 798
  • 1
  • 19
  • 30
EduL
  • 21
  • 1
  • 3
  • 1
    Can you add some explanation to your answer? – Brydenr Dec 17 '19 at 18:15
  • According to the documentation cv2.CAP_DSHOW refers to Direct Show which is a multimedia framework and API to perform various operations with media files or streams. – EduL Dec 18 '19 at 00:05
1

Ensure that you are able to connect to the camera

...
while True:
    a = a + 1
    if video.isOpened():
        check, frame = video.read()
        print(frame)
    ...
nathancy
  • 42,661
  • 14
  • 115
  • 137
0

I think this due to the Antivirus. Try disabling it.

Mayur
  • 1