Skip to content

Commit

Permalink
Merge pull request #30 from AgPipeline/alpha_channel
Browse files Browse the repository at this point in the history
Alpha channel support
  • Loading branch information
Chris-Schnaufer authored Oct 15, 2020
2 parents 3f0bb9a + 89b7386 commit dee83cf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
10 changes: 3 additions & 7 deletions canopycover.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,8 @@ def calculate_canopycover_masked(pxarray: np.ndarray) -> float:
Notes:
From TERRA REF canopy cover: https://github.com/terraref/extractors-stereo-rgb/tree/master/canopycover
"""
# If > 75% is NoData, return a -1 ccvalue for omission later
total_size = pxarray.shape[0] * pxarray.shape[1]
nodata = np.count_nonzero(pxarray[:, :, 3] == 0)
nodata_ratio = nodata/float(total_size)
if nodata_ratio > 0.75:
return -1

# For masked images, all pixels with rgb>0,0,0 are considered canopy
data = pxarray[pxarray[:, :, 3] == 255]
Expand Down Expand Up @@ -356,8 +352,6 @@ def perform_process(self, environment: Environment, check_md: dict, transformer_
else:
logging.info('Adding missing alpha channel to loaded image from "%s"', one_file)
image_to_use = _add_image_mask(one_file)
# mask = np.where(np.sum(pxarray, axis=0) == 0, 0, 255).astype(pxarray.dtype)
# image_to_use = np.stack((pxarray[0], pxarray[1], pxarray[2], mask))
del pxarray # Potentially free up memory

logging.debug("Calculating canopy cover")
Expand Down Expand Up @@ -389,6 +383,8 @@ def perform_process(self, environment: Environment, check_md: dict, transformer_
else:
continue
except Exception as ex:
if logging.getLogger().level == logging.DEBUG:
logging.exception("Exception caught while processing canopy")
logging.warning("Exception caught while processing canopy cover: %s", str(ex))
logging.warning("Error generating canopy cover for '%s'", one_file)
logging.warning(" plot name: '%s'", plot_name)
Expand All @@ -398,7 +394,7 @@ def perform_process(self, environment: Environment, check_md: dict, transformer_
if not num_files:
return {'code': -1000, 'error': "No files were processed"}
if not total_plots_calculated:
return {'code': -1001, 'error': "No plots intersected with the images provided"}
return {'code': -1001, 'error': "No images were able to have their canopy cover calculated"}

# Setup the metadata for returning files
file_md = []
Expand Down
12 changes: 8 additions & 4 deletions tests/test_canopy_cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import os
import random
import re
import shutil
import string
import tempfile
from shutil import rmtree
from subprocess import getstatusoutput
#import pytest
Expand Down Expand Up @@ -53,15 +55,17 @@ def test_no_args():
changed to some non-zero value to indicate a failure.
"""
ret_val, out = getstatusoutput(SOURCE_PATH)
assert ret_val == 0
assert re.search('{}', out)
assert ret_val == 1
assert re.search('FileNotFoundError', out)


def test_no_metadata():
""" Run with a file but no metadata"""
ret_val, out = getstatusoutput(f'{SOURCE_PATH} {INPUT1}')
temp_dir = tempfile.mkdtemp()
ret_val, out = getstatusoutput(f'{SOURCE_PATH} --working_space {temp_dir} {INPUT1}')
shutil.rmtree(temp_dir)
assert ret_val == 0
assert re.search('{}', out)
assert re.search('"code": 0', out)


def test_get_fields():
Expand Down

0 comments on commit dee83cf

Please sign in to comment.