Esempio n. 1
0
def predict():
    if request.method == 'POST':
        # read image file string data
        if 'file' not in request.files:
            return redirect(url_for('home'))
        else:
            filestr = request.files['file'].read()
            # convert string data to numpy array
            npimg = np.fromstring(filestr, np.uint8)
            # convert numpy array to image
            img = cv2.imdecode(npimg, cv2.IMREAD_GRAYSCALE)
            ctx = ctx = mx.gpu(0) if mx.context.num_gpus() > 0 else mx.cpu()

            # Models
            paragraph_segmentation_net = SegmentationNetwork(ctx=ctx)
            paragraph_segmentation_net.cnn.load_parameters(paragraph_segmentation_model, ctx)

            word_segmentation_net = WordSegmentationNet(2, ctx=ctx)
            word_segmentation_net.load_parameters(word_segmentation_model, ctx)

            handwriting_line_recognition_net = HandwritingRecognitionNet(rnn_hidden_states=rnn_hidden_states,
                                                                         rnn_layers=rnn_layers,
                                                                         max_seq_len=max_seq_len,
                                                                         ctx=ctx)
            handwriting_line_recognition_net.load_parameters(recognition_model, ctx)

            MAX_IMAGE_SIZE_FORM = (1120, 800)

            img_arr = np.asarray(img)

            resized_image = paragraph_segmentation_transform(img_arr, image_size=MAX_IMAGE_SIZE_FORM)
            paragraph_bb = paragraph_segmentation_net(resized_image.as_in_context(ctx))
            paragraph_segmented_image = crop_handwriting_page(img_arr, paragraph_bb[0].asnumpy(),
                                                              image_size=segmented_paragraph_size)
            word_bb = predict_bounding_boxes(word_segmentation_net, paragraph_segmented_image, min_c, overlap_thres, topk,
                                             ctx)
            line_bbs = sort_bbs_line_by_line(word_bb)
            line_images = crop_line_images(paragraph_segmented_image, line_bbs)

            predicted_text = []

            for line_image in line_images:
                line_image = handwriting_recognition_transform(line_image, line_image_size)
                character_probabilities = handwriting_line_recognition_net(line_image.as_in_context(ctx))
                decoded_text = denoise_func(character_probabilities)
                predicted_text.append(decoded_text)
            text = ' '.join(predicted_text)
            print(text)
            translated = gc_translate(text)
            sentiment = gc_sentiment(text)
            print(translated)
            print(sentiment)
            text_dict = {"text": text,
                         "translated": translated['translatedText'],
                         "polarity": sentiment[0],
                         "magnitude": sentiment[1]}

            a = text_dict
            return render_template('result.html', prediction=a)
def get_ns(is_train):
    network = BiLSTMNetwork(rnn_hidden_states=512,
                            rnn_layers=2,
                            max_seq_len=160,
                            ctx=ctx)
    network.load_parameters("models/handwriting_line_sl_160_a_512_o_2.params",
                            ctx=ctx)

    def noise_source_transform(image, text):
        image = handwriting_recognition_transform(image)
        output = network(image)
        predict_probs = output.softmax().asnumpy()
        return predict_probs

    ns = Noisy_forms_dataset(noise_source_transform,
                             train=is_train,
                             name="OCR_noise2",
                             topK_decode=topK_decode)
    return ns
    
    
#%%
        
    
train_dataset = FontDataset()
batch_size = 64
train_data = gluon.data.DataLoader(train_dataset.transform_first(gluon.data.vision.transforms.ToTensor()), batch_size=batch_size, last_batch='rollover', shuffle=True, num_workers=8)

#%%

plt.imshow(train_dataset._get_img('m"u'), cmap='gray')

#%%

handwriting_line_recognition_net = HandwritingRecognitionNet(rnn_hidden_states=512,
                                                             rnn_layers=2, ctx=ctx, max_seq_len=160)
handwriting_line_recognition_net.load_parameters("models/handwriting_line8.params", ctx=ctx)

#%%


im = np.array(train_dataset._get_img('cow'))

plt.imshow(im)

#%%

line_image_size = (60, 800)
line_image = handwriting_recognition_transform(np.array(im).mean(axis=2), line_image_size)
line_character_prob = handwriting_line_recognition_net(line_image.as_in_context(ctx))