You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python2.7中Aruco检测无标记时ids为None调用any报AttributeError怎么解决

错误根因

OpenCV的cv2.aruco.detectMarkers()函数在未检测到任何有效Aruco标记时,返回的第二个值ids为None,而非空numpy数组。此时直接调用ids.any()会触发NoneType无对应属性的报错。

修复方案

你只需要在调用any()方法前,先判断ids是否不为None即可,修改后的代码如下:

grey_im  = cv2.cvtColor(image_np, cv2.COLOR_BGR2GRAY)
(corners, ids, rejected) = cv2.aruco.detectMarkers(grey_im, self.alg.ARUCO_DICT, parameters=self.alg.ARUCO_PARAMS)

# 先判断ids不为None,再判断存在有效id
if ids is not None and ids.any():
    cnt = 0
    for id in ids:

注:原判断逻辑ids.any() != 0可直接简化为ids.any(),any()方法本身返回的就是布尔值,不需要额外做等值判断。

可选优化

如果你需要同时确认检测到的角点数据也有效,可以叠加对corners的判断,避免极端场景下的异常:

if ids is not None and len(corners) > 0 and ids.any():
    # 后续处理逻辑

内容的提问来源于stack exchange,提问作者Wilson Lysford

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 11:09:00