def scan(x, y): if hasData((x, y, x+1, y+1)): return img = imagery.fetchTile(x,y,zoomlevel) if img == None: print ('failed to fetch', x, y) if img != None: try: file_jpgdata = StringIO(img) i = Image.open(file_jpgdata) except IOError: return False i = i.resize((cnn.img_width, cnn.img_height)) arr = image.img_to_array(i) arr = np.expand_dims(arr, axis=0) arr = arr * (1./255) imgs = np.vstack([arr]) classes = cnn.model.predict_classes(imgs, batch_size=10, verbose=0) building = 'true' if classes[0][0] == 1 else 'false' cur.execute("insert into predictions (x, y, has_building) values (%s, %s, %s)",(x, y, building)) conn.commit() print ('scanned', x, y, building) return classes[0][0] == 1
def save(li, building): for (x,y) in li: print (x,y,building) img = imagery.fetchTile(x,y,zoomlevel) if(img != None): file = open("%s/%s_%s.jpg" % (root,x, y), 'w') file.write(img) cur.execute("insert into training_tiles (x, y, has_building, verified ) values (%s, %s, %s, %s)",(x, y, building, building)) conn.commit()
def getMask(startX,startY, zoomlevel): # skip if this is already in our tarining data cur.execute('select * from segmentation_training_tiles where x=%d and y=%d' % (startX, startY)) res = cur.fetchone() if res != None: print 'skip' return # find the lat lng bounding box of the tile endX = startX+1 endY = startY+1 (left,top) = tileMath.tile2deg(startX, startY, zoomlevel) (right,bottom) = tileMath.tile2deg(endX, endY, zoomlevel) # find all buildings in this tile buildings = queryosm("SELECT ST_AsGeoJSON(geometry) FROM building_polygon where geometry && ST_MakeEnvelope(%s, %s, %s, %s, 4326)" % (right, bottom, left, top)) # size in degrees of each side of the tile. x and y may not be the same xLength = abs(left-right) yLength = abs(top-bottom) # create a new image to draw our mask on with a black (0,0,0) background img = Image.new("RGB", (256,256), (0,0,0)) drw = ImageDraw.Draw(img, "RGB") # for each building vertex, convert its points to 0-255 coordinates on the image # and draw them as a white poly for rawbuilding in buildings: building = json.loads(rawbuilding[0]) try: buildingCoord = [((buildingX-left)/xLength*255,(top-buildingY)/yLength*255) for (buildingX,buildingY) in building["coordinates"][0]] drw.polygon(buildingCoord, fill=(255,255,255)) except ValueError: print ("could not fetch", startX, startY) # save the output to our masks directory, and download the tile from naip img.save("%s/%s_%s.jpg" % (maskDir,startX,startY)) try: realImg = imagery.fetchTile(startX,startY,zoomlevel) file_jpgdata = StringIO(realImg) i = Image.open(file_jpgdata).convert('RGB') i.save("%s/%s_%s.jpg" % (tileDir,startX,startY)) except TypeError: print ("failed to load tile", startX, startY) # mark it as saved in segmentation_training_tiles, but not verified cur.execute("insert into segmentation_training_tiles (x, y, verified ) values (%s, %s, %s)",(startX, startY, False)) conn.commit() print(startX, startY)
def getMask(startX, startY, zoomlevel): cur.execute( 'select * from segmentation_training_tiles where x=%d and y=%d' % (startX, startY)) res = cur.fetchone() if res != None: print 'skip' return endX = startX + 1 endY = startY + 1 (left, top) = tileMath.tile2deg(startX, startY, zoomlevel) (right, bottom) = tileMath.tile2deg(endX, endY, zoomlevel) buildings = queryosm( "SELECT ST_AsGeoJSON(geometry) FROM building_polygon where geometry && ST_MakeEnvelope(%s, %s, %s, %s, 4326)" % (right, bottom, left, top)) xLength = abs(left - right) yLength = abs(top - bottom) img = Image.new("RGB", (256, 256), (0, 0, 0)) drw = ImageDraw.Draw(img, "RGB") for rawbuilding in buildings: building = json.loads(rawbuilding[0]) try: buildingCoord = [((buildingX - left) / xLength * 255, (top - buildingY) / yLength * 255) for (buildingX, buildingY) in building["coordinates"][0]] drw.polygon(buildingCoord, fill=(255, 255, 255)) except ValueError: print("could not fetch", startX, startY) img.save("%s/%s_%s.jpg" % (maskDir, startX, startY)) try: realImg = imagery.fetchTile(startX, startY, zoomlevel) file_jpgdata = StringIO(realImg) i = Image.open(file_jpgdata).convert('RGB') i.save("%s/%s_%s.jpg" % (tileDir, startX, startY)) except TypeError: print("failed to load tile", startX, startY) cur.execute( "insert into segmentation_training_tiles (x, y, verified ) values (%s, %s, %s)", (startX, startY, False)) conn.commit() print(startX, startY)
def contours(x,y): z=17 (startX, startY) = tileMath.tile2deg(x,y,z) (endX, endY) = tileMath.tile2deg(x+1,y+1,z) dx = endX-startX dy = endY-startY img = imagery.fetchTile(x,y,z) file_jpgdata = StringIO(img) i = Image.open(file_jpgdata) out = predict.predictMask(i) contours = predict.getContours(out) realContours = [ [[((float(x)/255)*dx)+startX, ((float(y)/255)*dy)+startY] for [x,y] in pointset] for pointset in contours] return jsonify(realContours)
def contours(x, y): z = 17 # get lat lng boundaries and size of tile (startX, startY) = tileMath.tile2deg(x, y, z) (endX, endY) = tileMath.tile2deg(x + 1, y + 1, z) dx = endX - startX dy = endY - startY # download tile img = imagery.fetchTile(x, y, z) file_jpgdata = StringIO(img) i = Image.open(file_jpgdata) #get image mask and contours out = predict.predictMask(i) contours = predict.getContours(out) # transform contours from pixel space to lat lng space realContours = [[[((float(x) / 255) * dx) + startX, ((float(y) / 255) * dy) + startY] for [x, y] in pointset] for pointset in contours] return jsonify(realContours)