Ejemplo n.º 1
0
def getBuildingSet(prePath, resultPath):
    fh.createDirectory(resultPath)
    fh.deleteAllInDirectory(resultPath)
    resizedTiffPath = 'resizeTiffTmp.tif'
    for file in os.listdir(prePath):
        tifInfo = gh.getTifInfo(prePath + file)
        pixelRes = tifInfo['pixelResolution']
        resizeFactor = pixelRes[0] / BUILDING_RESOLUTION
        gh.resizeTiff(prePath + file, resizedTiffPath, resizeFactor)
        tifInfo = gh.getTifInfo(resizedTiffPath)
        pixelRes = tifInfo['pixelResolution']
        topLeftGps = tifInfo['topLeftCoordinate']
        size = tifInfo['size']
        im = cv2.imread(resizedTiffPath)
        imgheight = im.shape[0]
        imgwidth = im.shape[1]

        print(size[0])
        print(imgheight)
        print(size[1])
        print(imgwidth)

        for y in range(0, imgheight, BUILDING_IMAGE_SIZE):
            for x in range(0, imgwidth, BUILDING_IMAGE_SIZE):
                nextIm = im[y:y + BUILDING_IMAGE_SIZE,
                            x:x + BUILDING_IMAGE_SIZE]
                lat = topLeftGps[0] + pixelRes[1] * y
                long = topLeftGps[1] + pixelRes[0] * x
                tempGps = str(lat) + '_' + str(long)
                cv2.imwrite(
                    resultPath + str(pixelRes[0]) + '_' + tempGps + ".png",
                    nextIm)

        y = imgheight - BUILDING_IMAGE_SIZE
        for x in range(0, imgwidth, BUILDING_IMAGE_SIZE):
            nextIm = im[y:y + BUILDING_IMAGE_SIZE, x:x + BUILDING_IMAGE_SIZE]
            lat = topLeftGps[0] + pixelRes[1] * y
            long = topLeftGps[1] + pixelRes[0] * x
            tempGps = str(lat) + '_' + str(long)
            cv2.imwrite(resultPath + str(pixelRes[0]) + '_' + tempGps + ".png",
                        nextIm)

        x = imgwidth - BUILDING_IMAGE_SIZE
        for y in range(0, imgheight, BUILDING_IMAGE_SIZE):
            nextIm = im[y:y + BUILDING_IMAGE_SIZE, x:x + BUILDING_IMAGE_SIZE]
            lat = topLeftGps[0] + pixelRes[1] * y
            long = topLeftGps[1] + pixelRes[0] * x
            tempGps = str(lat) + '_' + str(long)
            cv2.imwrite(resultPath + str(pixelRes[0]) + '_' + tempGps + ".png",
                        nextIm)

        y = imgheight - BUILDING_IMAGE_SIZE
        x = imgwidth - BUILDING_IMAGE_SIZE
        nextIm = im[y:imgheight, x:imgwidth]
        lat = topLeftGps[0] + pixelRes[1] * y
        long = topLeftGps[1] + pixelRes[0] * x
        tempGps = str(lat) + '_' + str(long)
        cv2.imwrite(resultPath + str(pixelRes[0]) + '_' + tempGps + ".png",
                    nextIm)
Ejemplo n.º 2
0
def getBoundingBoxGPS(startLat, startLong, pixelResolution, pixelBoudingBox):
    lat1 = gh.pixelToGpsCoordinate(startLat, pixelResolution,
                                   pixelBoudingBox[0])
    long1 = gh.pixelToGpsCoordinate(startLong, pixelResolution,
                                    pixelBoudingBox[1])
    lat2 = gh.pixelToGpsCoordinate(startLat, pixelResolution,
                                   pixelBoudingBox[2])
    long2 = gh.pixelToGpsCoordinate(startLong, pixelResolution,
                                    pixelBoudingBox[3])
    return BoundingBoxGPS(lat1, long1, lat2, long2)
Ejemplo n.º 3
0
def dev(csvPath, tilesPath, imageResultPath):
    red = (0, 0, 255)
    green = (0, 255, 0)
    yellow = (0, 255, 255)
    blue = (255, 0, 0)
    data = fh.csvToDict(csvPath)
    for file in os.listdir(tilesPath):
        tifInfo = getTifInfo(tilesPath + file)
        pixelRes = tifInfo['pixelResolution']
        topLeftGps = tifInfo['topLeftCoordinate']
        im = readImage(tilesPath + file)
        for line in data:
            color = blue
            if 'BuildingPrediction' in line and 'Label' in line:
                damPred = json.loads(line['BuildingPrediction'])
                label = int(line['Label'])
                if label == 1:
                    color = green
                if label == 2:
                    color = yellow
                if label == 3:
                    color = red
                bBox = BoundingBoxGPS(damPred['BoundingBox']['lat1'],
                                      damPred['BoundingBox']['long1'],
                                      damPred['BoundingBox']['lat2'],
                                      damPred['BoundingBox']['long2'])
                buildPred = BuildingPrediction(damPred['Id'], bBox)
                pt1 = GeotiffHelper.gpsBoundingBoxToPixelArray(
                    bBox, topLeftGps[0], topLeftGps[1], pixelRes[0])
                cv2.rectangle(im, (pt1[1], pt1[0]), (pt1[3], pt1[2]), color)
        cv2.imwrite(imageResultPath + fh.extractFileName(file) + '.png', im)
Ejemplo n.º 4
0
def drawBuildingOnTile(csvBuildingPath, tilesPath, imageResultPath):
    # data = fh.csvToDict(Global.PRE_BUILDING_RESULT_PATH + Global.BUILDING_CSV_NAME + '.csv')
    data = fh.csvToDict(csvBuildingPath)
    for file in os.listdir(tilesPath):
        tifInfo = getTifInfo(tilesPath + file)
        pixelRes = tifInfo['pixelResolution']
        topLeftGps = tifInfo['topLeftCoordinate']
        im = readImage(tilesPath + file)
        for line in data:
            if 'BuildingPrediction' in line:
                damPred = json.loads(line['BuildingPrediction'])
                bBox = BoundingBoxGPS(damPred['BoundingBox']['lat1'],
                                      damPred['BoundingBox']['long1'],
                                      damPred['BoundingBox']['lat2'],
                                      damPred['BoundingBox']['long2'])
                buildPred = BuildingPrediction(damPred['Id'], bBox)
                pt1 = GeotiffHelper.gpsBoundingBoxToPixelArray(
                    bBox, topLeftGps[0], topLeftGps[1], pixelRes[0])
                rectColor = (255, 0, 0)
                if 'DamageLevel' in damPred:
                    damageLevel = int(damPred['DamageLevel'])
                    if damageLevel == 1:
                        rectColor = (0, 255, 0)
                    if damageLevel == 2:
                        rectColor = (0, 255, 255)
                    if damageLevel == 3:
                        rectColor = (0, 0, 255)
                cv2.rectangle(im, (pt1[1], pt1[0]), (pt1[3], pt1[2]),
                              rectColor)
        cv2.imwrite(imageResultPath + fh.extractFileName(file) + '.png', im)
Ejemplo n.º 5
0
def splitTile(tilePath, tileSize, splitTileSize, destinationPath):
    resizedTiffPath = 'resizeTiffTmp.tif'
    GeotiffHelper.resizeTiff(tilePath, resizedTiffPath, 3)
    tifInfo = getTifInfo(resizedTiffPath)
    pixelRes = tifInfo['pixelResolution']
    topLeftGps = tifInfo['topLeftCoordinate']
    im = readImage(resizedTiffPath)
    imgheight = im.shape[0]
    imgwidth = im.shape[1]
    x = 0
    y = 0
    for y in range(0, imgheight, splitTileSize):
        for x in range(0, imgwidth, splitTileSize):
            nextIm = im[y:y + splitTileSize, x:x + splitTileSize]
            lat = topLeftGps[0] + pixelRes[1] * y
            long = topLeftGps[1] + pixelRes[0] * x
            tempGps = str(lat) + '_' + str(long)
            cv2.imwrite(
                destinationPath + str(pixelRes[0]) + '_' + tempGps + ".png",
                nextIm)
Ejemplo n.º 6
0
def checkPreImage(prePath):
    isOk = True
    for file in os.listdir(prePath):
        fileExt = fh.extractFileExtension(file)
        if fileExt == '.tiff' or fileExt == '.tif':
            tifInfo = None
            try:
                tifInfo = gh.getTifInfo(prePath + file)
            except Exception as e:
                #Should log the exeception
                print('Cannot obtains GeoTransform from %s file' % (file))
                sys.exit()
        else:
            print('Image %s is not a tiff file' % (file))
            sys.exit()
Ejemplo n.º 7
0
def extractTile(tiffFile, xTile, yTile, tileSize, destinationPath):
    x = (xTile - 1) * tileSize
    y = (yTile - 1) * tileSize
    return GeotiffHelper.extractSubImage(tiffFile, destinationPath, x, y,
                                         TILE_PIXEL, TILE_PIXEL)
Ejemplo n.º 8
0
def createGridPreview(filePath):
    GeotiffHelper.createGridPreview(filePath, Global.GRID_PREVIEW_PATH,
                                    TILE_PIXEL)
Ejemplo n.º 9
0
def createPreview(filePath):
    GeotiffHelper.createPreview(filePath, Global.PREVIEW_PATH)
Ejemplo n.º 10
0
def extractBuildingImage(csvBuildingPath, preDisasterPath, postDisasterPath,
                         imageResultPath):
    data = fh.csvToDict(csvBuildingPath)

    preTifInfo = getTifInfo(preDisasterPath)
    postTifInfo = getTifInfo(postDisasterPath)

    preResolution = preTifInfo['pixelResolution'][0]
    postResolution = postTifInfo['pixelResolution'][0]
    preTopLeft = preTifInfo['topLeftCoordinate']
    postTopLeft = postTifInfo['topLeftCoordinate']

    preIm = readImage(preDisasterPath)
    postIm = readImage(postDisasterPath)
    predictionsToAdd = []
    for line in data:
        if 'BuildingPrediction' in line:
            damPred = json.loads(line['BuildingPrediction'])
            id = damPred['Id']
            bBox = BoundingBoxGPS(damPred['BoundingBox']['lat1'],
                                  damPred['BoundingBox']['long1'],
                                  damPred['BoundingBox']['lat2'],
                                  damPred['BoundingBox']['long2'])
            buildPred = BuildingPrediction(damPred['Id'], bBox)

            prePt = GeotiffHelper.gpsBoundingBoxToPixelArray(
                bBox, preTopLeft[0], preTopLeft[1], preResolution)
            buildingWidth = prePt[2] - prePt[0]
            buildingHeight = prePt[3] - prePt[1]

            x = max(0, prePt[1] - 25)
            y = max(0, prePt[0] - 25)
            # GeotiffHelper.extractSubImage(preDisasterPath, imageResultPath, x, y, buildingWidth+50, buildingHeight+50)
            building_img = GeotiffHelper.extractSubImageToArray(
                preIm, x, y, buildingWidth + 50, buildingHeight + 50)
            building_img2 = GeotiffHelper.extractSubImageToArray(
                postIm, x, y, buildingWidth + 50, buildingHeight + 50)

            preName = str(id) + '_A' + '.png'
            postName = str(id) + '_B' + '.png'
            predictionsToAdd.append([buildPred.toJSON(), preName])
            predictionsToAdd.append([buildPred.toJSON(), postName])
            # imageResult.show()
            # imageResult.savefig(resultPath)

            fh.arrayToCsv(Global.TRAIN_DATASET_PATH + 'label.csv',
                          predictionsToAdd)

            blackImg = np.zeros((300, 300, 3), np.uint8)
            blackImg2 = np.zeros((300, 300, 3), np.uint8)
            blackImg[0:buildingHeight + 50,
                     0:buildingWidth + 50] = building_img
            blackImg2[0:buildingHeight + 50,
                      0:buildingWidth + 50] = building_img2

            # cv2.imwrite(imageResultPath+str(id)+'_A' + '.png', blackImg)
            # cv2.imwrite(imageResultPath+str(id)+'_B' + '.png', blackImg2)

            cv2.imwrite(
                Global.TRAIN_DATASET_PRE_PATH + str(id) + '_A' + '.png',
                blackImg)
            cv2.imwrite(
                Global.TRAIN_DATASET_PRE_PATH + str(id) + '_B' + '.png',
                blackImg)

            cv2.imwrite(
                Global.TRAIN_DATASET_POST_PATH + str(id) + '_A' + '.png',
                blackImg)
            cv2.imwrite(
                Global.TRAIN_DATASET_POST_PATH + str(id) + '_B' + '.png',
                blackImg2)
Ejemplo n.º 11
0
def getTifInfo(tifPath):
    return GeotiffHelper.getTifInfo(tifPath)