1
votes

I am using Python 3.7.3 and I am trying to initiate to OpenCV and video capture and modification. But I am stuck with following problem: When I run the code below, my webcam data are well captured and displayed in imshow window but I am not able to get a proper Numpy array:

import cv2

first_frame = None

video = cv2.VideoCapture(0)
cpt = 0

while True:
    check, frame = video.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray,(21,21), 0)

    if cpt < 40:
        first_frame = gray
        cpt = cpt + 1
        print(cpt)
        continue

    delta_frame = cv2.absdiff(first_frame, gray)

    cv2.imshow("Gray frame", gray)

    cv2.imshow("Delta frame", delta_frame)

    key = cv2.waitKey(1)
    print(frame)
    if key == ord('q'):
        break

video.release()
cv2.destroyAllWindows()

My webcam images are well captured because I can see the result on the window opened by imshow().

But the numpy array return by print(frame) is made of zero values only while displayed images are not fully black, I can see my face:

>>> frame
array([[[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],
       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],
       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],
       ...,
       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],
       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],
       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]]], dtype=uint8)
1
What does print(np.sum(frame)) return? Also you are showing gray and delta_frame so you might want to print those or imshow(frame). Nonetheless it should have non-zero valuesJulian
Take a look at thisnathancy

1 Answers

0
votes

Frame should be a proper np array. Have you checked the entire frame? Print only shows a tiny proportion of the frame. You can print np.nonzero(frame) to get the index of all populated points.