使用TUM的associate.py脚本报错AttributeError: ‘dict_keys‘ object has no attribute ‘remove‘

运行脚本文件报错
使用TUM的associate.py脚本报错AttributeError: ‘dict_keys‘ object has no attribute ‘remove‘_第1张图片
错误原因:
python2 和python3的版本差异
在Python 3中,dict.keys()返回没有remove方法的dict_keys对象。与Python 2不同,Python 2 dict.keys()返回列表对象。
In Python 3, dict.keys() returns a dict_keys object (a view of the dictionary) which does not have remove method; unlike Python 2, where dict.keys() returns a list object.

解决办法
(1)直接强制用python2运行

python2 associate.py rgb.txt depth.txt > associate.txt

(2)还是用python3运行,小改一下源码
原本的这里

	for diff, a, b in potential_matches:
        if a in first_keys and b in second_keys:
            first_keys.remove(a)
            second_keys.remove(b)
            matches.append((a, b))
    
    matches.sort()
    return matches

上面加两句,把dict变为list

	# 新加的两行
	first_keys = list(first_keys)
    second_keys = list(second_keys)
    for diff, a, b in potential_matches:
        if a in first_keys and b in second_keys:
            first_keys.remove(a)
            second_keys.remove(b)
            matches.append((a, b))
    
    matches.sort()
    return matches

再运行(这里的python默认指向python3)

python associate.py rgb.txt depth.txt > associate.txt

你可能感兴趣的:(VSLAM,python,开发语言)