ETL文字データベース,「研究用途に限り」とか書いてあって,使用目的を書かされるので,敷居が高かったが,やってみたら自動応答でダウンロードURLとパスワードが送られてきた。
読み方は次の記事参照:
ここでは zipfile ライブラリを使って ETL1.zip だけ読み込む:
from zipfile import ZipFile
import struct
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
RECORD_SIZE = 2052
etl = []
with ZipFile('ETL1.zip') as etl1:
names = [n for n in etl1.namelist() if '_' in n]
for x in names:
with etl1.open(x) as f:
while True:
s = f.read(RECORD_SIZE)
if s is None or len(s) < RECORD_SIZE:
break
r = struct.unpack(">H2sHBBBBBBIHHHHBBBBHH2016s4x", s)
img = Image.frombytes('F', (64, 63), r[20], 'bit', (4, 0))
img = np.array(img.convert('L')) # 0..15
lbl = r[3] # 文字コード
etl.append((img, lbl))
ラベルは JIS X 0201 文字コードである。画像はモノクロで 0〜15 の値を持つ。数字の部分をランダムに表示させた:
rng = np.random.default_rng()
lst = [i for i in range(len(etl)) if etl[i][1] in range(48,58)] # 数字だけ
perm = rng.permutation(lst)
for k in range(24):
plt.subplot(3, 8, k+1)
plt.xticks([])
plt.yticks([])
img, lbl = etl[perm[k]]
plt.imshow(img, cmap='gray_r')
plt.title(f'{lbl:02X}')
plt.savefig("etlcdb.png", bbox_inches="tight")

数字以外にもたくさん文字が入っている:
perm = rng.permutation(len(etl))

背景が邪魔かもしれない。例えば plt.imshow(img >= 4) にすれば2値画像になる。
Last modified: