将图片转换为LMDB
import pickle
from PIL import Image
import numpy as np
import os
import matplotlib.image as plimg
from mysql import connector
DATA_ALL_PATH = []
DATA_ALL_LABEL = []
DATA_ALL_ARRAY = []
def readArtistnameFromMySql():
global DATA_ALL_PATH
global DATA_ALL_LABEL
conn = connector.connect(host="", user="", password="", database="",
charset="utf8", unix_socket='/tmp/mysql.sock')
cur = conn.cursor()
sql = "SELECT img_path, img_label FROM test_cifar_img"
cur.execute(sql)
result_set = cur.fetchall()
if result_set:
for row in result_set:
DATA_ALL_PATH.append(row[0].encode('utf8'))
DATA_ALL_LABEL.append(row[1])
else:
print '没有查到结果'
cur.close()
conn.close()
def read_file(filename):
im = Image.open(filename)
r, g, b = im.split()
r_arr = plimg.pil_to_array(r)
g_arr = plimg.pil_to_array(g)
b_arr = plimg.pil_to_array(b)
r_arr1 = r_arr.reshape(1024)
g_arr1 = g_arr.reshape(1024)
b_arr1 = b_arr.reshape(1024)
arr = np.concatenate((r_arr1, g_arr1, b_arr1))
return arr
print 'into main function'
readArtistnameFromMySql()
for data_path in DATA_ALL_PATH:
data_path = data_path.replace('.jpg', '.png')
data_arr = read_file(data_path)
DATA_ALL_ARRAY.append(data_arr)
contact = {'data': DATA_ALL_ARRAY, 'labels': DATA_ALL_LABEL}
f = open('contact/contact', 'wb')
pickle.dump(contact, f)
f.close()
print "存储完毕"