简体   繁体   English

遇到 AxisError:轴 1 超出维度 0 数组的范围

[英]Encountering AxisError: axis 1 is out of bounds for array of dimension 0

I was just trying to make a body recognization feature but I am encountering with this axis error.我只是想制作一个身体识别功能,但我遇到了这个轴错误。

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

If you're getting dimension zero, it probably means the capture failed so you've got a zero-sized array.如果您的维度为零,则可能意味着捕获失败,因此您有一个零大小的数组。 Test against ret to make sure it's set to True :测试ret以确保它设置为True

# ...
ret,background = cap.read()

if not ret:
    raise RuntimeError("Couldn't capture an image")

background = np.flip(background,axis=1)

You could also check the capture is actually open before trying to grab a frame:您还可以在尝试抓取帧之前检查捕获是否实际打开:

# ...

if not cap.isOpened():
     raise RuntimeError("Capture is not open - is the webcam connected?")

# ... Read the frame, check ret, flip it, etc.

A side-note: I'm not sure if this is on purpose, but you're also performing the flip only on the last frame as it's outside of the for-loop.旁注:我不确定这是否是故意的,但您也仅在最后一帧上执行翻转,因为它位于 for 循环之外。 If you intend to do it for every frame, it should be indented.如果你打算对每一帧都这样做,它应该缩进。 You're throwing away the first 59 background frames you capture.您正在丢弃您捕获的前 59 个背景帧。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AxisError:轴 1 超出维度 1 数组的范围 - AxisError: axis 1 is out of bounds for array of dimension 1 numpy.AxisError:轴 1 超出维度 1 数组的范围 - numpy 数组 - numpy.AxisError: axis 1 is out of bounds for array of dimension 1 - numpy array 如何解决 AxisError:轴 1 超出维度 0 数组的范围 - How to solve AxisError: axis 1 is out of bounds for array of dimension 0 numpy.AxisError:来源:轴 2 超出了维度 2 数组的范围 - numpy.AxisError: source: axis 2 is out of bounds for array of dimension 2 np.linalg.norm AxisError:轴 1 超出维度 1 数组的范围 - np.linalg.norm AxisError: axis 1 is out of bounds for array of dimension 1 AxisError:计算类的准确性时,轴 1 超出维度 1 数组的范围 - AxisError: axis 1 is out of bounds for array of dimension 1 when calculating accuracy of classes 2D 数组的 median_absolute_deviation 抛出 AxisError: axis 1 is out of bounds for array of dimension 1 - median_absolute_deviation of 2D array throws AxisError: axis 1 is out of bounds for array of dimension 1 AxisError:计算 roc_auc_score 时,轴 1 超出维度 1 数组的范围 - AxisError: axis 1 is out of bounds for array of dimension 1 when calculating roc_auc_score 分类器:轴 1 超出维度 1 数组的范围 - classifier : axis 1 is out of bounds for array of dimension 1 我无法解决“轴-1超出维度0数组的范围”的问题 - I can't solve issue “axis -1 is out of bounds for array of dimension 0”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM