数据集准备
首先有一个存放数据的文件夹,然后在这个文件夹存放你的image文件夹(里面是你的文字图片),还有一个train.txt文本文件,里面存放你的图片名称 和图片内容(比如:1012859 Mirceas)图片名称这里省略了.jpg,再新建一个文件夹lmdb_train,用来程序运行结果的lmdb格式的数据集。
环境准备
这里你需要在你的环境下安装opencv-python,numpy,pillow,lmdb第三方库,用于跑通代码。这里这个代码是引用ocr数据集(图片,txt)转lmdb | 码农家园 (codenong.com)
import os
import lmdb # install lmdb by "pip install lmdb"
import cv2
import re
from PIL import Image
import numpy as np
import imghdr
def checkImageIsValid(imageBin):
if imageBin is None:
return False
try:
imageBuf = np.fromstring(imageBin, dtype=np.uint8)
img = cv2.imdecode(imageBuf, cv2.IMREAD_GRAYSCALE)
imgH, imgW = img.shape[0], img.shape[1]
except:
return False
else:
if imgH * imgW == 0:
return False
return True
def writeCache(env, cache):
with env.begin(write=True) as txn:
for k, v in cache.items():
if type(k) == str:
k = k.encode()
if type(v) == str:
v = v.encode()
txn.put(k, v)
def createDataset(outputPath, imagePathList, labelList, lexiconList=None, checkValid=True):
assert(len(imagePathList) == len(labelList))
nSamples = len(imagePathList)
env = lmdb.open(outputPath, map_size=10995116277760)
cache = {}
cnt = 1
for i in range(nSamples):
imagePath = ''.join(imagePathList[i]).split()[0].replace('\n','').replace('\r\n','')
label = ''.join(labelList[i])
print(label)
####这两句就完成了对你图片地址的绝对引用,比如 1012859 Mirceas
####file_path='/home/....../image/1012859.jpg'
image_directory='/home/........../image'#####这里是你存放图片的文件夹的绝对地址
file_path=os.path.join(image_directory, imagePath+'.jpg')
with open(file_path, 'rb') as f:
imageBin = f.read()
if checkValid:
if not checkImageIsValid(imageBin):
print('%s is not a valid image' % imagePath)
continue
imageKey = 'image-%09d' % cnt
labelKey = 'label-%09d' % cnt
cache[imageKey] = imageBin
cache[labelKey] = label
if lexiconList:
lexiconKey = 'lexicon-%09d' % cnt
cache[lexiconKey] = ' '.join(lexiconList[i])
if cnt % 1000 == 0:
writeCache(env, cache)
cache = {}
print('Written %d / %d' % (cnt, nSamples))
cnt += 1
print(cnt)
nSamples = cnt-1
cache['num-samples'] = str(nSamples)
writeCache(env, cache)
print('Created dataset with %d samples' % nSamples)
if __name__ == '__main__':
outputPath = "./lmdb_train"#####这里是输出lmdb格式的存放文件
imgdata = open("./train.txt")#####这里是你的train.txt文件
imagePathList = list(imgdata)
labelList = []
for line in imagePathList:
word = line.split()[1]
labelList.append(word)
createDataset(outputPath, imagePathList, labelList)
输出结果
运行代码后,就会输出lmdb格式的数据集到lmdb_train中,结果如下图:
仅记录我最近遇到的问题,希望能对别人有所帮助,如有冒犯,请与我联系~