Exemple #1
0
def generateCustomSprite():
    data = flask.request.get_json()
    userFolder = data['userFolder']
    labels = data['labels']
    included = data['included']
    fileName = data['fileNames'][0]
    height = int(data['height'])
    width = int(data['width'])
    selectedLabels = []
    for i in range(len(labels)):
        if included[i] == True:
            selectedLabels.append(labels[i])
    path = os.path.join(app.config['DATASET_FOLDER'] + userFolder + '/',
                        fileName)
    if not os.path.isdir(app.config['DATASET_FOLDER'] + userFolder +
                         '/spritesheet'):
        os.makedirs(app.config['DATASET_FOLDER'] + userFolder + '/spritesheet')
    file = zipfile.ZipFile(path, 'r')
    contents = file.namelist()
    selectedFiles = []
    for item in contents:
        if included[labels.index(item.split('/')[0])] == True:
            selectedFiles.append(item)
    file.extractall(
        app.config['DATASET_FOLDER'] + userFolder + '/spritesheet/',
        selectedFiles)
    try:
        createSpritesheet(app.config['DATASET_FOLDER'] + userFolder,
                          selectedLabels, width, height)
    except:
        return flask.Response(json.dumps({'error': str(sys.exc_info()[0])}),
                              status=400)
    with open(app.config['DATASET_FOLDER'] + userFolder +
              '/spritesheet/labelnames.csv',
              'w',
              newline='') as labelsnamesCSVfile:
        writer = csv.writer(labelsnamesCSVfile)
        writer.writerow(selectedLabels)
    zipObj = zipfile.ZipFile(
        app.config['DATASET_FOLDER'] + userFolder + '/spritesheet/dataset.zip',
        'w')
    zipObj.write(
        app.config['DATASET_FOLDER'] + userFolder + '/spritesheet/data.jpg',
        '/data.jpg')
    zipObj.write(
        app.config['DATASET_FOLDER'] + userFolder + '/spritesheet/labels.bin',
        '/labels.bin')
    zipObj.write(
        app.config['DATASET_FOLDER'] + userFolder +
        '/spritesheet/labelnames.csv', '/labelnames.csv')
    download_link = '/workbench/sprite/download/' + userFolder
    download_file(userFolder)
    return flask.Response(json.dumps({
        'status': 'done',
        'userFolder': userFolder,
        'download': download_link
    }),
                          status=200)
Exemple #2
0
def generateSprite():
    data = flask.request.get_json()
    userFolder = data['userFolder']
    labels = data['labels']
    included = data['included']
    fileNames = data['fileNames']
    height = int(data['height'])
    width = int(data['width'])
    selectedLabels = []
    for i in range(len(labels)):
        if included[i] == True:
            selectedLabels.append(labels[i])
    for i in range(len(fileNames)):
        path = os.path.join(
            app.config['DATASET_FOLDER']+userFolder+'/', fileNames[i])[0:-4]
        csvFile = path+'/patches.csv'
        with open(csvFile, 'r') as data1:
            i = 0
            for line in csv.reader(data1):
                if i != 0 and line[2] != '' and line[2] in selectedLabels:
                        if not os.path.isdir(app.config['DATASET_FOLDER']+userFolder+'/spritesheet/'+line[2]):
                            os.makedirs(
                                app.config['DATASET_FOLDER']+userFolder+'/spritesheet/'+line[2])
                        file = pathlib.Path(path+line[8][1:])
                        if not file.is_file():
                            deleteDataset(userFolder)
                            return flask.Response(json.dumps({'error': 'Images are missing from one or more zip files'}), status=400)
                        file = path+line[8][1:]
                        fileName = ''.join(random.choice(
                            string.ascii_lowercase + string.digits) for _ in range(40))
                        newFile = app.config['DATASET_FOLDER']+userFolder + \
                            '/spritesheet/'+line[2]+'/'+fileName+'.jpg'
                        shutil.move(file, newFile)
                i += 1
    try:
        createSpritesheet(app.config['DATASET_FOLDER']+userFolder, selectedLabels, width, height)
    except:
        return flask.Response(json.dumps({'error': str(sys.exc_info()[0])}), status=400)
    with open(app.config['DATASET_FOLDER']+userFolder+'/spritesheet/labelnames.csv', 'w', newline='') as labelsnamesCSVfile:
        writer = csv.writer(labelsnamesCSVfile)
        writer.writerow(selectedLabels)
    zipObj = zipfile.ZipFile(
        app.config['DATASET_FOLDER']+userFolder+'/spritesheet/dataset.zip', 'w')
    zipObj.write(app.config['DATASET_FOLDER'] +
                 userFolder+'/spritesheet/data.jpg', '/data.jpg')
    zipObj.write(app.config['DATASET_FOLDER']+userFolder +
                 '/spritesheet/labels.bin', '/labels.bin')
    zipObj.write(app.config['DATASET_FOLDER']+userFolder +
                 '/spritesheet/labelnames.csv', '/labelnames.csv')
    download_link = '/workbench/sprite/download/'+userFolder
    download_file(userFolder)
    return flask.Response(json.dumps({'status': 'done', 'userFolder': userFolder, 'download': download_link}), status=200)