Пример #1
0
def generateMaskByScaling(imgPath,
                          maskImgPath,
                          dstImgPath,
                          dstMaskImgPath,
                          rStart=0.2,
                          rStop=2,
                          N=20):
    createPath(dstImgPath)
    createPath(dstMaskImgPath)
    for i in listFile(imgPath):
        imgFile = getFileName(i)
        img = loadImg(i)
        # H,W = getImgHW(img)
        name = imgFile[:imgFile.rfind('.')]
        maskImgFile = maskImgPath + '\\' + name + '_mask' + '.png'
        maskImg = loadImg(maskImgFile)
        # print(i,imgFile,maskImgFile)

        # start generate new images
        ratios = np.linspace(rStart, rStop, N)
        for ratio in ratios:
            # print('ratio=',ratio)
            rImg = resizeRtImg(img, ratio)
            newImgFile = name + '_scale_' + str(ratio)
            writeImg(rImg, dstImgPath + '\\' + newImgFile + '.png')

            rMaskImg = resizeRtImg(maskImg, ratio)
            newMaskImgFile = newImgFile + '_mask'
            writeImg(rMaskImg, dstMaskImgPath + '\\' + newMaskImgFile + '.png')
def generateImageLabel(imgPath, annotPath, dst):
    deleteFolder(dst)
    createPath(dst)
    for i in listFile(imgPath, 'png'):
        H, W = getImgHW(loadImg(i))
        # print(H,W)
        fAnnot = getImgAnnotFile(annotPath, i)
        fAnnotName = getFileName(fAnnot)
        dstFile = dst + '\\' + fAnnotName
        print('fAnnot=', fAnnot)
        # print('dstFile=', dstFile)

        deleteFile(dstFile)
        coordinates = getFileCoordinates(fAnnot)
        writeToAnnotFile(H, W, dstFile, coordinates)
def main():
    args = cmd_line()
    src = args.src
    dst = args.dst

    createPath(dst)
    print('src=', src, 'dst=', dst)

    for i in pathsFiles(src, 'jpg'):  # png
        fileName = getFileName(i)
        img = loadImg(i)

        dstFile = args.dst + '\\' + fileName
        print(i, fileName, dstFile)
        predImg = getPredictionMaskImg(img)
        writeImg(predImg, dstFile)
def testFileLabel(imgPath, LabelPath, dstRecImgPath):
    createPath(dstRecImgPath)
    for i in listFile(imgPath):
        imgFile = getFileName(i)
        img = loadImg(i)
        H, W = getImgHW(img)
        print(i, imgFile, H, W)

        labelFile = LabelPath + '\\' + imgFile[:imgFile.rfind('.')] + '.txt'
        # labels = getLabelFileLabels(labelFile)
        # print(labels)
        coordinats = getCoordinatesFromLabels(H, W, labelFile)
        # print(coordinats)
        recImg = rectangleImgFromCoordinates(img.copy(), coordinats)

        destFile = dstRecImgPath + '\\' + imgFile[:imgFile.rfind('.')] + '_rec' + '.png'
        print(destFile)
        writeImg(recImg, destFile)
Пример #5
0
def generateMaskByClipping(imgPath,
                           maskImgPath,
                           annotPath,
                           dstImgPath,
                           dstMaskImgPath,
                           N=10):
    createPath(dstImgPath)
    createPath(dstMaskImgPath)
    for i in listFile(imgPath):
        imgFile = getFileName(i)
        fAnnot = getImgAnnotFile(annotPath, i)
        img = loadImg(i)
        H, W = getImgHW(img)
        # print(fAnnot,H,W)
        name = imgFile[:imgFile.rfind('.')]
        maskImgFile = maskImgPath + '\\' + name + '_mask' + '.png'
        maskImg = loadImg(maskImgFile)

        coordinates = getFileCoordinates(fAnnot)
        boundCoordinate = getBoundaryCoordinate(coordinates)
        print(i, H, W, coordinates, boundCoordinate)

        clipXmin = np.random.randint(boundCoordinate[0], size=N)
        clipYmin = np.random.randint(boundCoordinate[1], size=N)
        clipXmax = np.random.randint(W - boundCoordinate[2], size=N)
        clipYmax = np.random.randint(H - boundCoordinate[3], size=N)

        for Xmin, YMin, Xmax, Ymax in zip(clipXmin, clipYmin, clipXmax,
                                          clipYmax):
            clipCoordinate = (Xmin, YMin, Xmax + boundCoordinate[2],
                              Ymax + boundCoordinate[3])
            clipImg, _ = clipImgCoordinate(img.copy(), clipCoordinate,
                                           coordinates)
            # print(coordinates,clipCoordinate,newCoordinates,'NewH=',clipImg.shape[0],'NewW=',clipImg.shape[1])
            clipImgName = imgFile[:imgFile.rfind('.')] + '_' + str(
                clipCoordinate[0]) + '_' + str(clipCoordinate[1]) + '_' + str(
                    clipCoordinate[2]) + '_' + str(clipCoordinate[3])
            # print(clipImgName)
            writeImg(clipImg, dstImgPath + '\\' + clipImgName + '.png')

            clipMaskImg, _ = clipImgCoordinate(maskImg.copy(), clipCoordinate,
                                               coordinates)
            writeImg(clipMaskImg,
                     dstMaskImgPath + '\\' + clipImgName + '_mask' + '.png')
Пример #6
0
def generateMaskByFlipping(imgPath, maskImgPath, dstImgPath, dstMaskImgPath):
    createPath(dstImgPath)
    createPath(dstMaskImgPath)
    for i in listFile(imgPath):
        imgFile = getFileName(i)
        img = loadImg(i)
        # H,W = getImgHW(img)
        name = imgFile[:imgFile.rfind('.')]
        maskImgFile = maskImgPath + '\\' + name + '_mask' + '.png'
        maskImg = loadImg(maskImgFile)
        # print(i,imgFile,maskImgFile)

        rImg = flipImg(img)
        newImgFile = name + '_flip'
        writeImg(rImg, dstImgPath + '\\' + newImgFile + '.png')

        rMaskImg = flipImg(maskImg)
        newMaskImgFile = newImgFile + '_mask'
        writeImg(rMaskImg, dstMaskImgPath + '\\' + newMaskImgFile + '.png')
def main():
    base = r'.\res\PennFudanPed\newImages\trainImages\\'

    dstImgPath = base + r'trainPNGImage'
    dstMaskImgPath = base + r'trainPNGImageMask'
    fileListFile = base + r'trainList.list'

    deleteFile(fileListFile)
    deleteFolder(dstImgPath)
    createPath(dstImgPath)
    deleteFolder(dstMaskImgPath)
    createPath(dstMaskImgPath)
    '''
    dstTrainPath = base + r'train_PNGImages'
    dstTestPath = base + r'test_PNGImages'
    dstTrainMaskPath =  base + r'train_MaskImages'
    dstTestMaskPath =  base + r'test_MaskImages'


    deleteFolder(dstTrainPath)
    createPath(dstTrainPath)
    deleteFolder(dstTestPath)
    createPath(dstTestPath)
    deleteFolder(dstTrainMaskPath)
    createPath(dstTrainMaskPath)
    deleteFolder(dstTestMaskPath)
    createPath(dstTestMaskPath)
    '''
    imgPathList = []
    imgPathList.append((r'.\res\PennFudanPed\newImages\newMaskCropping',
                        r'.\res\PennFudanPed\newImages\newMaskCroppingMask'))
    imgPathList.append((r'.\res\PennFudanPed\newImages\newMaskFlipping',
                        r'.\res\PennFudanPed\newImages\newMaskFlippingMask'))
    imgPathList.append((r'.\res\PennFudanPed\newImages\newMaskScaling',
                        r'.\res\PennFudanPed\newImages\newMaskScalingMask'))

    for i in imgPathList:
        imgPath = i[0]
        maskImgPath = i[1]
        collectImageDataset(imgPath, maskImgPath, dstImgPath, dstMaskImgPath,
                            fileListFile)
Пример #8
0
def colorAugmentation(imgPath, maskImgPath, annotPath):
    print('Color augmentation start...')
    print('imgPath=', imgPath)
    print('maskImgPath=', maskImgPath)

    tmpPath = r'.\res\PennFudanPed\tmp'

    for i in listFile(imgPath):
        deleteFolder(tmpPath)
        createPath(tmpPath)

        imgFile = getFileName(i)
        # img = loadImg(i)
        # H,W = getImgHW(img)
        name = imgFile[:imgFile.rfind('.')]
        maskImgFile = maskImgPath + '\\' + name + '_mask' + '.png'
        maskImg = loadImg(maskImgFile)

        fAnnot = getImgAnnotFile(annotPath, i)

        aug = ImageColorAug(i, tmpPath)
        aug.augmentAll(N=6)

        def copyNewFile(path):
            print('start to copy...')
            for f in listFile(path):
                name_ = getFileName(f)
                newName = name_[:name_.rfind('.')]
                newFile = imgPath + '\\' + newName + '.png'
                newMaskFile = maskImgPath + '\\' + newName + '_mask' + '.png'
                newAnnoFile = annotPath + '\\' + newName + '.txt'
                print(newFile, newMaskFile)
                copyFile(f, newFile)
                copyFile(fAnnot, newAnnoFile)
                writeImg(maskImg, newMaskFile)

        copyNewFile(tmpPath)