当前的Keras Captcha OCR 模型返回 CTC 编码输出,需要推理后解码。
要对其进行解码,需要在推理之后作为单独的步骤运行解码实用函数。
preds = prediction_model.predict(batch_images)
pred_texts = decode_batch_predictions(preds)
Run Code Online (Sandbox Code Playgroud)
解码的效用函数使用keras.backend.ctc_decode,而 又使用贪婪解码器或波束搜索解码器。
# A utility function to decode the output of the network
def decode_batch_predictions(pred):
input_len = np.ones(pred.shape[0]) * pred.shape[1]
# Use greedy search. For complex tasks, you can use beam search
results = keras.backend.ctc_decode(pred, input_length=input_len, greedy=True)[0][0][
:, :max_length
]
# Iterate over the results and get back the text
output_text = []
for res in results:
res = tf.strings.reduce_join(num_to_char(res)).numpy().decode("utf-8")
output_text.append(res)
return output_text …Run Code Online (Sandbox Code Playgroud) 以下Python脚本: re.sub("[^a-zA-Z]pi[^a-zA-Z]", "(math.pi)", "2pi3 + supirse")
结果是: '(math.pi) + supirse'
虽然之前和之后的非alpha匹配pi很关键,但我不希望在匹配中替换这些非alpha字符.我想看到以下输出:'2(math.pi)3 + supirse'
注意:先前的建议如下: re.sub("\Bpi\B", "(math.pi)", "2pi3 + supirse")
导致完全替换每个实例:'2(math.pi)3 + su(math.pi)rse'这也不是我想要的