索引错误:索引 7 超出尺寸为 7 的轴 0 的范围

问题描述 投票:0回答:1

我正在尝试评估一个人闭嘴时嘴唇是否移动过多(以得出其正在咀嚼的结论)。

嘴巴闭合部分没有任何问题,但是当我尝试通过标志评估嘴唇运动时(

dlib
),嘴巴的最后一个标志似乎有问题。

受到嘴巴示例的启发(https://github.com/mauckc/mouth-open/blob/master/detect_open_mouth.py#L17),我编写了以下函数:

def lips_aspect_ratio(shape):
    # grab the indexes of the facial landmarks for the lip
    (mStart, mEnd) = (61, 68)
    lip = shape[mStart:mEnd]
    print(len(lip))
    # compute the euclidean distances between the two sets of
    # vertical lip landmarks (x, y)-coordinates
    # to reach landmark 68 I need to get lib[7] not lip[6] (while I get lip[7] I get IndexOutOfBoundError)
    A = dist.euclidean(lip[1], lip[6])  # 62, 68
    B = dist.euclidean(lip[3], lip[5])  # 64, 66

    # compute the euclidean distance between the horizontal
    # lip landmark (x, y)-coordinates
    C = dist.euclidean(lip[0], lip[4])  # 61, 65

    # compute the lip aspect ratio
    mar = (A + B) / (2.0 * C)

    # return the lip aspect ratio
    return mar

嘴唇的标志是

(61, 68)
,当我将嘴唇提取为
lip = shape[61:68]
并尝试访问最后一个标志为
lip[7]
时,我收到以下错误:

IndexError: index 7 is out of bounds for axis 0 with size 7

这是为什么呢?以及如何获得嘴唇/脸部的最后一个标志

python numpy face-recognition dlib face-landmark
1个回答
3
投票
lip = shape[61:68]

切片不包括结束元素。所以你得到了 7 个元素:61,62,63,64,65,66,67。

len(lip) == 7
证实了这一点。

如果嘴唇形状确实有 8 个点,并且它们包含元素 68,则切片应该是:

lip = shape[61:69]
assert(len(range(61, 69) == 8)
assert(len(lip) == 8)

请注意,如果

shape
足够长,则
len(shape[a:b:c]) == len(range(a, b, c))
。这是因为 range 函数的作用就像从无限长的整数列表中取出一个切片(有警告)。

所以,这个问题是一个经典的一对一问题,与人工智能/图像分析没有太大关系:)

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.