比较函数有问题,我认为它没有正确读取所有样本。有什么建议吗?

2024-06-02 09:10:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我对图像识别非常在行,但一直在关注sendtex的视频系列。该程序一直工作到视频9/10,其中建立了一个函数来比较样本图像和正在评估的图像。这是视频->;https://www.youtube.com/watch?v=ry9AzwTMwJQ

我想问题可能出在“whatNumIsThis”函数中。我测试了视频中15:53显示的计数器工具,计数器工作正常。将变量“matchedAr”附加到变量“currentNum”可能会出现问题。或者该函数中的另一个变量有问题,我测试了其他函数直到那个点,只是在每一步打印结果,它们工作正常。你知道吗


def whatNumIsThis(filePath):
    matchedAr = []
    loadExamps = open('numArEx.txt','r').read()
    loadExamps = loadExamps.split('\n')

    i = Image.open(filePath)
    iar = np.array(i)
    iarl = iar.tolist()

    inQuestion = str(iarl)

    for eachExample in loadExamps:
        if len(eachExample) > 3:
            splitEx = eachExample.split('::')
            currentNum = splitEx[0]
            currentAr = splitEx[1]

            eachPixEx = currentAr.split('],')

            eachPixInQ = inQuestion.split('],')

            x = 0

            while x < len(eachPixEx):
                if eachPixEx[x] == eachPixInQ[x]:
                    matchedAr.append(int(currentNum))

                x += 1

    print (matchedAr)
    x = Counter(matchedAr)
    print (x)

没有错误消息,而是返回1的计数器,如下所示-


[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
Counter({1: 100})

我已经查过了numArEx.txt文件它有它应该有的所有样本,而不仅仅是数字“1”的样本。我希望它返回所有可用数字实例的计数,而不仅仅是“1”。不知道该怎么办。你知道吗


Tags: 函数图像视频计数器opensplit样本filepath