def do_hog(hog_type='xeryus', char_size=(72, 72), window_size=(80, 80),
        block_size=(2, 2), cell_size=(8, 8), nbins=9):
    # reset hog tree
    features = []
    labels = []
    testFeatures = []
    testLabels = []

    FEATURE_DIR.rmtree()
    FEATURE_DIR.mkdir()

    logging.debug("Creating test and training set")
    for label in IMG_DIR.listdir(filter=DIRS_NO_LINKS):
        for f in label.listdir(pattern='*.ppm'):
            img = cv2.imread(f)
            if hog_type == 'xeryus':
                hist = hog.hog_xeryus(img, char_size, window_size, block_size,
                    cell_size, nbins)
            else:
                hist = hog_alternative(img)
            features.append(hist[:,0])
            labels.append(str(label.name))
    logging.info("HOG feature size: %d", len(hist))

    np.save(FEATURE_DIR + 'hog', features)
    np.save(FEATURE_DIR + 'labels', labels)
Exemplo n.º 2
0
def do_hog(char_size=(32, 32), window_size=(80, 80), block_size=(2, 2), cell_size=(8, 8), nbins=9, train_split=0.7):
    # reset hog tree
    features = []
    labels = []
    test_features = []
    test_labels = []

    HOG_DIR.rmtree()
    HOG_DIR.mkdir()
    train_dir = HOG_DIR + "train/"
    test_dir = HOG_DIR + "test/"
    train_dir.mkdir()
    test_dir.mkdir()

    logging.debug("Creating test and training set")
    for label in IMG_DIR.listdir(filter=DIRS_NO_LINKS):
        for f in label.listdir(pattern="*.ppm"):
            img = cv2.imread(f)
            hist = hog.hog_xeryus(img, char_size, window_size, block_size, cell_size, nbins)
            # Split into training and test set
            if np.random.random_sample() <= train_split:
                features.append(hist[:, 0])
                labels.append(str(label.name))
            else:
                test_features.append(hist[:, 0])
                test_labels.append(str(label.name))
    logging.info("HOG feature size: %d", len(hist))

    np.save(HOG_DIR + "train/hog.npy", features)
    np.save(HOG_DIR + "train/labels.npy", labels)
    np.save(HOG_DIR + "test/hog.npy", test_features)
    np.save(HOG_DIR + "test/labels.npy", test_labels)
Exemplo n.º 3
0
def do_hog(char_size=(32, 32),
           window_size=(80, 80),
           block_size=(2, 2),
           cell_size=(8, 8),
           nbins=9,
           train_split=0.7):
    # reset hog tree
    features = []
    labels = []
    test_features = []
    test_labels = []

    HOG_DIR.rmtree()
    HOG_DIR.mkdir()
    train_dir = HOG_DIR + 'train/'
    test_dir = HOG_DIR + 'test/'
    train_dir.mkdir()
    test_dir.mkdir()

    logging.debug("Creating test and training set")
    for label in IMG_DIR.listdir(filter=DIRS_NO_LINKS):
        for f in label.listdir(pattern='*.ppm'):
            img = cv2.imread(f)
            hist = hog.hog_xeryus(img, char_size, window_size, block_size,
                                  cell_size, nbins)
            # Split into training and test set
            if np.random.random_sample() <= train_split:
                features.append(hist[:, 0])
                labels.append(str(label.name))
            else:
                test_features.append(hist[:, 0])
                test_labels.append(str(label.name))
    logging.info("HOG feature size: %d", len(hist))

    np.save(HOG_DIR + 'train/hog.npy', features)
    np.save(HOG_DIR + 'train/labels.npy', labels)
    np.save(HOG_DIR + 'test/hog.npy', test_features)
    np.save(HOG_DIR + 'test/labels.npy', test_labels)
Exemplo n.º 4
0
    def recursion(self, word_img, cuts, currentCut, lexicon, wordString,
                  probability, stateProbabilities, transProbabilities, classes,
                  hypotheses, savedPredictions, minCutWindow, maxCutWindow):
        if currentCut == len(cuts) - 1:
            if wordString in hypotheses:
                hypotheses[wordString] = max(hypotheses[wordString],
                                             probability)
            else:
                hypotheses[wordString] = probability
            return

        # Find the next cut
        start = currentCut
        for end in range(start + 1, len(cuts)):
            if not minCutWindow <= (cuts[end] - cuts[start]) < maxCutWindow:
                continue
            window = word_img[:, cuts[start]:cuts[end]]
            window = cut_letters.removeWhitelines(window)

            if not window is None:
                f = hog.hog_xeryus(window).reshape(1, -1)

                # First check if the selected combination has been predicted before
                if str(start) in savedPredictions and str(
                        end) in savedPredictions[str(start)]:
                    predictions = savedPredictions[str(start)][str(end)]
                else:  # Else calculate it and add to database
                    predictions = self.getPredictions(self.knn, f, classes)
                    savedPredictions[str(start)][str(end)] = predictions

                for prediction, prob in predictions.iteritems():
                    # Add the predicted character to the word
                    wordString = wordString + prediction
                    # Add a factor to overcome advantages for large windows
                    factor = end - start
                    # Add the prediction probability to the total probability
                    probability += factor * np.log10(prob)

                    # Add the state (position of character) probability to the total probability
                    if len(wordString) <= len(stateProbabilities):
                        if prediction in stateProbabilities[len(wordString) -
                                                            1]:
                            probability += factor * stateProbabilities[
                                len(wordString) - 1][prediction]
                        else:
                            continue
                            # probability -= 5
                    else:
                        continue
                        #probability -= 5

                    # Add the transition probability to the total probability
                    if len(wordString) > 1:
                        if wordString[-2] in transProbabilities:
                            if wordString[-1] in transProbabilities[
                                    wordString[-2]]:
                                probability += factor * transProbabilities[
                                    wordString[-2]][wordString[-1]]
                            else:
                                continue
                                # probability -= 5
                        else:
                            continue
                            # probability -= 5

                    # Set the correct cut index
                    currentCut = end
                    # Into recursion, and beyond!
                    self.recursion(word_img, cuts, currentCut, lexicon,
                                   wordString, probability, stateProbabilities,
                                   transProbabilities, classes, hypotheses,
                                   savedPredictions, minCutWindow,
                                   maxCutWindow)
Exemplo n.º 5
0
            word_img = img[rect['top']:rect['bottom'], rect['left']:rect['right']]
            word_img = cut_letters.removeWhitelines(word_img)
            if word_img is None:
                logger.warning('Word not good for classifying: {}'.format(text))
                continue

            # Get the cuts
            cuts = find_cuts(word_img)
            if cuts is None:
                logger.warning('No cuts found in word')
                continue

            cv2.imwrite(out_dir + 'original.png', word_img)
            cuts_img = cut_letters.showCuts(word_img.copy(), cuts)
            cv2.imwrite(out_dir + 'cuts.png', cuts_img)

            i = 0
            for start in range(len(cuts)):
                for end in range(start+1, len(cuts)):
                    if not (15 < cuts[end] - cuts[start] < 100):
                        continue # Skip too small/too large sections
                    segment = word_img[:,cuts[start]:cuts[end]]
                    f = hog_xeryus(segment)
                    letter = svm.predict(f[:,0])
                    print('{}: {}:{} {}'.format(i, cuts[start], cuts[end], letter[0]))
                    cv2.imwrite(out_dir + str(i) + '-' + letter[0] + '.png', segment)

                    i += 1

            sys.exit(0)
Exemplo n.º 6
0
    def recursion(self, word_img, cuts, currentCut, lexicon, wordString, probability, stateProbabilities, transProbabilities, classes, hypotheses, savedPredictions, minCutWindow, maxCutWindow):
        if currentCut == len(cuts)-1:
            if wordString in hypotheses:
                hypotheses[wordString] = max(hypotheses[wordString], probability)
            else:
                hypotheses[wordString] = probability
            return

        # Find the next cut
        start = currentCut
        for end in range(start+1, len(cuts)):
            if not minCutWindow <= (cuts[end] - cuts[start]) < maxCutWindow:
                continue
            window = word_img[:,cuts[start]:cuts[end]]
            window = cut_letters.removeWhitelines(window)

            if not window is None:
                f = hog.hog_xeryus(window).reshape(1, -1)

                # First check if the selected combination has been predicted before
                if str(start) in savedPredictions and str(end) in savedPredictions[str(start)]:
                    predictions = savedPredictions[str(start)][str(end)]
                else: # Else calculate it and add to database
                    predictions = self.getPredictions(self.knn, f, classes)
                    savedPredictions[str(start)][str(end)] = predictions

                for prediction, prob in predictions.iteritems():
                    # Add the predicted character to the word
                    wordString = wordString + prediction
                    # Add a factor to overcome advantages for large windows
                    factor = end-start
                    # Add the prediction probability to the total probability
                    probability += factor * np.log10(prob)

                    # Add the state (position of character) probability to the total probability
                    if len(wordString) <= len(stateProbabilities):
                        if prediction in stateProbabilities[len(wordString)-1]:
                            probability += factor * stateProbabilities[len(wordString)-1][prediction]
                        else:
                            continue
                            # probability -= 5
                    else:
                        continue
                        #probability -= 5

                    # Add the transition probability to the total probability
                    if len(wordString) > 1:
                        if wordString[-2] in transProbabilities:
                            if wordString[-1] in transProbabilities[wordString[-2]]:
                                probability += factor * transProbabilities[wordString[-2]][wordString[-1]]
                            else:
                                continue
                                # probability -= 5
                        else:
                            continue
                            # probability -= 5

                    # Set the correct cut index
                    currentCut = end
                    # Into recursion, and beyond!
                    self.recursion(
                        word_img, cuts, currentCut, lexicon, wordString,
                        probability, stateProbabilities, transProbabilities,
                        classes, hypotheses, savedPredictions, minCutWindow,
                        maxCutWindow
                    )