资源简介
用于将图片保存为mnist数据集格式,图片命名为‘\d*.jpg’,代码中txt文件为图片的数据标签,顺序与图片顺序一致
代码片段和文件信息
# -*- coding: utf8 -*-
#用于将图片保存为二进制文件,作为训练用的数据集,数据集分为三部分,train_setvalid_set
#和test_set,分别为300张78张78张,保存的图片为灰度模式,尺寸200*200
import cv2osretimerandomnumpy
import cPickle as pickle
time.clock()
#保存训练集
def train(trainnum_0trainnum_1labellistshufflelist):
train_list = []
train_list_x = []
train_list_y = []
for i in range(trainnum_0trainnum_1):
img = cv2.imread(‘%d.jpg‘%int(shufflelist[i])cv2.IMREAD_GRAYSCALE)
blur = cv2.GaussianBlur(img(33)0)#高斯滤波
img = cv2.resize(blur(100100))
retbinary = cv2.threshold(img0255cv2.THRESH_BINARY+cv2.THRESH_OTSU)
img = numpy.reshape(binary(1-1))[0]
train_list_x.append(img/255)
train_list_y.append(labellist[int(shufflelist[i])])
#train_list_y.append(labellist[int(shufflelist[i])])
train_list_x = numpy.asarray(train_list_xdtype=‘float32‘)
train_list_y = numpy.asarray(train_list_ydtype=‘int64‘)
train_list = (train_list_xtrain_list_y)
return train_list_x train_list_y
#保存验证集
def valid(validnum_0validnum_1labellistshufflelist):
valid_list = []
valid_list_x = []
valid_list_y = []
for i in range(validnum_0validnum_1):
img = cv2.imread(‘%d.jpg‘%int(shufflelist[i])cv2.IMREAD_GRAYSCALE)
blur = cv2.GaussianBlur(img(33)0)#高斯滤波
img = cv2.resize(blur(100100))
retbinary = cv2.threshold(img0255cv2.THRESH_BINARY+cv2.THRESH_OTSU)
img = numpy.reshape(binary(1-1))[0]
valid_list_x.append(img/255)
valid_list_y.append(labellist[int(shufflelist[i])])
#valid_list_y.append(labellist[int(shufflelist[i])])
valid_list_x = numpy.asarray(valid_list_xdtype=‘float32‘)
valid_list_y = numpy.asarray(valid_list_ydtype=‘int64‘)
valid_list = (valid_list_xvalid_list_y)
return valid_list_xvalid_list_y
#保存测试集
def test(testnum_0testnum_1labellistshufflelist):
test_list = []
test_list_x = []
test_list_y = []
for i in range(testnum_0testnum_1):
img =
- 上一篇:python+sqlite学生成绩管理
- 下一篇:基于深度学习的表情识别系统
评论
共有 条评论