意外的错误?

时间:2017-04-20 14:43:19

标签: python machine-learning

所以最近我一直在测试senddex的机器学习代码并遇到如下错误:

Traceback (most recent call last):
File "C:\Users\---\Desktop\Images\Foundations Picture\imagerecognition 
(1).py", line 61, in whatNumIsThis
loadExamps = open('numArEx.txt','r').read()
IOError: [Errno 2] No such file or directory: 'numArEx.txt'

至于我的编码,它来自以下:

from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import time
#check if it works
from collections import Counter


def createExamples():
     #Below, the file will create itself after running the script
     numberArrayExamples = open('numArEx.txt','a')
     #Different Diseases
     numbersWeHave = range(0,7)
     #Different pictures of the same disease
     versionsWeHave = range(1,4)
     #creates a sample file in the directory
        for eachNum in numbersWeHave:
        for eachVer in versionsWeHave:
            #print str(eachNum)+'.'+str(eachVer)
             imgfilePath = ''+str(eachNum)+'.'+str(eachVer)+'.png'
            ei = Image.open(imgFilePath)
            eiar = np.array(ei)
            eiar1 = str(eiar.tolist())

            lineToWrite = str(eachNum)+'::'+eiar1+'\n'
            numberArrayExamples.write(lineToWrite)

#changing array to make it black and white
def threshold(imageArray):
    balanceAr = []
    newAr = imageArray
    #For every color, it would take all of the lighter colors to be white 
and all of the darkers colors to be black.
    for eachRow in imageArray:
        for eachPix in eachRow:
            avgNum = reduce(lambda x, y: x + y, eachPix[:3])/len(eachPix[:3])
            balanceAr.append(avgNum)
     balance = reduce(lambda x, y: x + y, balanceAr/len(balanceAr))
     #Changing the color
    for eachRow in newAr: 
        for eachPix in eachRow:
            if reduce(lambda x, y: x + y, eachPix[:3])/len(eachPix[:3]) > 
balance:
                eachPix[0] = 255
                eachPix[1] = 255
                eachPix[2] = 255
                eachPix[3] = 255
            else:
                eachPix[0] = 0
                eachPix[0] = 0
                eachPix[0] = 0
                eachPix[0] = 255
    return newAr

#pattern recognition. It compares pictures   
def whatNumIsThis(filePath): 
    matchAr = []
    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

    graphX = []
    graphY = []

    for eachThing in x:
        print eachThing
        graphX.append(eachThing)
        print x[eachThing]
        graphY.append(x[eachThing])

    fig = plt.figure()
    ax1 = plt.subplot2grid((4,4),(0,0), rowspan=1, colspan=4)
    ax2 = plt.subplot2grid((4,4),(1,0), rowspan=3, colspan=4)


    ax1.imshow(iar)

    ax2.bar(graphX,graphY, align='center')
    #limits y-axis to 400
    plt.ylim(400)

    xloc = plt.MaxNLocator(12)

    ax2.xaxis.set_major_locator(xloc)


#import images in here
whatNumIsThis('Foundations Picture/0.1.png')

当我阅读此代码的指南时,它说newArEx.txt文件应该自己创建,所以我很困惑为什么我得到错误。有人可以解释一下吗?谢谢。

1 个答案:

答案 0 :(得分:0)

您要求它加载txt文件,但您还没有创建它。您需要先运行whatNumIsThis()。然后你可以运行createExamples()。此外,你的缩进仍然是关闭。在imgfilePath中,您可以为变量imgFilePath命名,但请致电pip install mysqldb-python