官网地址下载链接:http://www.nlpr.ia.ac.cn/databases/handwriting/Download.html
血泪的教训史:不要把文件夹存放在目录为/var/tmp或者/tmp下,默认情况下,Linux系统每个一段时间会自动清理var/tmp或者/tmp文件的,第二天开机发现辛辛苦苦下载的数据集不见了,又重新下载了一次。
安装unzip命令解压.zip文件,打开终端输入命令:
sudo apt install unzip
安装unalz命令解压.alz文件,打开终端输入命令:
sudo apt install unalz
官网地址下载链接:
http://www.nlpr.ia.ac.cn/databases/download/feature_data/HWDB1.1trn_gnt.zip
http://www.nlpr.ia.ac.cn/databases/download/feature_data/HWDB1.1tst_gnt.zip
找到文件存放目录,解锁权限,打开终端输入命令:
解压文件,打开终端输入命令:
解压号的文件应该都为.gnt文件,放入自己建立好的对应文件夹:
将.gnt文件转化为.png格式,打开终端输入spyder,进入搭建好的python环境,运行代码:
import os
import numpy as np
import struct
from PIL import Image
# data文件夹存放转换后的.png文件
data_dir = './data'
# 路径为存放数据集解压后的.gnt文件
train_data_dir = os.path.join(data_dir, '/home/admina/下载/OCR数据集/HWDB1.1trn_gnt')
test_data_dir = os.path.join(data_dir, '/home/admina/下载/OCR数据集/HWDB1.1tst_gnt')
def read_from_gnt_dir(gnt_dir=train_data_dir):
def one_file(f):
header_size = 10
while True:
header = np.fromfile(f, dtype='uint8', count=header_size)
if not header.size: break
sample_size = header[0] + (header[1] << 8) + (header[2] << 16) + (header[3] << 24)
tagcode = header[5] + (header[4] << 8)
width = header[6] + (header[7] << 8)
height = header[8] + (header[9] << 8)
if header_size + width * height != sample_size:
break
image = np.fromfile(f, dtype='uint8', count=width * height).reshape((height, width))
yield image, tagcode
for file_name in os.listdir(gnt_dir):
if file_name.endswith('.gnt'):
file_path = os.path.join(gnt_dir, file_name)
with open(file_path, 'rb') as f:
for image, tagcode in one_file(f):
yield image, tagcode
char_set = set()
for _, tagcode in read_from_gnt_dir(gnt_dir=train_data_dir):
tagcode_unicode = struct.pack('>H', tagcode).decode('gb2312')
char_set.add(tagcode_unicode)
char_list = list(char_set)
char_dict = dict(zip(sorted(char_list), range(len(char_list))))
print(len(char_dict))
print("char_dict=", char_dict)
import pickle
f = open('char_dict', 'wb')
pickle.dump(char_dict, f)
f.close()
train_counter = 0
test_counter = 0
for image, tagcode in read_from_gnt_dir(gnt_dir=train_data_dir):
tagcode_unicode = struct.pack('>H', tagcode).decode('gb2312')
im = Image.fromarray(image)
# 路径为data文件夹下的子文件夹,train为存放训练集.png的文件夹
dir_name = '/home/admina/下载/OCR数据集/HWDB1.1trn_gnt_train' + '%0.5d' % char_dict[tagcode_unicode]
if not os.path.exists(dir_name):
os.mkdir(dir_name)
im.convert('RGB').save(dir_name + '/' + str(train_counter) + '.png')
print("train_counter=", train_counter)
train_counter += 1
for image, tagcode in read_from_gnt_dir(gnt_dir=test_data_dir):
tagcode_unicode = struct.pack('>H', tagcode).decode('gb2312')
im = Image.fromarray(image)
# 路径为data文件夹下的子文件夹,test为存放测试集.png的文件夹
dir_name = '/home/admina/下载/OCR数据集/HWDB1.1tst_gnt_test' + '%0.5d' % char_dict[tagcode_unicode]
if not os.path.exists(dir_name):
os.mkdir(dir_name)
im.convert('RGB').save(dir_name + '/' + str(test_counter) + '.png')
print("test_counter=", test_counter)
test_counter += 1
需要改路径的地方有四处,前两处为读取.gnt的路径,后面为存储.png的路径。
最后可以得到.png图片: