create_kanji_dataset.py 文件源码

python
阅读 27 收藏 0 点赞 0 评论 0

项目:MachineLearning 作者: timomernick 项目源码 文件源码
def rasterize_kanji(kanji, weight, output_file):
    kanji = kanji[0:4] # strip extra stuff like footnotes off kanji

    prop = fm.FontProperties(fname="ipam.ttc", size=70)

    plt.figure(figsize=(1, 1))
    plt.text(0, 0, kanji, ha='center', va='center', fontproperties=prop)
    plt.xlim(-0.4, 0.1)
    plt.ylim(-0.1, 0.1)
    plt.axis("off")

    #plt.savefig(output_file)
    #image = scipy.misc.imread(output_file, flatten=True)

    buf = io.BytesIO()
    plt.savefig(buf, format="png")
    buf.seek(0)
    image = PIL.Image.open(buf).convert(mode="L")
    buf.close()
    image = np.asarray(image, dtype=np.uint8)
    plt.close()

    image = scipy.misc.imresize(image, [kanji_height, kanji_width])
    image = skimage.img_as_float(image).astype(np.float32)

    image = 1.0 - image # make the background black and the text white

    if (weight == "bold"):
        erosion_size = 5
    elif (weight == "normal"):
        erosion_size = 3
    else:
        erosion_size = 0

    if (erosion_size > 0):
        kernel = np.ones((erosion_size, erosion_size), np.float32)
        image = cv2.dilate(image, kernel, iterations=1)
        scipy.misc.imsave(output_file, (1.0 - image))

    return image
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号