Пример #1
0
class ToCharacters(object):
    segmenter = Segmenter()

    def __init__(self):
        '''
        constructor
        '''
        self.segmenter = Segmenter()

    def segment(self, image):
        'returns Number of available character segments in a line'
        return self.segmenter.segmentHorizontally(image)

    #returns the same but manipulated line Image
    def splitDika(self, image):
        segmentLine = ToLines()
        totalLines = segmentLine.segment(image)
        pixelCounter = PixelCounter()

        for lineNum in range(totalLines):
            lineImg = segmentLine.getLineSegment(lineNum)
            lineRowRange = segmentLine.getCoordinatesOfLineSegment(lineNum)
            countArr = pixelCounter.getCountArr_V(lineImg, 0)
            #print countArr
            min = pixelCounter.getMin(countArr)
            #dika width is min vertically and it is most occurred horizontally

            for x in pixelCounter.getOccurredPositionList(countArr, min):
                for height in range(lineRowRange[0],
                                    (lineRowRange[1] + lineRowRange[0]) / 2):
                    cv.Set2D(image, height, x, 255)
        return image
Пример #2
0
def learn(data, K, resolution, epoch, device, checkpoint=None):
    """
    Data is a tensor ith size (len(dataset), number of colours,resolution[0], resolution[1])
    """
    model = Segmenter(resolution, K, device)
    optimizer = optim.Adam(model.parameters(), lr=LR)
    if checkpoint != None:
        model.load_state_dict(checkpoint["model"])
        optimizer.load_state_dict(checkpoint["optimizer"])
    dataloader = torch.utils.data.DataLoader(data,
                                             BATCH,
                                             shuffle=True,
                                             drop_last=True)
    for i in tqdm(range(epoch), desc="Epochs"):
        for batch in tqdm(dataloader, desc="Batchs"):
            model.zero_grad()
            Uenc = model(batch, True)
            Nloss = Ncutsoft(Uenc, batch).to(device)
            print("Loss on this batch: {}".format(float(Nloss)))
            print("Accuracy on this batch : {}%".format(
                round(100 * (1 - float(Nloss) / K), 2)))
            Nloss.backward()
            optimizer.step()
            model.zero_grad()
            Udec = model(batch, False)[1]
            Jloss = Jrecons(Udec, batch).to(device)
            Jloss.backward()
            optimizer.step()
        torch.save(
            {
                "model": model.state_dict(),
                "optimizer": optimizer.state_dict()
            }, path_saved_models + '{}epoch'.format(i))
    return (model, optimizer)
Пример #3
0
class ToWords(object):
    segmenter=Segmenter()
    def __init__(self):
        '''
        constructor
        '''
        self.segmenter=Segmenter()
        
    def segment(self,image):
        transImg=cv.CreateImage((image.height,image.width), cv.IPL_DEPTH_8U, 1)    
        cv.Transpose(image, transImg)
        'returns Number of available line segments'
        return self.segmenter.segmentHorizontally(transImg)
    
    def getWordSegment(self,wordNumber):
        'returns line segment Image'
        image=self.segmenter.getSegment(wordNumber)    
        return image
    
    def getReadableWordSegment(self,wordNumber):
        'returns transposed form of line segment Image'
        image=self.segmenter.getSegment(wordNumber)
        transImg=cv.CreateImage((image.height,image.width), cv.IPL_DEPTH_8U, 1)    
        cv.Transpose(image, transImg)
        return transImg  
Пример #4
0
class ToWords(object):
    segmenter = Segmenter()

    def __init__(self):
        '''
        constructor
        '''
        self.segmenter = Segmenter()

    def segment(self, image):
        transImg = cv.CreateImage((image.height, image.width), cv.IPL_DEPTH_8U,
                                  1)
        cv.Transpose(image, transImg)
        'returns Number of available line segments'
        return self.segmenter.segmentHorizontally(transImg)

    def getWordSegment(self, wordNumber):
        'returns line segment Image'
        image = self.segmenter.getSegment(wordNumber)
        return image

    def getReadableWordSegment(self, wordNumber):
        'returns transposed form of line segment Image'
        image = self.segmenter.getSegment(wordNumber)
        transImg = cv.CreateImage((image.height, image.width), cv.IPL_DEPTH_8U,
                                  1)
        cv.Transpose(image, transImg)
        return transImg
 def init():
     """
     Initialize FeatureExtraction initializing Segmenter
     """
     print("Initializing FeatureExtractor...")
     FeatureExtractor.__feature_extractor_model = FeatureExtractor.__generate_model()
     Segmenter.init()
     print("Done.")
Пример #6
0
    def pitchTableForBuffer(self, pitchBuffer):
        filterer = Filterer(pitchBuffer, lowPassCutoffInHZ=settings.minimumPitchInHZ, highPassCutoffInHZ=settings.maximumPitchInHZ, gain=1)
        buf = filterer.process()

        segmenter = Segmenter(buf, windowWidth=2)
        pitchTable = sp.zeros(segmenter.numberOfSegments())

        for (buf, index) in segmenter.eachSegment():
            pitchTable[index] = PitchEstimator.pitchForPeriod(buf)

        return pitchTable
Пример #7
0
class ToCharacters(object):
    segmenter=Segmenter()
    def __init__(self):
        '''
        constructor
        '''
        self.segmenter=Segmenter()
    
    def segment(self,image):
        'returns Number of available character segments in a line'
        return self.segmenter.segmentHorizontally(image)
    
    #returns the same but manipulated line Image
    def splitDika(self,image):
        segmentLine=ToLines()
        totalLines=segmentLine.segment(image)
        pixelCounter = PixelCounter()

        for lineNum in range(totalLines):
            lineImg=segmentLine.getLineSegment(lineNum)
            lineRowRange=segmentLine.getCoordinatesOfLineSegment(lineNum)
            countArr=pixelCounter.getCountArr_V(lineImg, 0)
            #print countArr
            min=pixelCounter.getMin(countArr)
            #dika width is min vertically and it is most occurred horizontally
            
            for x in pixelCounter.getOccurredPositionList(countArr, min):
                for height in range(lineRowRange[0],(lineRowRange[1]+lineRowRange[0])/2):
                    cv.Set2D(image,height,x,255)
        return image 
Пример #8
0
    def POST(self):
        # Open a temporary file
        file = open("%s/in_%f.dat" % (directory, random()), 'wb')
        inputs = web.input(video={}, type={}, start={}, end={}, faces={})
        file.write(inputs['video'].value)

        # Flush the file
        file.flush()
        os.fsync(file.fileno())
        file.close()

        start = None
        end = None

        if len(inputs["start"]) > 0:
            start = int(inputs["start"])

        if len(inputs["end"]) > 0:
            end = int(inputs["end"])

        client = Client(file.name, int(inputs['type']), start=start, end=end)
        segmentNames = client.run(Segmenter(), inputs['faces'] == "on", "THEO",
                                  "ogg", False)

        return render.video(segments=segmentNames)
Пример #9
0
def testModel():
    'Test the model on a directory of character images'
    parser = argparse.ArgumentParser()
    parser.add_argument('--model',
                        help='select which model to use',
                        default="model")
    parser.add_argument('--test',
                        help="path to dir",
                        default="../../data/sample_data/IAM/")
    args = parser.parse_args()

    mf = ModelFactory(modelName=args.model)
    model = mf.load()

    CER = []
    ground_truth = []
    predicted = []
    labels = [
        "nominating", "any", "move", "meeting", "been", "texts", "ready", "one"
    ]
    ctr = 0

    imgFiles = []
    gt = []
    for root, _, files in os.walk(args.test):
        for file in files:
            # if file[-4:] != '.jpg':
            # 		continue
            imgFiles.append(os.path.join(root, file))

    imgFiles = sorted(imgFiles)
    print("Hello")

    for file in imgFiles:
        print(file)
        img = cv2.imread(file, cv2.IMREAD_GRAYSCALE)
        #print(img.shape)
        #charImg = mf.preprocess(img, invert=True)
        #print("Image processed")
        #prediction = mf.predictChar(model, charImg)
        segmenter = Segmenter()
        pred = mf.predictWord(model, segmenter, img, show=False)
        print(pred)
        print(file)
        gt = file.split('/')[-1]
        print(gt)
        gt = gt.split('.')[0]
        print(gt)
        gt = labels[ctr]
        print("labels", gt)
        ctr += 1
        CER.append(mf.getCER(pred, gt))
        ground_truth.append(gt)
        predicted.append(pred)

    for i in range(len(predicted)):
        print(ground_truth[i], predicted[i], CER[i])

    print(np.average(np.array(CER)))
Пример #10
0
    def gold_data(self, sentence):
        seg_sentence = SegSentence(sentence)
        fwd_bigrams, unigrams = self.sentence_sequence(seg_sentence)

        features = Segmenter.training_data(seg_sentence)

        return {
            'segSentence': seg_sentence,
            'fwd_bigrams': fwd_bigrams,
            'unigrams': unigrams,
            'features': features,
        }
Пример #11
0
class ToLines(object):
    segmenter=Segmenter()
    def __init__(self):
        '''
        constructor
        '''
        self.segmenter=Segmenter()
        
    def segment(self,image):
        'returns Number of available line segments'
        return self.segmenter.segmentHorizontally(image)
    
    def getLineSegment(self,lineNumber):
        'returns line segment Image'
        return self.segmenter.getSegment(lineNumber)
    
    def getCoordinatesOfLineSegment(self,lineNumber):
        return self.segmenter.getRowRange(lineNumber)
    
    def getPixelCountsOfLineSegment(self,lineNumber):
        #print self.segmenter.getPixelCountsOfSegment(lineNumber)
        return self.segmenter.getPixelCountsOfSegment(lineNumber)
Пример #12
0
    def __init__(self, buf):
        self.mainBuffer = buf
        self.pitchTable = None
        self.pitchBuffer = Buffer.copy(buf)

        if settings.preEmphasis:
            PreEmphasizer.processBuffer(buf)

        self.pitchTable = {}
        wrappedPitch = False
        if settings.overridePitch:
            wrappedPitch = settings.pitchValue
        else:
            self.pitchTable = self.pitchTableForBuffer(self.pitchBuffer)

        coefficients = sp.zeros(11)

        segmenter = Segmenter(buf=self.mainBuffer, windowWidth=settings.windowWidth)

        frames = []
        for (cur_buf, i) in segmenter.eachSegment():
            HammingWindow.processBuffer(cur_buf)
            coefficients = cur_buf.getCoefficientsFor()
            reflector = Reflector.translateCoefficients(coefficients, cur_buf.size)

            if wrappedPitch:
                pitch = int(wrappedPitch)
            else:
                pitch = self.pitchTable[i]

            frameData = FrameData(reflector, pitch, repeat=False)

            frames.append(frameData)

        if settings.includeExplicitStopFrame:
            frames.append(FrameData.stopFrame())

        self.frames = frames
    def extract_all_features(im):
        """
        Given an image (as np tensor) extract several features

        :param im: a np tensor 1x256x256x3
        :return: lbp, rgb histogram, ycbcr histogram, ycbcr statistics, rgb statistics, sift descriptors and neural features
        """
        im_copy = im.copy()
        segmented_im, mask = Segmenter.segment_image(im_copy)
        if np.sum(mask) == 0:
            raise FeatureExtractionException()
        rgb_hist = FeatureExtractor.extract_rgb_hist(segmented_im, mask)
        ycbcr_hist = FeatureExtractor.extract_ycbcr_hist(segmented_im, mask)
        neural = FeatureExtractor.extract_neural_features(im_copy)
        lbp = FeatureExtractor.compute_LBP_rgb(segmented_im, mask)
        ycbcr_statistics = FeatureExtractor.extract_statistics_ycbcr(segmented_im, mask)
        rgb_statistics = FeatureExtractor.extract_statistics_rgb(segmented_im, mask)
        sift_kp = FeatureExtractor.extract_sift_kp(segmented_im, mask)
        if sift_kp is None or len(sift_kp.shape) == 0:
            raise FeatureExtractionException()
        return lbp, rgb_hist, ycbcr_hist, ycbcr_statistics, rgb_statistics, sift_kp, neural
Пример #14
0
def predict(filepath, classifier):
    """
    Input: filepath to the image file (string)
    Output: MusicXML (string) and midi (base 64 string)
    """

    basename = osp.basename(filepath)
    filename, extension = osp.splitext(basename)

    cached_file = Path(osp.join("cached_stream", filename + '.pkl'))
    if cached_file.is_file():
        song_stream = converter.thaw(cached_file)
    else:

        symbols, seg = Segmenter.symbols_from_File(filepath, use_cache=True)
        y = classifier.predict_symbols(symbols, use_class_numbers=True)

        for i in range(len(y)):
            symbols[i].work_out_type(y[i])

        song = Song()
        song.add_symbols(symbols)
        song.parse_symbols()

        converter.freeze(song.stream,
                         fmt='pickle',
                         fp=osp.join(os.getcwd(), "cached_stream",
                                     filename + ".pkl"))
        song_stream = song.stream

    # get musicxml as a string
    out = musicxml.m21ToXml.GeneralObjectExporter(song_stream).parse()
    outStr = out.decode('utf-8')

    # save midi file, then convert into b64 format
    song_stream.write('midi', osp.join('resources', 'mid', filename + '.mid'))
    file = open(osp.join('resources', 'mid', filename + '.mid'), 'rb').read()
    b64 = base64.b64encode(file)

    return outStr.replace('\n', ''), b64
Пример #15
0
def main():
    sheet_path = './resources/sheets/training/'
    output_path = './resources/sheets/training/trimmed/'
    reject_parth = './resources/sheets/training/rejected/'
    for filename in os.listdir(sheet_path):
        if filename.endswith('.jpg') or filename.endswith(
                '.jepg') or filename.endswith('.png'):
            print(filename)
            segmenter = Segmenter(os.path.join(sheet_path, filename))
            staff_removed = segmenter.remove_staff_lines()
            boxed = segmenter.getSymbols(merge_overlap=True)
            cv2.imwrite(output_path + 'boxed{}.png'.format(filename[:-6]),
                        boxed)
            segmenter.saveSymbols(save_origonal=False,
                                  format=filename[:-6] + '_note{:03}.png',
                                  width=150,
                                  path=output_path,
                                  reject_path=reject_parth,
                                  dirty_times=0)
Пример #16
0
    if args.model is None:
        print('Must specify --model or (or --write-vocab) parameter.')
        print('    (Use -h or --help flag for full option list.)')
        sys.exit()

    if args.test is not None:
        from seg_sentence import SegSentence
        from network import Network
        from Segmenter import Segmenter

        test_sentence = SegSentence.load_sentence_file(args.test)
        print('Loaded test trees from {}'.format(args.test))
        network = Network.load(args.model)
        print('Loaded model from: {}'.format(args.model))
        accuracy = Segmenter.evaluate_corpus(test_sentence,fm,network)
        #Segmenter.write_predicted('data/predicted',test_sentence,fm,network)
        print('Accuracy: {}'.format(accuracy))

    elif args.train is not None:
        from network import Network
        if args.np_seed is not None:
            import numpy as np
            np.random.seed(args.np_seed)


        network = Network.train(
            corpus = fm,
            bigrams_dims = args.bigrams_dims,
            unigrams_dims = args.unigrams_dims,
            lstm_units = args.lstm_units,
from ImageProcessor import ImageProcessor
from Segmenter import Segmenter
import matplotlib.pyplot as plt
import numpy as np

path = "lena.bmp"

#Read and process image
IP = ImageProcessor()
img = IP.getImg(path)/256.
imgSize = IP.getSize(path)
imgSpace = IP.process(path)


#Segment image
S = Segmenter()
K = 300 #number of clusters
errorThreshold = 0.01
clusters = S.segment(K, imgSpace, imgSize, errorThreshold)

#Get back clusters into image shape
width, height = imgSize[0],imgSize[1]
clusters = np.flipud(clusters.reshape((width,height)))


#Plot image
#http://stackoverflow.com/questions/15822934/matplotlib-imshow-how-to-plot-points-instead-of-image
# Reduce the data by a factor of 4 (so that we can see the points)
im = img
# generate coordinates for the image. Note that the image is "top down", so the y coordinate goes from high to low.
ys, xs = np.mgrid[im.shape[0]:0:-1, 0:im.shape[1]]
Пример #18
0
from Segmenter import Segmenter
import json
import sys

# from gensim.corpora import Dictionary
from gensim.models.phrases import Phrases

seg = Segmenter()
# vocab = Dictionary()
phrases = Phrases()

text_path = sys.argv[1]

def get_data(text_path):

    for line in open(text_path, "r"):
        line = line.strip()

        if line:
            data = json.loads(line)

            yield data['abstract']

for ind, text in enumerate(get_data(text_path)):
    segments = seg(text, segment_len=1, segment_overlap=0)

    phrases.add_vocab(segments)
    # vocab.add_documents(segments, prune_at=2000000)

    if ind % 10000:
        print(f"\rProcessed:{ind}", end = "")
Пример #19
0
# this is a dummy file to run other scripts
# it contains examples on how to run the main Segmenter class
from lib.Helpers import load_text, load_file

from Segmenter import Segmenter
from Evaluation import Evaluation

# loading data
file_path = 'data/br-phono-train.txt'
#file_path = 'data/small.txt'
(text, word_freq, char_freq) = load_text(file_path)
segmenter = Segmenter(text=text,
                      char_freq=char_freq,
                      p=2,
                      alpha=20,
                      p_hash=0.5)
print "Start segmenting \n"
segm_text = segmenter.run(200)

filetext = load_file(file_path)

print "Start evaluating \n"
evaluation = Evaluation(filetext, segm_text)
P, R, F, BP, BR, BF, LP, LR, LF = evaluation.run()

print "Boundary evaluation: \n Precision: %.2f, Recall: %.2f, F-measure: %.2f \n" % (
    P, R, F)
print "Ambigious boundary evaluation: \n Precision: %.2f, Recall: %.2f, F-measure: %.2f \n" % (
    BP, BR, BF)
print "Lexicon evaluation: \n Precision: %.2f, Recall: %.2f, F-measure: %.2f \n" % (
    LP, LR, LF)
Пример #20
0
    def train(corpus, bigrams_dims, unigrams_dims, lstm_units, hidden_units,
              epochs, batch_size, train_data_file, dev_data_file,
              model_save_file, droprate, unk_params, alpha, beta):

        start_time = time.time()

        fm = corpus
        bigrams_size = corpus.total_bigrams()
        unigrams_size = corpus.total_unigrams()

        network = Network(
            bigrams_size=bigrams_size,
            unigrams_size=unigrams_size,
            bigrams_dims=bigrams_dims,
            unigrams_dims=unigrams_dims,
            lstm_units=lstm_units,
            hidden_units=hidden_units,
            label_size=fm.total_labels(),
            span_nums=fm.total_span_nums(),
            droprate=droprate,
        )

        network.init_params()

        print('Hidden units : {} ,per LSTM units : {}'.format(
            hidden_units,
            lstm_units,
        ))

        print('Embeddings: bigrams = {}, unigrams = {}'.format(
            (bigrams_size, bigrams_dims), (unigrams_size, unigrams_dims)))

        print('Dropout rate : {}'.format(droprate))
        print('Parameters initialized in [-0.01,0.01]')
        print('Random UNKing parameter z = {}'.format(unk_params))

        training_data = corpus.gold_data_from_file(train_data_file)
        num_batched = -(-len(training_data) // batch_size)
        print('Loaded {} training sentences ({} batches of size {})!'.format(
            len(training_data),
            num_batched,
            batch_size,
        ))

        parse_every = -(-num_batched // 4)

        dev_sentences = SegSentence.load_sentence_file(dev_data_file)
        print('Loaded {} validation sentences!'.format(len(dev_sentences)))

        best_acc = FScore()
        for epoch in xrange(1, epochs + 1):
            print('............ epoch {} ............'.format(epoch))

            total_cost = 0.0
            total_states = 0
            training_acc = FScore()

            np.random.shuffle(training_data)

            for b in xrange(num_batched):
                batch = training_data[(b * batch_size):(b + 1) * batch_size]

                explore = [
                    Segmenter.exploration(example,
                                          fm,
                                          network,
                                          alpha=alpha,
                                          beta=beta) for example in batch
                ]
                for (_, acc) in explore:
                    training_acc += acc

                batch = [example for (example, _) in explore]

                dynet.renew_cg()
                network.prep_params()

                errors = []
                for example in batch:
                    ## random UNKing ##
                    for (i, uni) in enumerate(example['unigrams']):
                        if uni <= 2:
                            continue

                        u_freq = fm.unigrams_freq_list[uni]
                        drop_prob = unk_params / (unk_params + u_freq)
                        r = np.random.random()
                        if r < drop_prob:
                            example['unigrams'][i] = 0

                    for (i, bi) in enumerate(example['fwd_bigrams']):
                        if bi <= 2:
                            continue

                        b_freq = fm.bigrams_freq_list[bi]
                        drop_prob = unk_params / (unk_params + b_freq)
                        r = np.random.random()
                        if r < drop_prob:
                            example['fwd_bigrams'][i] = 0

                    fwd, back = network.evaluate_recurrent(
                        example['fwd_bigrams'],
                        example['unigrams'],
                    )

                    for (left,
                         right), correct in example['label_data'].items():
                        # correct = example['label_data'][(left,right)]
                        scores = network.evaluate_labels(
                            fwd, back, left, right)

                        probs = dynet.softmax(scores)
                        loss = -dynet.log(dynet.pick(probs, correct))
                        errors.append(loss)
                    total_states += len(example['label_data'])

                batch_error = dynet.esum(errors)
                total_cost += batch_error.scalar_value()
                batch_error.backward()
                network.trainer.update()

                mean_cost = total_cost / total_states

                print(
                    '\rBatch {}  Mean Cost {:.4f}  [Train: {}]'.format(
                        b,
                        mean_cost,
                        training_acc,
                    ),
                    end='',
                )
                sys.stdout.flush()

                if ((b + 1) % parse_every) == 0 or b == (num_batched - 1):
                    dev_acc = Segmenter.evaluate_corpus(
                        dev_sentences,
                        fm,
                        network,
                    )
                    print(' [Val: {}]'.format(dev_acc))

                    if dev_acc.fscore() > best_acc.fscore():
                        best_acc = dev_acc
                        network.save(model_save_file)
                        print('    [saved model : {}]'.format(model_save_file))

            current_time = time.time()
            runmins = (current_time - start_time) / 60
            print(' Elapsed time: {:.2f}m'.format(runmins))

        return network
from ImageProcessor import ImageProcessor
from Segmenter import Segmenter
import matplotlib.pyplot as plt
import numpy as np

path = "lena.bmp"

#Read and process image
IP = ImageProcessor()
img = IP.getImg(path) / 256.
imgSize = IP.getSize(path)
imgSpace = IP.process(path)

#Segment image
S = Segmenter()
K = 300  #number of clusters
errorThreshold = 0.01
clusters = S.segment(K, imgSpace, imgSize, errorThreshold)

#Get back clusters into image shape
width, height = imgSize[0], imgSize[1]
clusters = np.flipud(clusters.reshape((width, height)))

#Plot image
#http://stackoverflow.com/questions/15822934/matplotlib-imshow-how-to-plot-points-instead-of-image
# Reduce the data by a factor of 4 (so that we can see the points)
im = img
# generate coordinates for the image. Note that the image is "top down", so the y coordinate goes from high to low.
ys, xs = np.mgrid[im.shape[0]:0:-1, 0:im.shape[1]]
# Scatter plots take 1d arrays of xs and ys, and the colour takes a 2d array,
Пример #22
0
 def label_symbols(self,
                   min_x_pos=500,
                   edit_last=False,
                   dont_use_cache=False):
     measures = self.xmldoc.getElementsByTagName('Measure')
     xml_objects = []
     while len(measures) is not 0:
         current_measure = measures.pop(0)
         current_measure_number = current_measure.getAttribute('number')
         for child in current_measure.childNodes:
             if child.localName is not None:
                 if child.nodeName == 'Clef':
                     xml_objects.append(('clef', current_measure_number))
                 elif child.nodeName == 'TimeSig':
                     xml_objects.append(('timeSig', current_measure_number))
                 elif child.nodeName == 'Rest':
                     xml_objects.append(('rest_{}'.format(
                         child.childNodes[1].childNodes[0].data),
                                         current_measure_number))
                 elif child.nodeName == 'Chord':
                     if child.childNodes[1].nodeName == 'BeamMode':
                         xml_objects.append(('tied_{}'.format(
                             child.childNodes[3].childNodes[0].data),
                                             current_measure_number))
                     elif child.childNodes[3].nodeName != 'Beam':
                         xml_objects.append(('{}'.format(
                             child.childNodes[1].childNodes[0].data),
                                             current_measure_number))
         xml_objects.append(('barline', current_measure_number))
     first_pic = True
     for filename in self.picture_filenames:
         segmenter = Segmenter.load_segmenter_from_file(filename)
         if (segmenter is None) or dont_use_cache:
             segmenter = Segmenter(filename)
             if not first_pic:
                 if edit_last or filename != self.picture_filenames[-1]:
                     segmenter.blockout_markings()
             segmenter.remove_staff_lines()
             segmenter.getSymbols(True)
             segmenter.save_to_file()
         symbols = segmenter.symbols
         i = -1
         saveCount = 0
         while len(symbols) != 0:
             i += 1
             saveCount = saveCount - 1 if saveCount > 0 else 0
             if not is_barline(
                     symbols[0]) and xml_objects[0][0] == 'barline':
                 saveCount = 0
                 print(
                     'error not enough xmls found at bar {} line {} in file {}'
                     .format(self.matched_pairs[-1][0][1],
                             symbols[0].line_num, filename[-20:-4]))
                 popped = self.matched_pairs.pop()
                 while popped[0][0] != 'barline':
                     popped = self.matched_pairs.pop()
                 self.matched_pairs.append(popped)
                 popped = symbols.pop(0)
                 while not is_barline(popped):
                     popped = symbols.pop(0)
                 symbols.insert(0, popped)
             if is_barline(symbols[0]) and xml_objects[0][0] != 'barline':
                 saveCount = 0
                 print(
                     'error too many xmls found at bar {} line {} in file {}'
                     .format(self.matched_pairs[-1][0][1],
                             symbols[0].line_num, filename[-20:-4]))
                 # save = True
                 popped = self.matched_pairs.pop()
                 while popped[0][0] != 'barline':
                     popped = self.matched_pairs.pop()
                 self.matched_pairs.append(popped)
                 popped = xml_objects.pop(0)
                 while popped[0] != 'barline':
                     popped = xml_objects.pop(0)
                 xml_objects.insert(0, popped)
             if symbols[0].x < min_x_pos and (not first_pic or i != 0):
                 self.matched_pairs.append((('clef', -1), symbols.pop(0)))
             else:
                 self.matched_pairs.append(
                     (xml_objects.pop(0), symbols.pop(0)))
             if saveCount > 0:
                 thumbnail_name = os.path.join(
                     self.output_path, '{}_{}_line{}__{}.png'.format(
                         filename[-13:-10], self.matched_pairs[-1][0][0],
                         self.matched_pairs[-1][1].line_num, i))
                 cv2.imwrite(thumbnail_name, self.matched_pairs[-1][1].im)
         first_pic = False
Пример #23
0
import numpy as np
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.metrics import roc_auc_score
import functools
import sys
import math
from Segmenter import Segmenter
from Tokenizer import Tokenizer
import TextNormalizer
import codecs

# ------------------------------------------------------------------------

df = pd.read_csv("../data/dftrain17.csv", encoding='utf-8')

segmenter = Segmenter()

#test_str = u'Сразу после возвращения Фрама Нансен стал главным специалистом по полярным исследованиям в мире, по выражению Р. Хантфорда — оракулом для всех исследователей полярных широт Севера и Юга [188]. Нансен консультировал бельгийского барона Адриена де Жерлаша, который планировал в 1898 году свою экспедицию в Антарктиду, одним из участников команды был Руаль Амундсен[189]. Известнейший исследователь Гренландии Кнуд Расмуссен сравнил посещение Нансена с посвящением в рыцари[190]. В то же время Нансен категорически отказался встречаться со своим соотечественником Карстеном Борхгревинком, сочтя его мошенником, хотя именно он совершил первую успешную зимовку на побережье Антарктиды[191]. В 1900 году в Норвегию приехал за консультациями Роберт Скотт со своим покровителем Клементом Маркхэмом — давним другом Нансена, готовившим британскую экспедицию в Антарктиду. Несмотря на то, что англичане практически проигнорировали все советы, Нансен и Скотт остались в хороших отношениях[192].'
#for l in segmenter.split(test_str):
#    print(l)

# -------------------------------------------------------------------------

wrt0 = codecs.open('../data/rows(y=0).txt', 'w', 'utf-8')
wrt1 = codecs.open('../data/rows(y=1).txt', 'w', 'utf-8')

for index, row in tqdm.tqdm(df.iterrows(), total=df.shape[0]):
    question = row.question
    paragraph = row.paragraph

    wrt = wrt0 if row['target'] == 0 else wrt1
Пример #24
0
    if opts.frameLengths is None or opts.frameLengths == []:
        opts.frameLengths = unspecifiedFrameLengths

    for vid in opts.videos:
        print("*** Video : ", vid)

        for length in opts.frameLengths:
            try:
                print("   --- Length : " + str(opts.start) + " -> " +
                      str(opts.start + length))
                client = Client(vid,
                                SplitType.ON_FACE_CLUSTERS,
                                start=opts.start,
                                end=opts.start + length)
                t1 = timeit(lambda: client.run(Segmenter(),
                                               True,
                                               "THEO",
                                               "OGG",
                                               False,
                                               options={
                                                   "verbose": 0,
                                                   "write": False
                                               }),
                            number=1)
                t2 = timeit(lambda: client.run(Segmenter(),
                                               True,
                                               "THEO",
                                               "OGG",
                                               False,
                                               options={
Пример #25
0
 def __init__(self):
     '''
     constructor
     '''
     self.segmenter=Segmenter()
Пример #26
0
from Dataset import Dataset
from Segmenter import Segmenter
from BlobAnalyzer import BlobAnalyzer

# reading dataset
dataset = Dataset("./immagini/*.BMP")

segmenter = Segmenter()
analyzer = BlobAnalyzer()

# set true: show the computation of all the process
show = False

for image in dataset.images:
    binary, labels, n_labels = segmenter.run(image, show=show)
    stats = analyzer.run(binary, labels, n_labels, show=show)
    analyzer.show(image, stats)
'''
Created on Nov 16, 2014
@author: Etienne
'''


from ImageProcessor import ImageProcessor
from Segmenter import Segmenter
import matplotlib.pyplot as plt

IP = ImageProcessor()
imgSize = IP.getSize("5.jpg")
imgSpace = IP.process("5.jpg")
# print imgSpace[:766,3:5]
S = Segmenter()
K = 108


#Test pick centers
#Should give S points evenly spaced across the space
gridInterval = S.calcGridInterval(imgSpace.shape, K)
centers = S.pickCenters(K, gridInterval, imgSpace, imgSize)
print centers.shape


# print centers
X0 = centers[:,3]
Y0 = centers[:,4]
# print X,Y
plt.plot(X0,Y0,"rx")
Пример #28
0
class Modifiers:
    segmenter = Segmenter()
    pixelCounter = PixelCounter()

    def __init__(self):
        'Constructor'

    #returns horizontal starting row and ending row of dika
    def locateDika(self, lineImg):
        countArr_V = self.pixelCounter.getCountArr_V(lineImg, 0)
        countArr_H = self.pixelCounter.getCountArr_H(lineImg, 0)
        dikaWidth = self.pixelCounter.getMin(countArr_V)

        max = self.pixelCounter.getMax(countArr_H)
        #print max
        maxAt = self.pixelCounter.getOccurredPositionList(countArr_H, max)
        #print maxAt

        return maxAt

        #get horizontally occurred pixel count
        #+1 or -1 or set some threshold
        # OR
        # get horizontally most occurred pixel count => say 'x'
        # find width of dika
        # ?? is this really needed?? position from x-widthDika to x+widthDika can be considered to be dika??
        # or may be count the pixels in that row and decide according to certain threshold

        ''

    def extractTop(self, lineImg):
        ''
        dikaList = self.locateDika(lineImg)

        rect = CvRect
        rect.x = 0
        rect.y = 0
        rect.width = lineImg.width
        rect.height = dikaList[
            0] - 2  # -2 is to eliminate disturbances from bha tha dha etc

        segmentImg = cv.CreateImage((rect.width, rect.height), cv.IPL_DEPTH_8U,
                                    1)
        cv.SetImageROI(lineImg, (rect.x, rect.y, rect.width, rect.height))
        cv.Resize(lineImg, segmentImg)
        cv.ResetImageROI(lineImg)

        cv.ShowImage("TOP", segmentImg)

        # locate horizontal dika position
        # set roi upper than dika
        # extract the roi

    def extractMainElement(self, lineImg):
        ''
        #strip top
        #strip bottom
        #set roi to remaining
        #extract roi
        #OR
        #using height of purnabiram extract all letters of that height

    def extractBottom(self, lineImg):
        ''
Пример #29
0
dftrain = pd.read_csv("../data/dftrain.csv", encoding='utf-8')
dftest = pd.read_csv("../data/dftest.csv", encoding='utf-8')

known_words = set()

# русский лексикон
with codecs.open('../data/word2lemma.dat', 'r', 'utf-8') as rdr:
    for line in rdr:
        tx = line.strip().split(u'\t')
        if len(tx) == 3:
            word = tx[0].lower()
            known_words.add(word)

# ------------------------------------------------------------------------

segmenter = Segmenter()


def is_oov(word):
    return TextNormalizer.normalize_word(
        word) not in known_words and len(word) > 0 and not word[0].isdigit()


for name, df in [('train', dftrain), ('test', dftest)]:
    for index, row in tqdm.tqdm(df.iterrows(),
                                total=df.shape[0],
                                desc="Computing the tail matching for " +
                                name):
        question = TextNormalizer.preprocess_question_str(row.question)
        paragraph = row.paragraph
Пример #30
0
from Indexer import Indexer
from FeatureExtractor import FeatureExtractor
from Segmenter import Segmenter
from PendingIterator import PendingIterator
from PDFExtractor import PDFExtractor as PDF
from params import index_store
import sys

pending = PendingIterator(index_store)
segm = Segmenter()
# https: // docs.python.org / 3 / library / sqlite3.html

for p in pending:

    segments = segm(PDF.read(p))
    for s_id, s in segments:
        print(s_id, s)
Пример #31
0
# -------------------------------------------------------------------

X_parag_train = None
X_parag1_train = None
X_quest_train = None

X_parag_test = None
X_parag1_test = None
X_quest_test = None

y_train = dftrain['target'].values

word2id = dict()
word2id[u''] = 0

segmenter = Segmenter()

for name, df in [('train', dftrain), ('test', dftest)]:

    X_parag  = np.zeros((df.shape[0], max_len), dtype=np.int32)
    X_parag1 = np.zeros((df.shape[0], max_len), dtype=np.int32)
    X_quest  = np.zeros((df.shape[0], max_len), dtype=np.int32)

    for index, row in tqdm.tqdm(df.iterrows(), total=df.shape[0], desc="Processing " + name):
        question = TextNormalizer.preprocess_question_str(row.question)
        paragraph = row.paragraph

        quest_words = TextNormalizer.tokenize_words(question)[0:max_len]
        parag_words = TextNormalizer.tokenize_words(paragraph)[0:max_len]

        for word_pos,word in enumerate(quest_words):
Пример #32
0
 def __init__(self):
     '''
     constructor
     '''
     self.segmenter = Segmenter()