所以最近我一直在sentdex的機器學習代碼測試和跨錯誤來,如下圖所示:意外錯誤?
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文件應該由它自己創建,所以我很困惑爲什麼我得到了錯誤。有人可以解釋嗎?謝謝。
您是否首先調用'createExamples()'?你可以修復縮進,你能告訴你如何運行程序嗎?我沒有看到任何函數調用... – MooingRawr
哦,我用我的部分編碼。我現在編輯,以便可以看到整個代碼。 – Nardea
你永遠不會首先調用'createEamples()'...就像我之前說的先調用它然後調用你的函數 – MooingRawr