python - OpenCV 在 vi​​deo.read 上返回一个全零数组

标签 python python-3.x opencv ffmpeg

下面是相关代码

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()

代码显示从我的网络摄像头捕获的视频。尽管如此,OpevCV 似乎并未处理任何帧,因为所有帧看起来都是这样

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

我认为这意味着它们是空的。

我认为这会阻止算法在画面中检测到我的脸。我感觉问题出在 ffmpeg 编解码器上,但即使是这种情况,我也不确定如何继续。

操作系统:Windows 10 语言:Python

编辑:框架不为空,但数组中的所有值似乎都是“0”

为什么框架是空的,我怎样才能让 OpenCV 检测框架中我的脸?

最佳答案

有时无法读取图像,因此cv2返回一个空矩阵,您需要确保框架不为空。

...
while True:
    a += 1
    check, frame = video.read()
    if not check or frame.empty():
        continue
    ...

关于python - OpenCV 在 vi​​deo.read 上返回一个全零数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55269943/

相关文章:

python - 加快顺序检查点是否在 Python 中的形状

python - 如何在 python 中抓取 instagram 帐户信息

python - python mysql 中表名的占位符

python-3.x - "ImportError: cannot import name ' convert_from_path ' from partially initialized module ' pdf2image ' (most likely due to a circular import)"

python - cv2.imwrite无法写入正确的像素值

android - 从 native 代码在 android 设备屏幕上显示 rgb888 数据

python - 值错误 : Invalid element(s) received for the 'data' property

python - Fuzz Ratio 的增加速度

c++ - 如何解决 OpenCV 3 和 C++ 中的 GpuAcc_64.dll 异常?

python - 在空列表上使用 .insert