Exemple #1
0
def getTrainDict():
    '''
    Creates a features list from given symbol images in train directory in form of a hashmap
    :return: a hashmap(dictionary) of labelled symbols(key) and their features(value) from train directory
    '''
    trainDir = os.listdir(meta.getRootDir() + '/images/labelled_symbol/')
    trainDict = {}
    for symbolFile in trainDir:
        image = cv2.imread(meta.getRootDir() + '/images/labelled_symbol/' + symbolFile, cv2.IMREAD_GRAYSCALE)
        scaledImage = part3.scaleSymbol(image)
        meta.saveImage(scaledImage,symbolFile)
        symbolName = symbolFile.split('.')
        trainDict[symbolName[0]] = scaledImage
    return trainDict
Exemple #2
0
def createMidi(mappedKeys, fileName):
    track = 0
    channel = 0
    time = 0  # In beats
    duration = 1  # In beats
    tempo = 100  # In BPM
    volume = 100  # 0-127, as per the MIDI standard

    MyMIDI = MIDIFile(
        1, adjust_origin=True
    )  # One track, defaults to format 1 (tempo track is created automatically)
    MyMIDI.addTempo(track, time, tempo)

    for i, pitch in enumerate(mappedKeys):
        if (mappedKeys[i] != 00):
            if (len(mappedKeys) > i + 1):
                if (mappedKeys[i + 1] != 00):
                    MyMIDI.addNote(track, channel, pitch, time + i, duration,
                                   volume)
                else:
                    MyMIDI.addNote(track, channel, pitch, time + i,
                                   duration + 1, volume)
            else:
                MyMIDI.addNote(track, channel, pitch, time + i, duration,
                               volume)

    with open(meta.getRootDir() + '/MIDI_files/' + fileName + '.mid',
              "wb") as output_file:
        MyMIDI.writeFile(output_file)
Exemple #3
0

#        calculates vertical_histogram
        i = y1
        while (i <= y2):
            while (i <= y2 and vertical_histogram[i] == 0):
                i = i + 1
            start = i
            while (i <= y2 and vertical_histogram[i] > 0):
                i = i + 1
            end = i
            if (end - start >= 7):
                #                Identified a symbol
                output.append(Rectangle.Rectangle((start, x1), (end, x2)))
                cv2.rectangle(binary_image, (start, x1), (end, x2), 0, 1)
    meta.displayImage(binary_image, 'binary')
    return output

if __name__ == "__main__":
    color_image = cv2.imread(meta.getRootDir() + '/images/vasant_cont.jpeg',
                             cv2.IMREAD_COLOR)
    input_lines = [((0, 0), (439, 30)), ((0, 90), (439, 120)),
                   ((0, 213), (439, 243))]
    rectList = []
    for tuple in input_lines:
        rectList.append(Rectangle.Rectangle(tuple[0], tuple[1]))
    o, binary_image = segmentImage(color_image, rectList)
    cv2.imshow('binary_image', binary_image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
Exemple #4
0
    :param color_image: input image
    :return: list of identified symbols
    '''
    gray_image = cv2.cvtColor(color_image, cv2.COLOR_BGR2GRAY)
    trainDict = getTrainDict()
    testSymbols = getTestSymbols(color_image)
    testResults = []
    count = 0
    for symbol in testSymbols:
        gray_symbol = gray_image[symbol.colst:symbol.colen, symbol.rowst:symbol.rowen]
        scaledsymbol = part3.scaleSymbol(gray_symbol)
        count = count + 1
        ans = trainDict.keys()[1]
        anscount = len(np.where(scaledsymbol == trainDict[ans]))
        for trainedSymbol in trainDict.keys():
            matchcount = np.sum(scaledsymbol == trainDict[trainedSymbol])
            if matchcount > anscount:
                ans, anscount = trainedSymbol, matchcount
        testResults.append(ans)
        #meta.saveImage(scaledsymbol, ans+str(count)+"kafi.jpeg")
    return testResults

if __name__ == "__main__":
    color_image = cv2.imread(meta.getRootDir() + '/images/kafi.jpeg', cv2.IMREAD_COLOR)
    testResults = main_first(color_image)
    # for i in range(len(testResults)):
    #     if testResults[i] == 'hy' and i>0:
    #         testResults[i] = testResults[i-1]
    print testResults
    part4.keyMapping(testResults,'kafi')
Exemple #5
0
    symbolImage = meta.binary_threshold(symbolImage, 200)
    #symbolImage = meta.gauss_otsu_threshold(symbolImage,ksize=3)
    #meta.saveImage(symbolImage, "BinThres.jpeg")
    height, width = symbolImage.shape
    black_rows = np.where(np.any(symbolImage == 0, axis=1))[0]
    black_cols = np.where(np.any(symbolImage == 0, axis=0))[0]
    row_start, row_end = 0, height - 1
    if len(black_rows) > 0:
        row_start, row_end = min(black_rows), max(black_rows)
    col_start, col_end = 0, width - 1
    if len(black_cols) > 0:
        col_start, col_end = min(black_cols), max(black_cols)
    s = cv2.resize(symbolImage[row_start:row_end + 1, col_start:col_end + 1],
                   (32, 32))
    return meta.otsu_threshold(s)


if __name__ == "__main__":
    trainDir = os.listdir(meta.getRootDir() + '/images/labelled_symbol/')
    trainDict = {}
    for symbolFile in trainDir:
        print symbolFile
        image = cv2.imread(
            meta.getRootDir() + '/images/labelled_symbol/' + symbolFile,
            cv2.IMREAD_GRAYSCALE)
        scaledImage = scaleSymbol(image)
        meta.saveImage(scaledImage, "output.jpeg")
        trainDict[symbolFile[:-5]] = scaledImage
    print trainDict.keys()

    # meta.displayImage(scaledImage,"ScaledImage")
Exemple #6
0
    '''
    #meta.saveImage(symbolImage,"input.jpeg")
    symbolImage = meta.adapt_mean_threshold(symbolImage,blockSize=9,C=7)
    #meta.saveImage(symbolImage, "meanThres.jpeg")
    symbolImage = meta.binary_threshold(symbolImage,200)
    #meta.saveImage(symbolImage, "BinThres.jpeg")
    height, width = symbolImage.shape
    black_rows = np.where(np.any(symbolImage == 0, axis=1))[0]
    black_cols = np.where(np.any(symbolImage == 0, axis=0))[0]
    row_start,row_end = 0,height-1
    if len(black_rows) > 0:
        row_start, row_end = min(black_rows),max(black_rows)
    col_start,col_end = 0,width-1
    if len(black_cols) > 0:
        col_start, col_end = min(black_cols),max(black_cols)
    s = cv2.resize(symbolImage[row_start:row_end+1,col_start:col_end+1],(16,16))
    return meta.otsu_threshold(s)


if __name__ == "__main__":
    trainDir = os.listdir(meta.getRootDir() + '/images/labelled_symbol/')
    trainDict = {}
    for symbolFile in trainDir:
        print symbolFile
        image = cv2.imread(meta.getRootDir() + '/images/labelled_symbol/'+symbolFile, cv2.IMREAD_GRAYSCALE)
        scaledImage = scaleSymbol(image)
        meta.saveImage(scaledImage,"output.jpeg")
        trainDict[symbolFile[:-5]] = scaledImage
    print trainDict.keys()

    '''
    gray_image = cv2.cvtColor(color_image, cv2.COLOR_BGR2GRAY)
    trainDict = getTrainDict()
    testSymbols = getTestSymbols(color_image)
    testResults = []
    count = 0
    for symbol in testSymbols:
        gray_symbol = gray_image[symbol.colst:symbol.colen,
                                 symbol.rowst:symbol.rowen]
        scaledsymbol = part3.scaleSymbol(gray_symbol)
        count = count + 1
        ans = trainDict.keys()[1]
        anscount = len(np.where(scaledsymbol == trainDict[ans]))
        for trainedSymbol in trainDict.keys():
            matchcount = np.sum(scaledsymbol == trainDict[trainedSymbol])
            if matchcount > anscount:
                ans, anscount = trainedSymbol, matchcount
        testResults.append(ans)
        #meta.saveImage(scaledsymbol, ans+str(count)+"kafi.jpeg")
    return testResults


if __name__ == "__main__":
    color_image = cv2.imread(meta.getRootDir() + '/images/kafi.jpeg',
                             cv2.IMREAD_COLOR)
    testResults = main_first(color_image)
    # for i in range(len(testResults)):
    #     if testResults[i] == 'hy' and i>0:
    #         testResults[i] = testResults[i-1]
    print testResults
    part4.keyMapping(testResults, 'kafi')