How to Use Google Earth Engine and Python API to Export Images to Roboflow
This is a guest post written by Ethan Arsht and Raluca Cîrju.
Google Earth Engine is a powerful tool for analyzing and acquiring geographic data. Machine learning experts use Google Earth Engine to identify physical attributes and analyze patterns, in areas including forestry, farmland, ice coverage, and weather. Unfortunately, documentation and examples on using Google Earth Engine, especially via its Python API, are limited. Here, we will show you how to use Google Earth Engine to acquire Roboflow-ready images from the Sentinel-2 satellite straight to your Google Drive.
Sign Up for Google Earth Engine
First, you will need to authenticate your information with Google Earth Engine. You can provide your authentication information here. Typically, it will take approximately 10 minutes before you receive your account information.
Connecting to Google Earth Engine
Once you have your account information, you have two ways to access Google Earth Engine.
- First, Google Earth Engine has a built-in IDE which you can access here. This interface allows users to write JavaScript code to access Google Earth Engine. If your script has a visual output, it will appear directly on the world map included in the IDE.
- However, we will focus on the second way to access Google Earth Engine: the Python API.
Python API Access to Google Earth Engine
From here, you can follow along in this notebook.
We start by installing the accompanying earthengine
package and authenticate to the Earth Engine API (code cell [2] in notebook). We will then follow a link provided by Google and accept certain permissions to acquire our authentication code. We will also authenticate to Google Drive, using a similar process (cell [3]).
Before we go further in exporting images, we have to understand how the underlying data in Google Earth functions.
- Google Earth (and Google Earth Engine) is built on photographs from satellites and airplanes.
- Each one of these photographs, or tiles, covers part of the Earth.
- When we view Google Earth in the browser, we are viewing these tiles aligned with one another.
However, when using Google Earth Engine, we need to specify which tile we want to export before proceeding with the export. You can read more about the data in Google Earth Engine here.
For this example, we will export an image of Des Moines, Iowa – the birthplace of Roboflow! Our goal is to find a Sentinel-2 tile that includes Des Moines, is relatively recent, and has the fewest clouds.
To do so, we write one line of code with the following steps:
- Specify that we want to use Sentinel tiles.
- Filter to Sentinel tiles that cover a point in the center of Des Moines.
- Filter to tiles that were photographed by Sentinel satellites in the year 2020.
- Select the tiles with the least cloudiness, so we have the clearest possible image.
tile = ee.ImageCollection('COPERNICUS/S2_SR')\
.filterBounds(area_of_interest)\
.filterDate('2020-01-01', '2020-12-31')\
.sort('CLOUDY_PIXEL_PERCENTAGE')\
.first()
This provides us with one image (cell [6] in notebook). From there, we can specify a region and export it to a raster. However, this image contains several layers, as shown below:
We will export the raw image of the Earth’s surface, which means we need to work with bands 2 (blue), 3 (green), and 4 (red). With these three bands, an image viewer can display a color image (cell [7]).
Finally, we specify the region we want to export (cell [8]). We don’t need the entire tile, so we create a bounding box that specifies the coordinates for the city of Des Moines. With that, Google Earth Engine exports only the area that we want, rather than the entire photograph from Sentinel-2.
Export Images to Google Drive
We’re ready to export. The export will take place on Google Earth Engine’s server, but we can interface with the server from Python. The export job has several parameters (cell [9]). Note that, because the job runs on Google Earth’s servers, we may not receive an error message if there are any problems.
To complete the export, we execute the code on Google Earth’s servers and wait for it to become inactive (cell [10]). Then we can check out our image in Google Drive. If you’ve followed the steps in the notebook, it should look something like this:
And that's it! Following the above steps and linked notebook, you should be able to use Google Earth Engine to generate satellite images. These images are ready to read directly into Roboflow – just upload those files directly!