我们能读懂图像中的文字并用TensorFlow打印吗?

2024-04-23 07:34:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我想读取一个图像(png)并打印该图像中的文本。我需要使用Python在张量流中执行此操作。我在tf库中做了很多搜索,尝试了很多方法。但是我可以打印张量数组或者形状。如何将张量数组转换为与图像内容匹配的可读文本。我在这里附上样品图片。在

我可以创造测试.txt但那里面有所有的垃圾字符(像中文/日语字符的显示)。我的目标是读取图像并在控制台或文本文件中打印图像“ABCDEFGHIJKLMNOPQRSTUVWXYZ”的内容。在

import tensorflow as tf
import numpy as np
import PIL.Image 

filename_queue = tf.train.string_input_producer(['Test.png'])
reader = tf.FixedLengthRecordReader(24300)
key, value = reader.read(filename_queue)
record_bytes = tf.decode_raw(value, tf.uint8
depth_major = tf.reshape(tf.slice(record_bytes, [0], [24300]),[3, 90, 90])
result_uint8image = tf.transpose(depth_major, [1, 2, 0])
init_op = tf.initialize_all_variables()
with tf.Session() as sess:
   sess.run(init_op)

   coord = tf.train.Coordinator()
   threads = tf.train.start_queue_runners(coord=coord)

   result = sess.run(result_uint8image)
   image=record_bytes.eval()
   a=np.asarray(image)
   a.tofile('Test.txt')
coord.request_stop()
coord.join(threads)

English Text


Tags: 图像文本import内容bytespngqueuetf