aletrn commited on
Commit
b241742
·
1 Parent(s): 6d1f220

[bug] fix wrong latlng to xy pixel conversion, update .gitignore and .dockerignore

Browse files
.dockerignore CHANGED
@@ -4,4 +4,5 @@ __cache__
4
  .idea
5
  tmp/
6
  .env*
7
- __pycache__
 
 
4
  .idea
5
  tmp/
6
  .env*
7
+ __pycache__
8
+ .DS_Store
.gitignore CHANGED
@@ -4,4 +4,5 @@ __cache__
4
  .idea
5
  tmp/
6
  .env*
7
- *.onnx
 
 
4
  .idea
5
  tmp/
6
  .env*
7
+ *.onnx
8
+ .DS_Store
src/app.py CHANGED
@@ -1,15 +1,17 @@
1
  import json
2
  import time
3
  from http import HTTPStatus
 
4
  from typing import Dict
5
 
6
  from aws_lambda_powertools.event_handler import content_types
7
  from aws_lambda_powertools.utilities.typing import LambdaContext
8
 
9
  from src import app_logger
10
- from src.io.coordinates_pixel_conversion import get_latlng_to_pixel_coordinates
11
  from src.prediction_api.predictors import samexporter_predict
12
- from src.utilities.constants import CUSTOM_RESPONSE_MESSAGES
 
13
  from src.utilities.utilities import base64_decode
14
 
15
 
@@ -57,20 +59,29 @@ def get_parsed_bbox_points(request_input: Dict) -> Dict:
57
  for prompt in request_input["prompt"]:
58
  app_logger.info(f"current prompt: {type(prompt)}, value:{prompt}.")
59
  data = prompt["data"]
60
- app_logger.info(f"current data points: {type(data)}, value:{data}.")
61
- data_ne = data["ne"]
62
- app_logger.info(f"current data_ne point: {type(data_ne)}, value:{data_ne}.")
63
- data_sw = data["sw"]
64
- app_logger.info(f"current data_sw point: {type(data_sw)}, value:{data_sw}.")
65
-
66
- diff_pixel_coords_origin_data_ne = get_latlng_to_pixel_coordinates(ne, data_ne, zoom, "ne")
67
- app_logger.info(f'current diff prompt ne: {type(data)}, {data} => {diff_pixel_coords_origin_data_ne}.')
68
- diff_pixel_coords_origin_data_sw = get_latlng_to_pixel_coordinates(ne, data_sw, zoom, "sw")
69
- app_logger.info(f'current diff prompt sw: {type(data)}, {data} => {diff_pixel_coords_origin_data_sw}.')
70
- prompt["data"] = [
71
- diff_pixel_coords_origin_data_ne["x"], diff_pixel_coords_origin_data_ne["y"],
72
- diff_pixel_coords_origin_data_sw["x"], diff_pixel_coords_origin_data_sw["y"]
73
- ]
 
 
 
 
 
 
 
 
 
74
 
75
  app_logger.info(f"bbox => {bbox}.")
76
  app_logger.info(f'## request_input["prompt"] updated => {request_input["prompt"]}.')
@@ -110,9 +121,13 @@ def lambda_handler(event: dict, context: LambdaContext):
110
  app_logger.info(f"body, #2: {type(body)}, {body}...")
111
 
112
  try:
 
 
113
  body_request = get_parsed_bbox_points(body)
114
  app_logger.info(f"body_request=> {type(body_request)}, {body_request}.")
115
- body_response = samexporter_predict(body_request["bbox"], body_request["prompt"], body_request["zoom"])
 
 
116
  app_logger.info(f"output body_response:{body_response}.")
117
  response = get_response(HTTPStatus.OK.value, start_time, context.aws_request_id, body_response)
118
  except Exception as ex2:
 
1
  import json
2
  import time
3
  from http import HTTPStatus
4
+ from pathlib import Path
5
  from typing import Dict
6
 
7
  from aws_lambda_powertools.event_handler import content_types
8
  from aws_lambda_powertools.utilities.typing import LambdaContext
9
 
10
  from src import app_logger
11
+ from src.io.coordinates_pixel_conversion import get_latlng_to_pixel_coordinates, get_point_latlng_to_pixel_coordinates
12
  from src.prediction_api.predictors import samexporter_predict
13
+ from src.utilities.constants import CUSTOM_RESPONSE_MESSAGES, ROOT
14
+ from src.utilities.serialize import serialize
15
  from src.utilities.utilities import base64_decode
16
 
17
 
 
59
  for prompt in request_input["prompt"]:
60
  app_logger.info(f"current prompt: {type(prompt)}, value:{prompt}.")
61
  data = prompt["data"]
62
+ if prompt["type"] == "rectangle":
63
+ app_logger.info(f"current data points: {type(data)}, value:{data}.")
64
+ data_ne = data["ne"]
65
+ app_logger.info(f"current data_ne point: {type(data_ne)}, value:{data_ne}.")
66
+ data_sw = data["sw"]
67
+ app_logger.info(f"current data_sw point: {type(data_sw)}, value:{data_sw}.")
68
+
69
+ diff_pixel_coords_origin_data_ne = get_latlng_to_pixel_coordinates(ne, sw, data_ne, zoom, "ne")
70
+ app_logger.info(f'current diff prompt ne: {type(data)}, {data} => {diff_pixel_coords_origin_data_ne}.')
71
+ diff_pixel_coords_origin_data_sw = get_latlng_to_pixel_coordinates(ne, sw, data_sw, zoom, "sw")
72
+ app_logger.info(f'current diff prompt sw: {type(data)}, {data} => {diff_pixel_coords_origin_data_sw}.')
73
+ prompt["data"] = [
74
+ diff_pixel_coords_origin_data_ne["x"], diff_pixel_coords_origin_data_ne["y"],
75
+ diff_pixel_coords_origin_data_sw["x"], diff_pixel_coords_origin_data_sw["y"]
76
+ ]
77
+ elif prompt["type"] == "point":
78
+ current_point = get_latlng_to_pixel_coordinates(ne, sw, data, zoom, "point")
79
+ app_logger.info(f"current prompt: {type(current_point)}, value:{current_point}.")
80
+ new_prompt_data = [current_point['x'], current_point['y']]
81
+ app_logger.info(f"new_prompt_data: {type(new_prompt_data)}, value:{new_prompt_data}.")
82
+ prompt["data"] = new_prompt_data
83
+ else:
84
+ raise ValueError("valid prompt types are only 'point' and 'rectangle'")
85
 
86
  app_logger.info(f"bbox => {bbox}.")
87
  app_logger.info(f'## request_input["prompt"] updated => {request_input["prompt"]}.')
 
121
  app_logger.info(f"body, #2: {type(body)}, {body}...")
122
 
123
  try:
124
+ prompt_latlng = body["prompt"]
125
+ app_logger.info(f"prompt_latlng:{prompt_latlng}.")
126
  body_request = get_parsed_bbox_points(body)
127
  app_logger.info(f"body_request=> {type(body_request)}, {body_request}.")
128
+ body_response = samexporter_predict(
129
+ body_request["bbox"], body_request["prompt"], body_request["zoom"], prompt_latlng
130
+ )
131
  app_logger.info(f"output body_response:{body_response}.")
132
  response = get_response(HTTPStatus.OK.value, start_time, context.aws_request_id, body_response)
133
  except Exception as ex2:
src/io/coordinates_pixel_conversion.py CHANGED
@@ -45,17 +45,16 @@ def get_point_latlng_to_pixel_coordinates(latlng, zoom: int) -> PixelCoordinate:
45
  raise e_format_latlng_to_pixel_coordinates
46
 
47
 
48
- def get_latlng_to_pixel_coordinates(latlng_origin, latlng_current_point, zoom, k: str):
49
- # latlng_origin_list = get_latlng_coords_list(latlng_origin, k)
50
- # latlng_current_point_list = get_latlng_coords_list(latlng_current_point, k)
51
- app_logger.info(f"latlng_origin - {k}: {type(latlng_origin)}, value:{latlng_origin}.")
52
  app_logger.info(f"latlng_current_point - {k}: {type(latlng_current_point)}, value:{latlng_current_point}.")
53
- latlng_map_origin = get_point_latlng_to_pixel_coordinates(latlng_origin, zoom)
 
54
  latlng_map_current_point = get_point_latlng_to_pixel_coordinates(latlng_current_point, zoom)
55
- diff_coord_x = abs(latlng_map_origin["x"] - latlng_map_current_point["x"])
56
- diff_coord_y = abs(latlng_map_origin["y"] - latlng_map_current_point["y"])
57
  point = PixelCoordinate(x=diff_coord_x, y=diff_coord_y)
58
- app_logger.info(f"point - {k}: {point}.")
59
  return point
60
 
61
 
 
45
  raise e_format_latlng_to_pixel_coordinates
46
 
47
 
48
+ def get_latlng_to_pixel_coordinates(latlng_origin_ne, latlng_origin_sw, latlng_current_point, zoom, k: str):
49
+ app_logger.info(f"latlng_origin - {k}: {type(latlng_origin_ne)}, value:{latlng_origin_ne}.")
 
 
50
  app_logger.info(f"latlng_current_point - {k}: {type(latlng_current_point)}, value:{latlng_current_point}.")
51
+ latlng_map_origin_ne = get_point_latlng_to_pixel_coordinates(latlng_origin_ne, zoom)
52
+ latlng_map_origin_sw = get_point_latlng_to_pixel_coordinates(latlng_origin_sw, zoom)
53
  latlng_map_current_point = get_point_latlng_to_pixel_coordinates(latlng_current_point, zoom)
54
+ diff_coord_x = abs(latlng_map_origin_sw["x"] - latlng_map_current_point["x"])
55
+ diff_coord_y = abs(latlng_map_origin_ne["y"] - latlng_map_current_point["y"])
56
  point = PixelCoordinate(x=diff_coord_x, y=diff_coord_y)
57
+ app_logger.info(f"point type - {k}: {point}.")
58
  return point
59
 
60
 
src/prediction_api/predictors.py CHANGED
@@ -100,28 +100,27 @@ def samexporter_predict(bbox, prompt: list[dict], zoom: float = ZOOM, model_name
100
  )
101
  app_logger.info(f"saved prediction_masks:{prediction_masks_output}.")
102
 
103
- # mask = np.zeros((prediction_masks.shape[2], prediction_masks.shape[3]), dtype=np.uint8)
104
- # app_logger.info(f"output mask shape:{mask.shape}, {mask.dtype}.")
105
- # ## todo: convert to geojson directly within the loop to avoid merging two objects
106
- # for n, m in enumerate(prediction_masks[0, :, :, :]):
107
- # app_logger.info(f"## {n} mask => m shape:{mask.shape}, {mask.dtype}.")
108
- # mask[m > 0.0] = 255
109
- prediction_masks0 = prediction_masks[0]
110
- app_logger.info(f"prediction_masks0 shape:{prediction_masks0.shape}.")
111
-
112
- try:
113
- pmf = np.sum(prediction_masks0, axis=0).astype(np.uint8)
114
- except Exception as e_sum_pmf:
115
- app_logger.error(f"e_sum_pmf:{e_sum_pmf}.")
116
- pmf = prediction_masks0[0]
117
- app_logger.info(f"creating pil image from prediction mask with shape {pmf.shape}.")
118
- pil_pmf = Image.fromarray(pmf)
119
- pil_pmf_output = f"/tmp/pil_pmf_{pmf.shape[0]}_{pmf.shape[1]}.png"
120
- pil_pmf.save(pil_pmf_output)
121
- app_logger.info(f"saved pil_pmf:{pil_pmf_output}.")
122
-
123
- mask = np.zeros(pmf.shape, dtype=np.uint8)
124
- mask[pmf > 0] = 255
125
 
126
  # cv2.imwrite(f"/tmp/cv2_mask_predicted_{mask.shape[0]}_{mask.shape[1]}_{mask.shape[2]}.png", mask)
127
  pil_mask = Image.fromarray(mask)
 
100
  )
101
  app_logger.info(f"saved prediction_masks:{prediction_masks_output}.")
102
 
103
+ mask = np.zeros((prediction_masks.shape[2], prediction_masks.shape[3]), dtype=np.uint8)
104
+ app_logger.info(f"output mask shape:{mask.shape}, {mask.dtype}.")
105
+ for n, m in enumerate(prediction_masks[0, :, :, :]):
106
+ app_logger.info(f"## {n} mask => m shape:{mask.shape}, {mask.dtype}.")
107
+ mask[m > 0.0] = 255
108
+ # prediction_masks0 = prediction_masks[0]
109
+ # app_logger.info(f"prediction_masks0 shape:{prediction_masks0.shape}.")
110
+ #
111
+ # try:
112
+ # pmf = np.sum(prediction_masks0, axis=0).astype(np.uint8)
113
+ # except Exception as e_sum_pmf:
114
+ # app_logger.error(f"e_sum_pmf:{e_sum_pmf}.")
115
+ # pmf = prediction_masks0[0]
116
+ # app_logger.info(f"creating pil image from prediction mask with shape {pmf.shape}.")
117
+ # pil_pmf = Image.fromarray(pmf)
118
+ # pil_pmf_output = f"/tmp/pil_pmf_{pmf.shape[0]}_{pmf.shape[1]}.png"
119
+ # pil_pmf.save(pil_pmf_output)
120
+ # app_logger.info(f"saved pil_pmf:{pil_pmf_output}.")
121
+ #
122
+ # mask = np.zeros(pmf.shape, dtype=np.uint8)
123
+ # mask[pmf > 0] = 255
 
124
 
125
  # cv2.imwrite(f"/tmp/cv2_mask_predicted_{mask.shape[0]}_{mask.shape[1]}_{mask.shape[2]}.png", mask)
126
  pil_mask = Image.fromarray(mask)