# -*- coding: utf-8 -*-
from scipy.misc import imsave
import numpy as np
import cPickle
import os
from mysql import connector
# # 定义将图片路径写入数据库的函数
#
# def write_img_path(img_path, img_label):
# insertsql = 'INSERT INTO `test_cifar_img` (`img_path`,`img_label`) VALUES (%s,%s)'
# insertvalues = [(img_path, img_label)]
#
# cnx = connector.connect(host="", user="", password="", database="",
# charset="utf8", unix_socket='/tmp/mysql.sock')
#
# db0 = cnx.cursor()
# db0.executemany(insertsql, insertvalues)
# cnx.commit()
# db0.close()
# 该函数是将cifar数据文件转换为字典
def unpickle(file_path):
cifar_date_file = open(file_path, 'rb')
cifar_dict = cPickle.load(cifar_date_file)
cifar_date_file.close()
return cifar_dict
print 'into main function'
# 拼接cifar文件路径
cifar_date_path = 'contact/contact'
# 获取cifar相应的数据字典
cifar_date_dict = unpickle(cifar_date_path)
print len(cifar_date_dict['data'])
# 将字典中的图片数据取出来转换为图片
for i in range(0, 10000):
# 这里是因为LMDB数据格式规定必须这么解析
single_img = np.reshape(cifar_date_dict['data'][i], (3, 32, 32))
single_std_img = single_img.transpose(1, 2, 0)
single_std_img_path = 'test/' + str(cifar_date_dict['labels'][i]) + '/' + str(i) + '.png'
dirpath = 'test/' + str(cifar_date_dict['labels'][i])
if not os.path.exists(dirpath):
os.makedirs(dirpath)
imsave(single_std_img_path, single_std_img)
# 这里我们将图片的路径写入数据库
# write_img_path(single_std_img_path, cifar_date_dict['labels'][i])
print 'cifar 图片解析完成'
LMDB数据生成与解析
最新推荐文章于 2023-11-11 14:32:19 发布