def index(): amongus_str, res = "", "" if request.method == "POST" and "amongus_str" in request.form: amongus_str = request.form.get("amongus_str") if len(amongus_str) > 10000: res["error"] = "input string is too long" else: res = infer(amongus_str) return render_template("inference.html", amongus_str=amongus_str, res=res)
def upload_image(): if request.method == "POST": if request.files: image = request.files["image"] image.save(os.path.join(app.config["IMAGE_UPLOADS"], 'test.png')) decoderType = DecoderType.BeamSearch model = Model(open('../data/charList.txt').read(), decoderType, mustRestore=True) text = infer(model, '../data/test.png') return make_response(jsonify(text), 200)
def recognize_button(): # submit photo for handwriting to be recognized in rec_word = infer(model, filename) rec_label.configure(text="Recognized text is: " + rec_word, font=("Times", 32)) if len(rec_word) > 10: rec_label.place_configure(x=60, y=325) elif len(rec_word) < 5: rec_label.place_configure(x=135, y=325) elif len(rec_word) < 5: rec_label.place_configure(x=115, y=325) else: rec_label.place_configure(x=90, y=325) afterRecognitionImageCanvas.place_configure(x=75, y=20) recognizeButton.place_forget() tryAgainRecognize.place_configure(x=300, y=400) recognitionImageCanvas.place_forget()
# -*- coding: utf-8 -*- """ Created on Mon Feb 3 01:05:28 2020 @author: Tanmay Thakur """ import cv2 import os, sys from DataLoader import DataLoader, Batch from Model import Model, DecoderType from SamplePreprocessor import preprocess from autocorrect import Speller from main import infer, FilePaths spell = Speller(lang="en") decoderType = DecoderType.BestPath loader = DataLoader(FilePaths.fnTrain, Model.batchSize, Model.imgSize, Model.maxTextLen) model = Model(loader.charList, decoderType) imgFiles = os.listdir('data_/') for i in imgFiles: images = os.listdir('out/%s' % i) for j in images: if (j != "summary.png"): infer(model, "out/" + i + "/" + j)
help='file name') args = parser.parse_args() print(args) checkpoint = torch.load(os.path.join(args.dir, args.name)) model, final, words, word2int, emb = checkpoint['model'], checkpoint[ 'final'], checkpoint['words'], checkpoint['word2int'], checkpoint[ 'emb'] print('Finish Loading') device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model.to(device) final.to(device) start_words, start_freq = calc_word_freq('./data/poems.txt') while True: start = input() try: if len(start) == 0: start = start_words[prob_sample(start_freq)] poems = infer(model, final, words, word2int, emb, hidden_size=model.hidden_size, start=start, n=20, num=5 if random.random() < 0.5 else 7) print(evaluate(poems)) except KeyError: print(u'此字在语料库中未出现过,请更换首字')