import os
import numpy as np
import matplotlib.pyplot as plt
from skimage import color,data,transform,io
labelList = os.listdir("F:\\MachineLearn\\ML-xiaoxueqi\\fruits\\Training")
allFruitsImageName = []
for i in range(len(labelList)):
allFruitsImageName.append(os.listdir("F:\\MachineLearn\\ML-xiaoxueqi\\fruits\\Training\\"+labelList[i]))
allsortImageName = []
for i in range(len(allFruitsImageName)):
oneClass = allFruitsImageName[i]
nr = []
r = []
r2 = []
for i in range(len(oneClass)):
if(oneClass[i].split("_")[0].isdigit()):
nr.append(int(oneClass[i].split("_")[0]))
else:
if(len(oneClass[i].split("_")[0])==1):
r.append(int(oneClass[i].split("_")[1]))
else:
r2.append(int(oneClass[i].split("_")[1]))
sortnr = sorted(nr)
sortnrImageName = []
for i in range(len(sortnr)):
sortnrImageName.append(str(sortnr[i])+"_100.jpg")
sortr = sorted(r)
sortrImageName = []
for i in range(len(sortr)):
sortrImageName.append("r_"+str(sortr[i])+"_100.jpg")
sortr2 = sorted(r2)
sortr2ImageName = []
for i in range(len(sortr2)):
sortr2ImageName.append("r2_"+str(sortr2[i])+"_100.jpg")
sortnrImageName.extend(sortrImageName)
sortnrImageName.extend(sortr2ImageName)
allsortImageName.append(sortnrImageName)
trainData = []
for i in range(len(allsortImageName)):
one = []
for j in range(len(allsortImageName[i])):
rgb=io.imread("F:\\MachineLearn\\ML-xiaoxueqi\\fruits\\Training\\"+labelList[i]+"\\" + allsortImageName[i][j]) #读取图片
gray=color.rgb2gray(rgb) #将彩色图片转换为灰度图片
dst=transform.resize(gray,(64,64)) #调整大小,图像分辨率为64*64
one.append(dst)
trainData.append(one)
print(np.shape(trainData))
trainLabelNum = []
for i in range(len(trainData)):
for j in range(len(trainData[i])):
trainLabelNum.append(i)
imageGray = trainData[i][j]
io.imsave("F:\\MachineLearn\\ML-xiaoxueqi\\fruits\\trainGrayImage\\"+str(i)+"_"+str(j)+".jpg",imageGray)
np.save("F:\\MachineLearn\\ML-xiaoxueqi\\fruits\\trainLabelNum",trainLabelNum)
print("图片处理完了")
testLabelList = os.listdir("F:\\MachineLearn\\ML-xiaoxueqi\\fruits\\Test")
testallFruitsImageName = []
for i in range(len(testLabelList)):
testallFruitsImageName.append(os.listdir("F:\\MachineLearn\\ML-xiaoxueqi\\fruits\\Test\\"+testLabelList[i]))
testallsortImageName = []
for i in range(len(testallFruitsImageName)):
oneClass = testallFruitsImageName[i]
nr = []
r = []
r2 =