Skip to content

Commit

Permalink
push changes to develop version
Browse files Browse the repository at this point in the history
  • Loading branch information
patel-zeel committed Oct 7, 2024
1 parent 8dc59cf commit 7885e50
Show file tree
Hide file tree
Showing 17 changed files with 1,764 additions and 1,046 deletions.
3 changes: 3 additions & 0 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## To generate `README.md` file

Run the command `python README.py` in the terminal. This will generate the `README.md` file.
57 changes: 57 additions & 0 deletions README.jinja2
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<!-- align h1 to center -->
<h1 align="center">
Garuda
</h1>

<p align="center">
<img src="logo/garuda_profile_full1.png" width="100%">
</p>
<p align="center">
A research-oriented computer vision library for satellite imagery.
</p>

[![Coverage Status](https://coveralls.io/repos/github/patel-zeel/garuda/badge.svg?branch=main)](https://coveralls.io/github/patel-zeel/garuda?branch=main)

## Installation

Stable version:

```bash
pip install garuda
```

Latest version:

```bash
pip install git+https://github.com/patel-zeel/garuda
```

## Usage

See the [examples](examples) directory for more details.

## Functionality

### Configuration

#### Disable/Enable TQDM

```python
{{ config_tqdm }}
```

Output:
```python
{{ config_tqdm_output }}
```

#### Adjust log level

```python
{{ config_log_level }}
```

Output:
```python
{{ config_log_level_output }}
```
176 changes: 52 additions & 124 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,155 +15,83 @@
## Installation

Stable version:

```bash
pip install garuda
```

Latest version:

```bash
pip install git+https://github.com/patel-zeel/garuda
```

## Non-circular imports
* core -> box
* box -> utils
* core + box -> annotate

## Terminology

| Term | Description |
| -------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Local image co-ordinates | (x, y) where x is the column number and y is the row number of an image. Origin is at the top-left corner. |
| Web Mercator (webm) pixel co-ordinates | (x, y) pixel co-ordinates as described on [Google Maps Developer Documentation](https://developers.google.com/maps/documentation/javascript/coordinates). |
| Geo co-ordinates | (latitude, longitude) as genereally used in GPS systems. |
| Oriented Bounding Box (OBB) | A rectangular bounding box that is not aligned with the x and y axes. In Ultralytics YOLO format, it is defined as `[label_id, x1, y1, x2, y2, x3, y3, x4, y4]`. |
| Axis-aligned (AA) bounding box | A rectangular bounding box that is aligned with the x and y axes. In Ultralytics YOLO format, it is defined as `[label_id, x_center, y_center, width, height]`. |

## Usage

See the [examples](examples) directory for more details.

## Functionality

### Operations
### Configuration

Convert Ultralytics format of YOLO oriented bounding box to YOLO axis aligned bounding box.
#### Disable/Enable TQDM

```python
import numpy as np
from garuda.ops import obb_to_aa
obb_label_path = "data/labels/obb/22.32,87.93.txt"
aa_label = obb_to_aa(obb_label_path)
# OR
obb_label = np.loadtxt(obb_label_path, ndmin=2)
aa_label = obb_to_aa(obb_label)
from garuda.config import enable_tqdm, disable_tqdm
enable_tqdm()
disable_tqdm()
```

Convert local image pixel coordinates to geo coordinates (latitude, longitude).

Output:
```python
from garuda.ops import local_to_geo
img_x, img_y = 100, 100
zoom = 17
img_center_lat, img_center_lon = 22.32, 87.93
img_width, img_height = 1120, 1120
geo_coords = local_to_geo(img_x, img_y, zoom, img_center_lat, img_center_lon, img_width, img_height)
GARUDA INFO : TQDM progress bar enabled
GARUDA INFO : TQDM progress bar disabled
```

Convert geo coordinates (latitude, longitude) to global image pixel coordinates in Web Mercator projection at a given zoom level.
#### Adjust log level

```python
from garuda.ops import geo_to_webm_pixel
lat, lon = 22.32, 87.93
zoom = 17
webm_x, webm_y = geo_to_webm_pixel(lat, lon, zoom)
from garuda.config import set_log_level
from garuda.base import logger

def _log_everything():
logger.debug('Debug message')
logger.info('Info message')
logger.warning('Warning message')
logger.error('Error message')
logger.critical('Critical message')
set_log_level('DEBUG')
_log_everything()
set_log_level('INFO')
_log_everything()
set_log_level('WARNING')
_log_everything()
set_log_level('ERROR')
_log_everything()
set_log_level('CRITICAL')
_log_everything()
```

Convert global image pixel coordinates in Web Mercator projection to geo coordinates (latitude, longitude) at a given zoom level.

Output:
```python
from garuda.ops import webm_pixel_to_geo
x, y = 100, 100
lat, lon = webm_pixel_to_geo(x, y, zoom)
```

### Object Detection in Satellite Imagery

Convert center of a YOLO axis-aligned or oriented bounding box to geo coordinates (latitude, longitude).

```python
from garuda.od import yolo_aa_to_geo # for axis aligned bounding box
from garuda.od import yolo_obb_to_geo # for oriented bounding box
yolo_aa_label = "data/labels/aa/22.32,87.93.txt"
yolo_obb_label = "data/labels/obb/22.32,87.93.txt"
zoom = 17
img_center_lat, img_center_lon = 22.32, 87.93
img_width, img_height = 1120, 1120

# For axis aligned bounding box
geo_coords = yolo_aa_to_geo(yolo_aa_label, zoom, img_center_lat, img_center_lon, img_width, img_height)

# For oriented bounding box
geo_coords = yolo_obb_to_geo(yolo_obb_label, zoom, img_center_lat, img_center_lon, img_width, img_height)
```

Convert label studio "CSV" bounding boxes to YOLO format.
```python
import pandas as pd
from garuda.od import add_obb_to_label_studio_df
df = pd.read_csv("data/raw/22.32,87.93.csv")
label_map = {"FCBK": 0, "Zigzag": 1, "ZIGZAG": 1}
df = add_obb_to_label_studio_df(df, label_map) # obb added to the dataframe in "obb" column
print(df['obb'].iloc[0].shape)
# (9, 9) # (n, d): n=9 brick kiln bounding boxes with d=9 values each in [label_id, x1, y1, x2, y2, x3, y3, x4, y4] format
```

### Bulk Operations
Please be careful while using the bulk operation functions as they may lead to catastrophic data loss by overwriting some files. To ensure minimum damage, currently we follow these rules:
* No new directory will be created by the functions. All directories must exist before calling the function.
* Every new file written must not exist before calling the function.

#### Label Studio CSV to YOLO Labels
Write YOLO labels in a directory by image names (detected from label-studio CSV file).

```python
import os
import pandas as pd
import tempfile
from garuda.bulk_ops import write_obb_labels_from_label_studio_csv
df = pd.read_csv("data/raw/22.32,87.93.csv")
with tempfile.TemporaryDirectory() as save_dir:
label_map = {"FCBK": 0, "Zigzag": 1, "ZIGZAG": 1}
write_obb_labels_from_label_studio_csv(save_dir, df, label_map)
print(f"Labels written to {save_dir}")
```

#### OBB Labels to AA Labels
Read all OBB labels from a source directory and write them in AA format to a destination directory.

```python
from garuda.bulk_ops import write_aa_labels_from_obb
import tempfile
load_dir = "data/labels/obb"
with tempfile.TemporaryDirectory() as save_dir:
write_aa_labels_from_obb(load_dir, save_dir)
print(f"Labels written to {save_dir}")
```

### Visualization

Plot a satellite image with correct geo-coordinates on the x-axis and y-axis.

```python
from garuda.plot import plot_webm_pixel_to_geo
import matplotlib.pyplot as plt

img = plt.imread('data/images/22.32,87.93.png')

fig, ax = plt.subplots()
ax = plot_webm_pixel_to_geo(img, img_center_lat, img_center_lon, zoom, ax)
```

### Why `OBBLabel` and not `OBBLabels`
* Non-vectorized operations are easier to understand and debug.
* It'd be easy for us to separate out exact false positives and false negatives at a single object level.
Log level set to DEBUG
GARUDA DEBUG : Debug message
GARUDA INFO : Info message
GARUDA WARNING : Warning message
GARUDA ERROR : Error message
GARUDA CRITICAL : Critical message
Log level set to INFO
GARUDA INFO : Info message
GARUDA WARNING : Warning message
GARUDA ERROR : Error message
GARUDA CRITICAL : Critical message
Log level set to WARNING
GARUDA WARNING : Warning message
GARUDA ERROR : Error message
GARUDA CRITICAL : Critical message
Log level set to ERROR
GARUDA ERROR : Error message
GARUDA CRITICAL : Critical message
Log level set to CRITICAL
GARUDA CRITICAL : Critical message
```
54 changes: 54 additions & 0 deletions README.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import io
import ast
import jinja2
import contextlib
from README_codes import *
import logging
from garuda.base import logger, log_format

# Read the README codes
with open("README_codes.py", "r") as f:
readme_codes = ast.parse(f.read())

function_body_dict = {}
output_dict = {}
for node in ast.walk(readme_codes):
if isinstance(node, ast.FunctionDef) and not node.name.startswith("_"):
function_code = ast.unparse(node)
function_body_code = ast.unparse(node.body)
function_body_dict[node.name] = function_body_code.strip()

# Get the function
# exec(compile(ast.parse(function_code), filename="<ast>", mode="exec"))
function = locals()[node.name]

output_stream = io.StringIO()

# Set up a custom logging handler
handler = logging.StreamHandler(output_stream)
handler.setFormatter(logging.Formatter(log_format))
logger.addHandler(handler)

# Execute it and capture the output
with contextlib.redirect_stdout(output_stream), contextlib.redirect_stderr(output_stream):
function()

# Save the output
output = output_stream.getvalue()
output_dict[f"{node.name}_output"] = output.strip()

# Read the README template
template_path = "README.jinja2"
with open(template_path, "r") as f:
template = f.read()
template = jinja2.Template(template)

# Render the template
kwargs = {**function_body_dict, **output_dict}
rendered = template.render(**kwargs)

# Write the README
with open("README.md", "w") as f:
f.write(rendered)

print(rendered)
32 changes: 32 additions & 0 deletions README_codes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
def config_tqdm():
from garuda.config import enable_tqdm, disable_tqdm

enable_tqdm() # Enable tqdm progress bar across the library
disable_tqdm() # Disable tqdm progress bar across the library

def config_log_level():
from garuda.config import set_log_level
from garuda.base import logger

def _log_everything():
logger.debug("Debug message")
logger.info("Info message")
logger.warning("Warning message")
logger.error("Error message")
logger.critical("Critical message")

# default log level is INFO
set_log_level("DEBUG")
_log_everything()

set_log_level("INFO")
_log_everything()

set_log_level("WARNING")
_log_everything()

set_log_level("ERROR")
_log_everything()

set_log_level("CRITICAL")
_log_everything()
4 changes: 2 additions & 2 deletions garuda/annotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import geojson
from ipywidgets import Button, Label, HBox, Dropdown, SelectionSlider, RadioButtons
from IPython.display import display
from garuda.core import geo_to_webm_pixel, webm_pixel_to_geo, xywhr2xyxyxyxy
from garuda.base import geo_to_webm_pixel, webm_pixel_to_geo, xywhr2xyxyxyxy
from garuda.box import OBBLabel
from shapely.geometry import Polygon

Expand Down Expand Up @@ -197,7 +197,7 @@ def submit_button_clicked(self, *args, **kwargs):
self.cache_label()
self.show_current_label()
self.enable_buttons()
# self.next_button_clicked()
self.next_button_clicked()

def cache_label(self):
cache_path = f"{self.cache_dir}/label_{self.index}.geojson"
Expand Down
Loading

0 comments on commit 7885e50

Please sign in to comment.