コード例 #1
0
def resize_images(imagePath, resizedPath, size=[800, 800]):
    cv = Convnet(False, False, size, form=True)
    sizeTxt = '{}x{}'.format(size[0], size[1])
    lastResized = getLastFromDir(resizedPath + '/' + sizeTxt)
    for subdir, dirs, files in os.walk(imagePath):
        lenFiles = len(files)
        break
    nameList = list(x + lastResized for x in range(lenFiles))
    x = 0
    for subdir, dirs, files in os.walk(imagePath):
        for file in files:
            path = subdir.replace("""\ """, '/') + '/' + file
            try:
                img = imread(path).astype(np.float32)
                if img.shape[2] != 3:
                    newImg = np.zeros((img.shape[0], img.shape[1], 3))
                    for x in range(len(newImg)):
                        for y in range(len(newImg[x])):
                            for z in range(len(newImg[x][y])):
                                newImg[x][y][z] = img[x][y][z]
                    img = newImg
            except Exception as e:
                print(e)
                print(file)
                continue
            img = img.reshape(img.shape[0], img.shape[1], 3)
            image = cv.format_images([img])[0]
            name = nameList[random.randrange(len(nameList))]
            scipy.misc.imsave(
                resizedPath + '/' + sizeTxt + '/' + '{}.png'.format(x), image)
            x += 1
            nameList.remove(name)
            lastResized += 1
コード例 #2
0
def save_as_array(imagePath, arrayPath, size=[800, 800]):
    cv = Convnet(False, False, size, form=True)
    sizeTxt = '{}x{}'.format(size[0], size[1])
    lastArray = getLastFromDir(arrayPath + '/' + sizeTxt)
    batch = []
    for subdir, dirs, files in os.walk(imagePath):
        for file in files:
            path = subdir.replace("""\ """, '/') + '/' + file
            img = imread(path).astype(np.float32)
            batch.append(img)
            if len(batch) >= batch_size:
                with open(
                        arrayPath + '/' + sizeTxt + '/' + str(lastArray + 1) +
                        '.p', "wb") as f:
                    batch = cv.format_images(batch)
                    lastArray += 1
                    pickle.dump(batch, f)
                batch = []
    with open(arrayPath + '/' + str(lastArray + 1) + '.p', "wb") as f:
        pickle.dump(batch, f)