[英]Encountering AxisError: axis 1 is out of bounds for array of dimension 0
我只是想制作一个身体识别功能,但我遇到了这个轴错误。
import cv2
import time
import numpy as np
## Preparation for writing the ouput video
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc,20.0, (640,480))
##reading from the webcam
cap = cv2.VideoCapture(0)
## Allow the system to sleep for 3 seconds before the webcam starts
time.sleep(3)
count = 0
background = 0
## Capture the background in range of 60
for i in range(60):
ret,background = cap.read()
background = np.flip(background,axis=1)
AxisError
Traceback (most recent call last) <ipython-input-6-28a19252eb8f> in <module>()
for i in range(60):
ret,background = cap.read()
---> background = np.flip(background,axis=1) AxisError: axis 1 is out of bounds for array of dimension 0
如果您的维度为零,则可能意味着捕获失败,因此您有一个零大小的数组。 测试ret
以确保它设置为True
:
# ...
ret,background = cap.read()
if not ret:
raise RuntimeError("Couldn't capture an image")
background = np.flip(background,axis=1)
您还可以在尝试抓取帧之前检查捕获是否实际打开:
# ...
if not cap.isOpened():
raise RuntimeError("Capture is not open - is the webcam connected?")
# ... Read the frame, check ret, flip it, etc.
旁注:我不确定这是否是故意的,但您也仅在最后一帧上执行翻转,因为它位于 for 循环之外。 如果你打算对每一帧都这样做,它应该缩进。 您正在丢弃您捕获的前 59 个背景帧。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.