with open(output, 'wb') as outcsv:
        writer = csv.writer(outcsv, dialect='excel')
        header = [
            'filename', 'distance', 'num_node', "score", "num_leaf",
            "count_contourBoxes", "count_shareBox"
        ]
        writer.writerow(header)

        for row in data:
            writer.writerow(row)


if __name__ == '__main__':

    clfPath = "/Users/sephon/Desktop/Research/VizioMetrics/Corpus/Phylogenetic/CNN_corpus/line_patch/models/RF.pkl"
    phyloParser = PhyloParser(clfPath=clfPath)
    ground_truth_path = "/Users/sephon/Desktop/Research/VizioMetrics/Corpus/Phylogenetic/CNN_corpus/hq_ground_truth0228.csv"
    folderPath = '/Users/sephon/Desktop/Research/VizioMetrics/Corpus/Phylogenetic/CNN_corpus/high_quality_tree'
    ground_truth = {}
    fileNameList = []
    with open(ground_truth_path, 'rb') as incsv:
        reader = csv.reader(incsv, dialect='excel')
        reader.next()

        for row in reader:
            ground_truth[row[0]] = row[1]
            #             print row[0], row[1]
            fileNameList.append(row[0])

#     fileNameList = getFilesInFolder(folderPath)
    outFileName = '/Users/sephon/Desktop/Research/VizioMetrics/Corpus/Phylogenetic/CNN_corpus/high_quality_tree_20170307_rline_new.csv'
    viewPatch = getViewPatch(image, line)

    #     avgline = [int(avg_line_x), 0, int(avg_line_x), image.shape[0], 0]
    #     print avgline
    #     PhyloParser.displayLines(image, [avgline])

    #     PhyloParser.displayImage(viewPatch)
    #     PhyloParser.displayImage(hpatch)
    #     PhyloParser.displayImage(vpatch)

    return feature, viewPatch


if __name__ == '__main__':

    phyloParser = PhyloParser()

    folderPath = '/Users/sephon/Desktop/Research/VizioMetrics/Corpus/Phylogenetic/CNN_corpus/far_species'
    ground_truth = {}
    fileNameList = []

    fileList = []
    for dirPath, dirNames, fileNames in os.walk(folderPath):
        for f in fileNames:
            extension = f.split('.')[-1].lower()
            if extension in ["jpg", "png"]:
                fileList.append(os.path.join(dirPath, f))

#     fileNameList = getFilesInFolder(folderPath)
    pathFolder = '/Users/sephon/Desktop/Research/VizioMetrics/Corpus/Phylogenetic/CNN_corpus/line_patch'
    #     datasetFlieName = '/Users/sephon/Desktop/Research/VizioMetrics/Corpus/Phylogenetic/CNN_corpus/line_feature.npy'
    boundaries = [[335, 351, 185, 408], [351, 365, 212, 470]]
    boxes = [[[336, 344, 382, 385], [335, 344, 315, 333], [336, 345, 337, 342],
              [336, 345, 389, 408], [335, 345, 185, 257], [335, 349, 262, 310],
              [335, 349, 346, 404]],
             [[355, 362, 446, 448], [355, 363, 452, 470], [354, 363, 410, 435],
              [355, 365, 212, 292], [354, 365, 298, 347], [354, 365, 346,
                                                           404]]]

    boundaries, boxes = PhyloParser.stitchBoundries(boundaries, boxes)
    print boundaries
    print boxes

    #     new_boundaries, boxes =  PhyloParser.splitBoundaries(boundaries, boxes)

    phyloPaser = PhyloParser()

    filename = '/Users/sephon/Desktop/Research/VizioMetrics/Corpus/Phylogenetic/phylogenetic_tree_for_parsing/PMC1474148_ijbsv02p0133g05.jpg'
    filename = '/Users/sephon/Desktop/Research/VizioMetrics/Corpus/Phylogenetic/phylogenetic_tree_for_parsing/PMC2324105_1471-2148-8-100-1.jpg'

    filename = '/Users/sephon/Desktop/Research/VizioMetrics/Corpus/Phylogenetic/phylogenetic_tree_for_parsing/PMC2323573_pone.0002033.g002.jpg'
    filename = '/Users/sephon/Desktop/Research/VizioMetrics/Corpus/Phylogenetic/phylogenetic_tree_for_parsing/image_5569.jpg'

    filename = '/Users/sephon/Desktop/Research/VizioMetrics/Corpus/Phylogenetic/phylogenetic_tree_for_parsing/PMC2323573_pone.0002033.g002.jpg'
    #     filename = '/Users/sephon/Desktop/Research/VizioMetrics/Corpus/Phylogenetic/phylogenetic_tree_for_parsing/PMC2761929_1471-2180-9-208-3.jpg'

    # bg example
    #     filename = '/Users/sephon/Desktop/Research/VizioMetrics/Corpus/Phylogenetic/phylogenetic_tree_for_parsing/PMC2287175_1471-2148-8-57-2.jpg'
    #     filename = '/Users/sephon/Desktop/Research/VizioMetrics/Corpus/Phylogenetic/phylogenetic_tree_for_parsing/image_336.jpg'
    #     filename = '/Users/sephon/Desktop/Research/VizioMetrics/Corpus/Phylogenetic/phylogenetic_tree_for_parsing/image_337.jpg'
    #     filename = '/Users/sephon/Desktop/Research/VizioMetrics/Corpus/Phylogenetic/phylogenetic_tree_for_parsing/tree16.jpg'
def execute(filePath):

    ############ run experiment ######################

    # a = ExperimentExecutor()

    # a.autoRun('high_quality_tree/', 'groundTruth.csv', 'compareResult_0124.csv')

    ###################################################

    # PMC2323573_pone.0002033.g002.jpg

    try:
        phyloParser = PhyloParser(clfPath=clfPath)

        # folderPath = 'images/'
        # folderPath = 'high_quality_tree/'

        # fileNameList = getFilesInFolder(folderPath)

        # fileNameList.sort()
        # with open('image_fileName_list.txt', 'r')as f:
        #     fileNameList = f.readlines()

        # testingFilePath = [join(folderPath, x) for x in fileNameList]
        # result = {}
        # groundTruth = {}
        # a = ExperimentExecutor()
        # a.execute(result, groundTruth, testingFilePath)

        # for index, fileName in enumerate(fileNameList):
        #     print index, fileName
        #         # f.write(fileName + '\n')
        # fileNameList = [x.rstrip() for x in fileNameList]
        # fileNameList = [folderPath + x for x in fileNameList]

        # for index in range(15, len(fileNameList)):
        # try:
        # print index, fileNameList[index]
        # filePath = folderPath + fileNameList[index]
        # fileName = fileNameList[index]
        # # fileName = 'PMC2614190_ebo-04-181-g01.jpg'
        # # fileName = 'PMC2674049_1471-2148-9-74-4.jpg'
        # # fileName = 'PMC1326215_1471-2148-5-71-6.jpg'
        # # fileName = 'PMC2644698_1471-2229-8-133-4.jpg'
        # # fileName = 'PMC2644698_1471-2229-8-133-4.jpg'
        # # fileName = 'PMC2775678_IJMB2009-701735.001.jpg'
        # # fileName = 'PMC2697986_1471-2148-9-107-3.jpg'
        # # fileName = 'PMC1182362_1471-2148-5-38-3.jpg'
        # filePath = folderPath + fileName
        # filePath = 'treeset/images/multi/1471-2148-10-52-2-l.jpg'
        if isfile(filePath):
            image = cv.imread(filePath, 0)
        # PhyloParser.displayImage(image)

        # imageData = ImageData(image)
        # imageData = PhyloParser.preprocces(imageData, debug = False)
        # # imageData = PhyloParser.detectLines__v2(imageData, debug=True)
        # imageData = PhyloParser.testing(imageData)

        image_data = ImageData(image)
        image_data = phyloParser.preprocces(image_data, debug=False)
        # PhyloParser.displayImage(image)

        image_data = phyloParser.detectLines(image_data, debug=False)

        # image_data = phyloPaser.traceTree(image_data, debug = True)
        image_data = phyloParser.getCorners(image_data, debug=False)
        image_data = phyloParser.makeLinesFromCorner(image_data, debug=False)
        image_data = phyloParser.includeLinesFromCorners(image_data)

        image_data = phyloParser.postProcessLines(image_data)

        # image_data = phyloParser.removeRepeatedLines(image_data)
        image_data = phyloParser.groupLines(image_data, debug=False)
        image_data = phyloParser.matchLineGroups(image_data, debug=False)

        #         # image_data = phyloParser.detectCorners(image_data)
        #         # image_data = phyloParser.refineLinesByCorners(image_data)
        #         # image_data = phyloPaser.getSpecies_v2(image_data, debug = True)
        #         # image_data = phyloPaser.getSpecies(image_data, debug = True)
        # image_data = phyloParser.matchLines(image_data, debug = True, useNew = False)
        # image_data = phyloParser.getSpecies_v2(image_data, debug = True)
        #         # print image_data.orphanBox2Text
        image_data = phyloParser.getSpecies_v3(image_data, debug=False)
        # testString = phyloParser.makeTree(image_data, debug = True, tracing = False)
        # treeString = phyloParser.constructTreeByTracing(image_data, debug = False)
        treeString = phyloParser.constructTree_eval(image_data,
                                                    debug=False,
                                                    tracing=False)

        dbdata = treeString.treeHead.getParentChildrenStyle()
        # print dbdata['cluster']

        with open('dbdata.csv', 'ab') as f:
            csvwriter = csv.writer(f, delimiter='\t')
            for i in range(dbdata['relation_index'] + 1):
                if i >= dbdata['count_labels']:
                    string = ''
                    for node in dbdata[i]:
                        string += '#' + str(node)
                    csvwriter.writerow([fileName, str(i), string])
                else:
                    csvwriter.writerow([fileName, str(i), dbdata[i]])
        with open('dbdata_cluster.csv', 'ab') as f:
            csvwriter = csv.writer(f, delimiter='\t')
            if 'root' in dbdata['cluster']:
                csvwriter.writerow([
                    fileName,
                    str(dbdata['relation_index']), dbdata['cluster']['root']
                ])
            for i in range(dbdata['relation_index']):
                csvwriter.writerow([fileName, str(i), dbdata['cluster'][i]])
    except:
        with open('error.txt', 'ab') as f:
            f.write(filePath + '\n')
    return fileNameList


def saveResult(data, output):
    with open(output, 'wb') as outcsv:
        writer = csv.writer(outcsv, dialect='excel')
        header = ['filename', 'distance', 'num_node', "score"]
        writer.writerow(header)

        for row in data:
            writer.writerow(row)


if __name__ == '__main__':

    phyloParser = PhyloParser()
    ground_truth_path = "/Users/sephon/Desktop/Research/VizioMetrics/Corpus/Phylogenetic/CNN_corpus/hq_ground_truth.csv"
    ground_truth = {}
    with open(ground_truth_path, 'rb') as incsv:
        reader = csv.reader(incsv, dialect='excel')
        reader.next()

        for row in reader:
            ground_truth[row[0]] = row[1]
#             print row[0], row[1]

    folderPath = '/Users/sephon/Desktop/Research/VizioMetrics/Corpus/Phylogenetic/CNN_corpus/high_quality_tree'
    fileNameList = getFilesInFolder(folderPath)
    outFileName = '/Users/sephon/Desktop/Research/VizioMetrics/Corpus/Phylogenetic/CNN_corpus/high_quality_tree_result_line_corners_0227.csv'
    results = []
Beispiel #6
0
from ete3 import Tree
import operator
import random
from os import listdir
from os.path import isfile, join


from PhyloParser import *

def getFilesInFolder(folderPath):
    fileNameList = [f for f in listdir(folderPath) if isfile(join(folderPath, f))]

    return fileNameList

if __name__ == '__main__':
    phyloPaser = PhyloParser()
    
    folderPath = 'images/'
    fileNameList = getFilesInFolder(folderPath)
    
    for index, fileName in enumerate(fileNameList):
        print index, fileName

    for index in range(2, len(fileNameList)):
        print index
        filePath = folderPath + fileNameList[index]
        if isfile(filePath) :
            image = cv.imread(filePath,0)
            
        image_data = ImageData(image)
        image_data = phyloPaser.preprocces(image_data)