Skip to content

Commit

Permalink
Merge branch 'feature/feedback_v1' of https://github.com/hotosm/fAIr
Browse files Browse the repository at this point in the history
…into feature/feedback_v1
  • Loading branch information
omranlm committed Aug 29, 2023
2 parents 1c71fac + 0189f00 commit b5f00b8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
10 changes: 9 additions & 1 deletion backend/core/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
FeedbackLabelFileSerializer,
LabelFileSerializer,
)
from core.utils import bbox, download_imagery, get_start_end_download_coords
from core.utils import (
bbox,
download_imagery,
get_start_end_download_coords,
is_dir_empty,
)
from django.conf import settings
from django.contrib.gis.db.models.aggregates import Extent
from django.contrib.gis.geos import GEOSGeometry
Expand Down Expand Up @@ -106,8 +111,11 @@ def train_model(
base_path=training_input_image_source,
source=source_imagery,
)

except Exception as ex:
raise ex
if is_dir_empty(training_input_image_source):
raise ValueError("No images found in the area")

## -----------LABEL GENERATOR---------
logging.info("Label Generator started")
Expand Down
14 changes: 13 additions & 1 deletion backend/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,24 @@ def get_start_end_download_coords(bbox_coords, zm_level, tile_size):
import logging


def is_dir_empty(directory_path):
return not any(os.scandir(directory_path))


def download_image(url, base_path, source_name):
response = requests.get(url)

# Check if the URL request was successful
if response.status_code != 200:
raise ValueError(
f"Failed to download image from {url}. Status code: {response.status_code}"
)

image = response.content
url = re.sub(r"\.(png|jpeg)$", "", url)

url = re.sub(r"\.(png|jpeg)$", "", url)
url_splitted_list = url.split("/")

filename = f"{base_path}/{source_name}-{url_splitted_list[-2]}-{url_splitted_list[-1]}-{url_splitted_list[-3]}.png"

with open(filename, "wb") as f:
Expand Down
3 changes: 3 additions & 0 deletions backend/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
get_dir_size,
get_start_end_download_coords,
gpx_generator,
is_dir_empty,
process_rawdata,
request_rawdata,
)
Expand Down Expand Up @@ -532,6 +533,8 @@ def post(self, request, *args, **kwargs):
)
prediction_output = f"{temp_path}/prediction/output"
print("Image Downloaded , Starting Inference")
if is_dir_empty(temp_path):
return Response("No Images found", status=500)
start_time = time.time()
model_path = os.path.join(
settings.TRAINING_WORKSPACE,
Expand Down

0 comments on commit b5f00b8

Please sign in to comment.