def recognize(image_path: str, weights_path: str, config: GlobalConfig, is_vis=True): logger = LogFactory.get_logger() image = load_and_resize_image(image_path) inputdata = tf.placeholder(dtype=tf.float32, shape=[1, 32, 100, 3], name='input') net = CRNN(phase='Test', hidden_nums=256, seq_length=25, num_classes=37) with tf.variable_scope('shadow'): net_out = net.build(inputdata=inputdata) decodes, _ = tf.nn.ctc_beam_search_decoder(inputs=net_out, sequence_length=25 * np.ones(1), merge_repeated=False) decoder = TextFeatureIO() # config tf session sess_config = tf.ConfigProto() sess_config.gpu_options.per_process_gpu_memory_fraction = config.get_gpu_config( ).memory_fraction sess_config.gpu_options.allow_growth = config.get_gpu_config( ).is_tf_growth_allowed() # config tf saver saver = tf.train.Saver() sess = tf.Session(config=sess_config) with sess.as_default(): saver.restore(sess=sess, save_path=weights_path) preds = sess.run(decodes, feed_dict={inputdata: image}) preds = decoder.writer.sparse_tensor_to_str(preds[0]) logger.info('Predict image {:s} label {:s}'.format( ops.split(image_path)[1], preds[0])) if is_vis: plt.figure('CRNN Model Demo') plt.imshow( cv2.imread(image_path, cv2.IMREAD_COLOR)[:, :, (2, 1, 0)]) plt.show() sess.close()
def __init__(self, tfrecords_path: str, weights_path: str, config: GlobalConfig): self._log = LogFactory.get_logger() self._tfrecords_path = tfrecords_path self._weights_path = weights_path self._batch_size = config.get_test_config().batch_size self._merge_repeated = config.get_test_config().merge_repeated_chars self._gpu_config = config.get_gpu_config() self._decoder = TextFeatureIO().reader self._recognition_time = None