def collectImageDataset(srcPath, maskImgPath, dstImgPath, dstMaskImgPath,
                        fileListFile):
    files = pathsFilesList(srcPath, filter='png')
    # total = len(files)

    for i, f in enumerate(files):
        fileName = getFileName(f)
        maskFileName = fileName[:fileName.rfind('.')] + '_mask.png'

        fMaskFile = maskImgPath + '\\' + maskFileName
        # print('fMaskFile=',fMaskFile)

        dstImg = dstImgPath + '\\' + fileName
        dstMaskImg = dstMaskImgPath + '\\' + maskFileName

        # copyFile(f, dstImg)
        # copyFile(fMaskFile, dstMaskImg)
        img = loadImg(f)
        img = grayImg(resizeImg(img, newH=trainImgHeight, newW=trainImgWidth))
        assert (img is not None)
        writeImg(img, dstImg)

        imgMask = loadImg(fMaskFile)
        imgMask = grayImg(
            resizeImg(imgMask, newH=trainImgHeight, newW=trainImgWidth))
        assert (imgMask is not None)
        writeImg(imgMask, dstMaskImg)

        # print('img.shape=',img.shape)
        # print('imgMask.shape=',imgMask.shape)
        writeToDst(fileListFile,
                   dstImg + ',' + dstMaskImg + '\n')  # new  trainList.list
Exemplo n.º 2
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')
Exemplo n.º 3
0
 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)
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)
Exemplo n.º 7
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')
Exemplo n.º 8
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')
Exemplo n.º 9
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)
def getImgAnnotFile(annotPath, file):
    fAnnot = getFileName(file)
    return annotPath + '\\' + fAnnot[:fAnnot.rfind('.')] + '.txt'