keep-loving-pythonのブログ

Pythonを愛し続けたいです(Pythonが流行っている限りですが。。。)

解決策。TypeError: only integer scalar arrays can be converted to a scalar index

解決策。TypeError: only integer scalar arrays can be converted to a scalar index

エラー

エラー内容

TypeError: only integer scalar arrays can be converted to a scalar index

↓ 詳細

Traceback (most recent call last):
  File "stitching_detailed.py", line 519, in <module>
    main()
  File "stitching_detailed.py", line 351, in main
    img_names_subset.append(img_names[indices[i]])
TypeError: only integer scalar arrays can be converted to a scalar index

環境

windows10
python 3.7
opencv-contrib-python 4.5.3.56
numpy 1.21.6 (1.19.5, 1.18.0もダメだった)

解決策

解決策の前に。。。

以下の記事によると、このメッセージ自体は、ちょっと、「???」かも。

https://stackoverflow.com/questions/50997928/typeerror-only-integer-scalar-arrays-can-be-converted-to-a-scalar-index-with-1d

引用

Perhaps the error message is somewhat misleading, but

では、解決策。

スカラーが欲しいところを、配列(例:" [3] "とか)になっているだけなので。。。

その1

[0]で、中味にする。

        img_names_subset.append(img_names[indices[i][0]])

その2

int()で囲む。

        img_names_subset.append(img_names[int(indices[i])])