(self, img_crop)
return words, prob
attention ocr 做文字识别
        img_crop:
            cv2图片，rgb顺序
        返回：
            ocr读数，prob置信度
attention ocr 做文字识别
        img_crop:
            cv2图片，rgb顺序
        返回：
            ocr读数，prob置信度
attention ocr 做文字识别 img_crop : cv2图片，rgb顺序 返回： ocr读数，prob置信度
def predict(self, img_crop):
        '''attention ocr 做文字识别
        img_crop:
            cv2图片，rgb顺序
        返回：
            ocr读数，prob置信度
        '''
        img_tensor = self.constant_pad(img_crop)
        encoder_out = self.encoder(img_tensor)

        decoded_words = []
        prob = 1.0
        decoder_input = torch.zeros(1).long()      # 初始化decoder的开始,从0开始输出
        decoder_hidden = self.decoder.initHidden(1)
        if torch.cuda.is_available() and self.use_gpu:
            decoder_input = decoder_input.cuda()
            decoder_hidden = decoder_hidden.cuda()
        # 预测的时候采用非强制策略，将前一次的输出，作为下一次的输入，直到标签为EOS_TOKEN时停止
        for di in range(self.max_length):  # 最大字符串的长度
            decoder_output, decoder_hidden, decoder_attention = self.decoder(
                decoder_input, decoder_hidden, encoder_out)
            probs = torch.exp(decoder_output)
            # decoder_attentions[di] = decoder_attention.data
            topv, topi = decoder_output.data.topk(1)
            ni = topi.squeeze(1)
            decoder_input = ni
            prob *= probs[:, ni]
            if ni == self.EOS_TOKEN:
                # decoded_words.append('<EOS>')
                break
            else:
                decoded_words.append(self.converter.decode(ni))

        words = ''.join(decoded_words)
        prob = prob.item()

        return words, prob
def predict ( self , img_crop ) : img_tensor = self . constant_pad ( img_crop ) encoder_out = self . encoder ( img_tensor ) decoded_words = [ ] prob = 1.0 decoder_input = torch . zeros ( 1 ) . long ( ) # 初始化decoder的开始,从0开始输出 decoder_hidden = self . decoder . initHidden ( 1 ) if torch . cuda . is_available ( ) and self . use_gpu : decoder_input = decoder_input . cuda ( ) decoder_hidden = decoder_hidden . cuda ( ) # 预测的时候采用非强制策略，将前一次的输出，作为下一次的输入，直到标签为EOS_TOKEN时停止 for di in range ( self . max_length ) : # 最大字符串的长度 decoder_output , decoder_hidden , decoder_attention = self . decoder ( decoder_input , decoder_hidden , encoder_out ) probs = torch . exp ( decoder_output ) # decoder_attentions[di] = decoder_attention.data topv , topi = decoder_output . data . topk ( 1 ) ni = topi . squeeze ( 1 ) decoder_input = ni prob *= probs [ : , ni ] if ni == self . EOS_TOKEN : # decoded_words.append('<EOS>') break else : decoded_words . append ( self . converter . decode ( ni ) ) words = '' . join ( decoded_words ) prob = prob . item ( ) return words , prob