【问题标题】:TypeError: tuple indices must be integers or slices, not str, when using MTCNNTypeError:使用 MTCNN 时,元组索引必须是整数或切片,而不是 str
【发布时间】:2021-09-13 04:20:12
【问题描述】:

当我想访问以下元组时出现以下错误

[{'box': [215, 102, 179, 238], “信心”:0.999460756778717, '关键点': {'left_eye': (277, 193), 'mouth_left': (287, 284), 'mouth_right': (356, 283), '鼻子': (330, 239), 'right_eye': (361, 191)}}]

for person in face_locations:
bounding_box = person['box']   #in this line I get the error
keypoints = person['keypoints']



cv2.rectangle(img_final,
              (bounding_box[0], bounding_box[1]),
              (bounding_box[0]+bounding_box[2], bounding_box[1] + bounding_box[3]),
              (0,155,255),
              2)
cv2.circle(img_final,(keypoints['left_eye']), 2, (0,155,255), 2)
cv2.circle(img_final,(keypoints['right_eye']), 2, (0,155,255), 2)
cv2.circle(img_final,(keypoints['nose']), 2, (0,155,255), 2)
cv2.circle(img_final,(keypoints['mouth_left']), 2, (0,155,255), 2)
cv2.circle(img_final,(keypoints['mouth_right']), 2, (0,155,255), 2) 

【问题讨论】:

  • 代码应该在正确的标识下正常工作。您应该检查 face_locations 内的每个对象是否有错误。可能是您没有从正确的位置获取数据,或者可能是以前的一些错误是给您一个元组而不是一个对象

标签: python cv2


【解决方案1】:

而不是这个:

for person in face_locations:
bounding_box = person['box']   
keypoints = person['keypoints']

你应该这样做:

person = face_locations[0]
bounding_box = person['box']
keypoints = person['keypoints']
print(bounding_box, keypoints)

结果:

 [`215, 102, 179, 238]`

{'left_eye': (277, 193), 'mouth_left': (287, 284), 'mouth_right': (356, 283), 'nose': (330, 239), 'right_eye': (361, 191)}

【讨论】:

    猜你喜欢
    • 2017-03-11
    • 2019-11-23
    • 2018-12-29
    • 2021-01-18
    • 2018-03-12
    • 1970-01-01
    • 2015-12-09
    • 2019-02-05
    • 2021-11-28
    相关资源
    最近更新 更多