簡體   English   中英

遇到 AxisError:軸 1 超出維度 0 數組的范圍

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM