【问题标题】:"integer argument expected, got float" ERROR“需要整数参数,得到浮点数”错误
【发布时间】:2021-04-22 10:50:54
【问题描述】:

我正在使用 OpenCV 在 python 中进行跳棋游戏,但出现了一个错误:

integer argument expected, got float

我不明白为什么。给我错误的行是:

cv2.circle(frameToDraw, (tuple(shapeTarget.center)), 10, (0, 0, 255), 1)

我的完整代码在这里:

 def drawShapes(self, shapeTargets, **kwargs):  #RETURNS A FRAME WITH THE SHAPES DRAWN ON IT
    """
    Draws circles on every center of the shapes in the shapeArray
    frameToDraw: What frame to draw on top of.
    """

    frameToDraw = kwargs.get('frameToDraw', self.vid.frame.copy())
    color       = kwargs.get('color', (0, 255, 0))

    for shapeTarget in shapeTargets:
        if hasattr(shapeTarget, 'center'):
            cv2.circle(frameToDraw, (tuple(shapeTarget.center)), 10, (0, 0, 255), 1)
            cv2.polylines(frameToDraw, [np.asarray(shapeTarget.vertices)], True, color, 4)

    return frameToDraw

谁能帮我解决这个问题,让我完成我的项目?

【问题讨论】:

  • 最有可能在元组(tuple(shapeTarget.center)) 中有float 值,但应该有int。这是解决方案之一(tuple(map(int, shapeTarget.center)))
  • “我不明白为什么。” -- 然后做一些调试。考虑到该函数只有一个参数可能是罪魁祸首,这并不是工作量太大……可能比写下这个问题所花费的时间更少(即添加print(shapeTarget.center),看看会显示什么) .
  • @crackanddie 我已经尝试过你的建议,但又出现一个错误,提示“circle() 缺少必需的参数 'radius' (pos 3)”。我很确定我已经输入了半径。你认为我的错误是什么?我错过了什么吗?

标签: python opencv artificial-intelligence python-3.9


【解决方案1】:

尝试解压位置的 x 和 y 坐标,然后将其传递到带有 int 包装器的元组中:

cv2.circle(frameToDraw, (tuple(shapeTarget.center)), 10, (0, 0, 255), 1)

x, y = shapeTarget.center
cv2.circle(frameToDraw, (int(x), int(y)), 10, (0, 0, 255), 1)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-16
    • 2018-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-07
    相关资源
    最近更新 更多