Traceback - TypeError: 'set' object is not subscriptable

Traceback - TypeError: 'set' object is not subscriptable

我一直在研究 AI,它将解决单人纸牌游戏。突然,经过一些更改(我不知道如何更改)后,控制台出现错误,即:

如下代码,错误指的是:

def CatchingCard(self, imageName, accurate=0.90):
    finalPath = os.path.join(os.getcwd(),
                                 self.vals.ImageDepository, imageName)
    pos = TryToFindCardInGame(
        finalPath, accurate)
    if pos[0] != -1:
        EasyCatch(finalPath, pos, "left", 0, False, offset=5)
        return True
    return False

以及 TryToFindCardInGame:

def TryToFindCardInGame(image, accurate=0.8):
cardImage = pyautogui.screenshot()
if not retina_enabled:
    pass
else:
    assert isinstance(cardImage.thumbnail, object)
    cardImage.thumbnail
colorScheme = np.array(cardImage)
colorDark = cv2.cvtColor(colorScheme, cv2.COLOR_BGR2GRAY)
colorLayout = cv2.imread(image, 0)
colorLayout.shape[::-1]

cv_matching = cv2.matchTemplate(colorDark, colorLayout, cv2.TM_CCOEFF_NORMED)
max_loc: object
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(cv_matching)
assert isinstance(max_val, object)
if accurate <= max_val:
    return max_loc
return {-1, -1}

我得到的错误:

if pos[0] != -1:
TypeError: 'set' object is not subscriptable

我一直在四处寻找,找不到适合我的情况的解决方案。 我应该寻找什么?

看起来像TryToFindCardInGame returns一个集合,它不同于列表。 https://docs.python.org/3.5/library/stdtypes.html#set-types-set-frozenset

python 中的集合对象未排序,因此它们不支持您提供的索引 if pos[0] != -1

我对您正在使用的模块了解不多,但您可以尝试将数据类型从 set 更改为 list 以访问这样的索引

if list(pos)[0] != -1

或者在您的 TryToFindCardInGame 中,您可以将 max_loc 的数据类型更改为列表或元组(如果它是一个集合),并将 return {-1,-1} 更改为 return [-1,-1]