原文地址:https://blog.csdn.net/zhang43211234/article/details/81172154
代码如下
# -*- coding: utf-8 -*-
import numpy as np
from PIL import Image
import operator
from os import listdir
import sys
import cPickle as pickle
import random
data={}
list1=[]
list2=[]
list3=[]
def img_tra():
for k in range(0,num):
currentpath=folder+"/"+imglist[k]
im=Image.open(currentpath)
#width=im.size[0]
#height=im.size[1]
x_s=32
y_s=32
out = im.resize((x_s,y_s),Image.ANTIALIAS)
out.save(folder_ad+"/"+str(imglist[k]))
def addWord(theIndex,word,adder):
theIndex.setdefault(word,[]).append(adder)
def seplabel(fname):
filestr=fname.split(".")[0]
label=int(filestr.split("_")[0])
return label
def mkcf():
global data
global list1
global list2
global list3
for k in range(0,num):
currentpath=folder_ad+"/"+imglist[k]
im=Image.open(currentpath)
with open(binpath, 'a') as f:
for i in range (0,32):
for j in range (0,32):
cl=im.getpixel((i,j))
list1.append(cl[0])
for i in range (0,32):
for j in range (0,32):
cl=im.getpixel((i,j))
#with open(binpath, 'a') as f:
#mid=str(cl[1])
#f.write(mid)
list1.append(cl[1])
for i in range (0,32):
for j in range (0,32):
cl=im.getpixel((i,j))
list1.append(cl[2])
list2.append(list1)
list1=[]
f.close()
print("image"+str(k+1)+"saved.")
list3.append(imglist[k].encode('utf-8'))
arr2=np.array(list2,dtype=np.uint8)
data['batch_label'.encode('utf-8')]='testing batch 1 of 1'.encode('utf-8')
data.setdefault('labels'.encode('utf-8'),label)
data.setdefault('data'.encode('utf-8'),arr2)
data.setdefault('filenames'.encode('utf-8'),list3)
output = open(binpath, 'wb')
pickle.dump(data, output)
output.close()
folder="image"
folder_ad="images"
imglist=listdir(folder_ad)
num=len(imglist)
img_tra()
label=[]
for i in range (0,num):
label.append(seplabel(imglist[i]))
binpath="test_batch"
print(binpath)
mkcf()
在生成数据集文件之后,查看文件
def unpickle(file):
import pickle
# with open(file, 'rb') as fo:
fo=open(file, 'rb')
dict = pickle.load(fo, encoding='iso-8859-1')
return dict
p = "test_batch"
cc=unpickle(p)
print(cc.keys())
filenames = cc.get(b'filenames')
print(filenames)
显示
为什么前面一直会有‘b’,该怎么去掉,或者改变什么类型吗?一直没明白