예제 #1
0
파일: main.py 프로젝트: tkerr97/PyScribe
    def runModel(self):
        self.text = ""
        letters, _ = get_letters(get_processed_image(self.image))
        for letter in letters:

            self.text += get_char(self.model.predict(letter).argmax())
        print(self.text)
예제 #2
0
def from_morse(morse_code: str) -> str:
    """Translate Morse Code to normal text."""
    text = []
    for morse_word in morse_code.split(' / '):
        codes = morse_word.split()
        for i, code in enumerate(codes):
            char = get_char(code)
            codes[i] = char
        text.append(''.join(codes))
    return ' '.join(text).upper()
예제 #3
0
파일: game.py 프로젝트: mtolkacz/tetris
    def start(self):

        while True:

            while not self.new_figure_needed:
                self.reset_move_state()

                while not self.correct_move:
                    self.print_info()
                    self.user_move = get_char()

                    if self.user_move == EXIT:
                        sys.exit()

                    try:
                        if self.user_move in MOVES:
                            self.correct_move = True

                            self.figure.set_fields(self.user_move)

                            try:
                                self.check_board()
                                self.prepare_next_move()
                            except NotValidMove as nvm:
                                self.message = nvm.message
                                self.check_other_moves()

                        self.get_board_state()

                    except ValidMoveNotExists:
                        self.set_no_valid_move_state()

            try:
                self.set_figure()
                self.check_board(new_figure=True)
                self.board.add(self.figure.fields)
                self.get_board_state()

            except ValidMoveNotExists:
                self.new_figure_needed = True
                self.message = None

            except GameOver as go:
                self.message = go.message
                self.board.add(self.figure.fields)
                self.exit()
예제 #4
0
    def insert_text(self, substring, from_undo=False):
        root = find_parent(self, TenFingersPlay)

        if check_char(get_char(root.original_text, root.initial_index),
                      substring) and not self.error:
            write_text(root, SUCCESS)
            if not self.is_forward:
                self.is_forward = True
            root.initial_index += 1
            self.error = False
        else:
            self.error = True
            write_text(root, ERROR)
        super(TenFingerInputText, self).insert_text(substring,
                                                    from_undo=from_undo)
        self.detect_correct_word()
        self.focus = True
예제 #5
0
# nsm
select_anchor = select_anchor[keep_index]
select_score = select_score[keep_index]
select_score = np.reshape(select_score, (select_score.shape[0], 1))
nmsbox = np.hstack((select_anchor, select_score))
keep = utils.nms(nmsbox, 0.3)
select_anchor = select_anchor[keep]
select_score = select_score[keep]

#text line
textConn = TextProposalConnectorOriented()
text = textConn.get_text_lines(select_anchor, select_score, [h, w])

################### recognition ##########################
chars = utils.get_char()
ctc_infer_model = ctc_model.create_densenet_ctc_infer_model(img_h=config.img_h,
                                                            nclass=len(chars))
ctc_infer_model.load_weights(ctc_weight_path)


def pred_ctc(model, gray_image):
    image = utils.resize(gray_image, height=config.img_h)

    image = image.astype(np.float32) / 255 - 0.5
    image = np.expand_dims(np.expand_dims(image, axis=-1),
                           axis=0)  # for example, (1, 32, 280, 1)

    pred = model.predict(image)
    # argmax = np.argmax(pred, axis=-1)[0]
    out = K.get_value(
예제 #6
0
if use_cuda:
    rnn = rnn.cuda()
    log_softmax = log_softmax.cuda()

# loss structure
LEARNING_RATE = .001
EPOCHS = 100000
criterion = nn.NLLLoss()
optimizer = optim.SGD(rnn.parameters(), lr=LEARNING_RATE)

for e in range(EPOCHS):

    # set up inputs
    onehot = char_to_input_tensor(">", env.vocab)
    hidden = target_embedded
    sentence = get_char(onehot=onehot, chars=env.chars)
    optimizer.zero_grad()
    loss = 0

    if use_cuda:
        onehot = onehot.cuda()
        hidden = hidden.cuda()

    # main training loop
    times_in_loop = 0
    for char in target:
        times_in_loop += 1

        # run network
        onehot, hidden = rnn(onehot, hidden)
        onehot = log_softmax(onehot)
예제 #7
0
    def _recog(lines, image):
        '''

        :param lines: text coordinates from detection model
        :param image: a numpy array image with shape (h, w, c=3)
        :return: recognized text
        '''
        def __pred_ctc(model, gray_image):
            image = utils.resize(gray_image, height=32)

            image = image.astype(np.float32) / 255 - 0.5
            image = np.expand_dims(np.expand_dims(image, axis=-1),
                                   axis=0)  # for example, (1, 32, 280, 1)

            pred = model.predict(image)
            out = K.get_value(
                K.ctc_decode(
                    pred,
                    input_length=np.ones(pred.shape[0]) * pred.shape[1],
                )[0][0])[0]
            out = u''.join(utils.id_to_char(chars)[i] for i in out)

            return out

        def __get_sorted_bboxes(bboxes, thresh_height=20):
            text_sorted = np.array(
                sorted([t[:-1] for t in bboxes], key=lambda x: x[1]))
            bboxes_lines = []
            i = 0
            while i < text_sorted.shape[0]:
                cur_box = text_sorted[i]
                if i + 1 >= text_sorted.shape[0]:
                    bboxes_lines.append([list(cur_box)])
                    break
                line = [list(cur_box)]
                cur_h = thresh_height  # cur_box[5] - cur_box[1]
                # next_box = text_sorted[i + 1]
                for next_box in text_sorted[i + 1:]:
                    if next_box[1] - cur_box[1] < cur_h:
                        line.append(list(next_box))
                line = sorted(line, key=lambda s: s[0])
                bboxes_lines.append(line)
                i += len(line)

            return bboxes_lines

        chars = utils.get_char()
        texts = []
        for line in __get_sorted_bboxes(lines):
            out_line_texts = []
            for i in line:
                i = [int(j) for j in i]
                pts = np.array([(i[k], i[k + 1]) for k in range(0, 7, 2)])
                warped_image = four_point_transform(image, pts)

                warped_image = cv2.cvtColor(warped_image, cv2.COLOR_BGR2GRAY)
                ctc_out = __pred_ctc(ctc_infer_model, warped_image)
                out_line_texts.append(ctc_out)
            out_line_texts = ' '.join(out_line_texts)
            # print(out_line_texts)

            texts.append(out_line_texts)

        return texts