from numpy import * import operator from os import listdir #从列方向扩展 #tile(a,(size,1)) def knn(k,testdata,traindata,labels): traindatasize=traindata.shape[0] dif=tile(testdata,(traindatasize,1))-traindata sqdif=dif**2 sumsqdif=sqdif.sum(axis=1) distance=sumsqdif**0.5 sortdistance=distance.argsort() count={} for i in range(0,k): vote=labels[sortdistance[i]] count[vote]=count.get(vote,0)+1 sortcount=sorted(count.items(),key=operator.itemgetter(1),reverse=True) return sortcount[0][0] #图片处理 #先将所有图片转为固定宽高,比如32*32,然后再转为文本 #pillow from PIL import Image im=Image.open("C:/Users/Administrator/Desktop/33.png") fh=open("C:/Users/Administrator/Desktop/33.txt","a") #im.save("C:/Users/me/Pictures/weixin.bmp") width=im.size[0] height=im.size[1] #k=im.getpixel((1,9)) #print(k) for i in range(0,width): for j in range(0,height): cl=im.getpixel((i,j)) clall=cl[0]+cl[1]+cl[2] if(clall==0): #黑色 fh.write("1") else: fh.write("0") fh.write("\n") fh.close() #加载数据 def datatoarray(fname): arr=[] fh=open(fname) for i in range(0,32): thisline=fh.readline() for j in range(0,32): arr.append(int(thisline[j])) return arr arr1=datatoarray("E:/m/others/traindata/0_4.txt") #建立一个函数取文件名前缀 def seplabel(fname): filestr=fname.split(".")[0] label=int(filestr.split("_")[0]) return label #建立训练数据 def traindata(): labels=[] trainfile=listdir("E:/m/others/traindata") num=len(trainfile) #长度1024(列),每一行存储一个文件 #用一个数组存储所有训练数据,行:文件总数,列:1024 trainarr=zeros((num,1024)) for i in range(0,num): thisfname=trainfile[i] thislabel=seplabel(thisfname) labels.append(thislabel) trainarr[i,:]=datatoarray("traindata/"+thisfname) return trainarr,labels #用测试数据调用KNN算法去测试,看是否能够准确识别 def datatest(): trainarr,labels=traindata() testlist=listdir("testdata") tnum=len(testlist) for i in range(0,tnum): thistestfile=testlist[i] testarr=datatoarray("testdata/"+thistestfile) rknn=knn(3,testarr,trainarr,labels) print(rknn) #datatest() #抽某一个测试文件出来进行试验 trainarr,labels=traindata() thistestfile="33.txt" testarr=datatoarray("C:/Users/Administrator/Desktop/"+thistestfile) rknn=knn(3,testarr,trainarr,labels) print(rknn) ******************************************************************************************
from PIL import Image im=Image.open("C:/Users/Administrator/Desktop/33.png") #im.save("C:/Users/Administrator/Desktop/33.bmp") fh=open("C:/Users/Administrator/Desktop/33.txt","a") pic_size=im.size width=im.size[0] height=im.size[1] print(pic_size) print(" width:"+str(width)) print (" height:"+str(height)) px_color=im.getpixel((0,9)) print(px_color) from os import listdir listdir_name=listdir("E:/m/others/traindata")#得到文件夹下的所有文件名 print(listdir_name) from numpy import * zero=zeros((2,5)) print (zero)