【问题标题】:(python error code) slice indices must be integers or None or have an __index__ method(python 错误代码)切片索引必须是整数或 None 或具有 __index__ 方法
【发布时间】:2020-05-20 16:15:26
【问题描述】:

enter image description here[在此处输入图像描述][2] (base) C:\Users\aaa\TPU-Posenet>pose_camera_single_tpu.py --videofile video1.mp4 回溯(最近一次通话最后): 文件“C:\Users\aaa\TPU-Posenet\pos​​e_camera_single_tpu.py”,第 122 行,在 res, inference_time = engine.DetectPosesInImage(prepimg) DetectPosesInImage 中的文件“C:\Users\aaa\TPU-Posenet\pos​​e_engine.py”,第 126 行 输出 = [输出[i:j] for i, j in zip(self._output_offsets, self._output_offsets[1:])] 文件“C:\Users\aaa\TPU-Posenet\pos​​e_engine.py”,第 126 行,在 输出 = [输出[i:j] for i, j in zip(self._output_offsets, self._output_offsets[1:])] TypeError:切片索引必须是整数或无或具有 index 方法


在 Windows Python 中, 我们正在开发一个项目,以使用 anaconda 提示检测图像中的对象。目前, 开发环境全部搭建完毕,剩下最后一段代码, 但是由于此错误代码,我们无法继续前进。

我仍在努力,但我没有取得任何进展。有什么问题?也许这是numpy版本的问题..帮助大家

enter image description here

【问题讨论】:

  • 请出示您的代码
  • 什么样的源代码? Pose_camera_single_tpu.py?
  • 导致错误的代码
  • pose_camera_single_tpu.py / 中存在错误。第 122 行。我将图像附加到 Imgur。
  • 如您所见,第 122 行仅包含对 engine.DetectPosesInImage 的调用。你能证明这部分代码引发了异常吗?

标签: python anaconda


【解决方案1】:

原因是您的代码在索引中有一个 for 循环。您不能将 for 循环放在索引中。列表[对于 i,j]。至少,不是那样。使用列表,您只能以这种方式将整数作为索引的参数。所以 list[integer:integer] 是可以接受的,但你所做的不是。

outputs = [output[i:j] for i, j in zip(self._output_offsets, self._output_offsets[1:])]

如果您尝试将列表设置为 [output[i:j], for argument],这将不起作用,因为 for 是一个关键字。 Python 认为您试图在列表中放置一个 for 循环。

【讨论】:

  • 我尝试修改代码,但错误代码没有变化。
  • 我看了你的代码,也许是我不知道python的东西,但是你为什么把你的for循环放在你的输出索引中呢?我认为您不能将循环函数放在索引中。
  • 这个答案是错误的。听起来您还没有听说过列表推导。
猜你喜欢
  • 2014-01-11
  • 1970-01-01
  • 1970-01-01
  • 2018-06-06
  • 2020-07-01
  • 1970-01-01
  • 2017-07-27
  • 2015-04-01
  • 2017-10-16
相关资源
最近更新 更多