Skip to content

Commit

Permalink
Merge pull request #89 from ai-cfia/69-Retrieve-pictures-and-inferenc…
Browse files Browse the repository at this point in the history
…es-for-a-given-folder

69 retrieve pictures and inferences for a given folder
  • Loading branch information
sylvanie85 authored Aug 1, 2024
2 parents cb204a0 + 2912796 commit 841b97b
Show file tree
Hide file tree
Showing 10 changed files with 641 additions and 22 deletions.
6 changes: 1 addition & 5 deletions datastore/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

load_dotenv()


class UserAlreadyExistsError(Exception):
pass

Expand Down Expand Up @@ -184,8 +183,7 @@ async def create_picture_set(
raise e
except Exception:
raise BlobUploadError("An error occured during the upload of the picture set")



async def get_picture_sets_info(cursor, user_id: str):
"""This function retrieves the picture sets names and number of pictures from the database.
Expand All @@ -205,7 +203,6 @@ async def get_picture_sets_info(cursor, user_id: str):
result[str(picture_set_id)] = [picture_set_name, nb_picture]
return result


async def delete_picture_set_permanently(
cursor, user_id, picture_set_id, container_client
):
Expand Down Expand Up @@ -258,7 +255,6 @@ async def delete_picture_set_permanently(
print(e)
raise Exception("Datastore Unhandled Error")


async def upload_pictures(
cursor, user_id, hashed_pictures, container_client, picture_set_id=None
):
Expand Down
117 changes: 116 additions & 1 deletion datastore/db/metadata/inference/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"""

import json

import datastore.db.queries.seed as seed
import datastore.db.queries.inference as inference
import datastore.db.queries.machine_learning as machine_learning

class MissingKeyError(Exception):
pass
Expand Down Expand Up @@ -74,3 +76,116 @@ def compare_object_metadata(object1:dict , object2:dict) -> bool:
if object1[key] != object2[key]:
return False
return True

def rebuild_inference(cursor, inf) :
"""
This function rebuilds the inference object from the database.
Parameters:
- inference: The inference from the database to convert into inference result.
Returns:
- The inference object as a dict.
"""
inference_id = str(inf[0])
inference_data = json.loads(json.dumps(inf[1]))
pipeline_id = str(inf[2])

models = []
if pipeline_id is not None :
pipeline = machine_learning.get_pipeline(cursor, pipeline_id)
models_data = pipeline["models"]
version = pipeline["version"]
for model_name in models_data :
model = {}
model["name"] = model_name
model["version"] = version
models.append(model)

objects = inference.get_objects_by_inference(cursor, inference_id)
boxes = rebuild_boxes_export(cursor, objects)

inf = {
"boxes": boxes,
"filename": inference_data.get("filename"),
"inference_id": inference_id,
"labelOccurrence" : inference_data.get("labelOccurrence"),
"totalBoxes" : inference_data.get("totalBoxes"),
"models" : models,
"pipeline_id" : pipeline_id,
}
return inf


def rebuild_boxes_export(cursor, objects) :
"""
This function rebuilds the boxes object from the database.
Parameters:
- objects: The objects of an inference from the database to convert into boxes.
Returns:
- The boxes object as an array of dict.
"""
boxes = []
for object in objects:
box_id = str(object[0])

box_metadata = object[1]
box_metadata = json.loads(json.dumps(box_metadata))

box = box_metadata.get("box")
color = box_metadata.get("color")
overlapping = box_metadata.get("overlapping")
overlappingIndices = box_metadata.get("overlappingIndices")

top_id = str(inference.get_inference_object_top_id(cursor, box_id))
top_seed_id = str(seed.get_seed_object_seed_id(cursor, top_id))

seed_objects = inference.get_seed_object_by_object_id(cursor, box_id)
topN = rebuild_topN_export(cursor, seed_objects)

top_score = 0
if inference.is_object_verified(cursor, box_id):
top_score = 1
else :
for seed_object in seed_objects:
score = seed_object[2]
seed_id = str(seed_object[1])
if seed_id == top_seed_id:
top_score = score
top_label = seed.get_seed_name(cursor, top_seed_id)

object = {"box" : box,
"box_id": box_id,
"color" : color,
"label" : top_label,
"object_type_id" : 1,
"overlapping" : overlapping,
"overlappingIndices" : overlappingIndices,
"score" : top_score,
"topN": topN,
"top_id" : top_id}

boxes.append(object)
return boxes


def rebuild_topN_export(cursor, seed_objects) :
"""
This function rebuilds the topN object from the database.
Parameters:
- seed_objects: The seed_objects from the database to convert into topN.
Returns:
- The topN object as an array of dict.
"""
topN = []
for seed_obj in seed_objects :
seed_obj = {
"label": seed.get_seed_name(cursor, str(seed_obj[1])),
"object_id": str(seed_obj[0]),
"score": seed_obj[2]}
topN.append(seed_obj)
return topN
43 changes: 43 additions & 0 deletions datastore/db/queries/inference/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class InferenceObjectNotFoundError(Exception):

class InferenceAlreadyVerifiedError(Exception):
pass

"""
INFERENCE TABLE QUERIES
Expand Down Expand Up @@ -90,6 +91,30 @@ def get_inference(cursor, inference_id: str):
except Exception:
raise InferenceNotFoundError(f"Error: could not get inference {inference_id}")

def get_inference_by_picture_id(cursor, picture_id: str):
"""
This functions retrieve inference of a given picture
Parameters:
- cursor (cursor): The cursor of the database.
- picture_id (str): The UUID of the picture.
"""
try :
query = """
SELECT
id, inference, pipeline_id
FROM
inference
WHERE
picture_id = %s
"""
cursor.execute(query, (picture_id,))
result = cursor.fetchone()
return result
except Exception:
raise InferenceNotFoundError(
f"Error: could not get inference for the picture {picture_id}")

def set_inference_feedback_user_id(cursor, inference_id, user_id):
"""
This function sets the feedback_user_id of an inference.
Expand Down Expand Up @@ -522,3 +547,21 @@ def get_seed_object_id(cursor, seed_id: str, object_id:str):
return res
except Exception:
raise Exception(f"Error: could not get seed_object_id for seed_id {seed_id} for object {object_id}")

def get_seed_object_by_object_id(cursor, object_id: str):
try:
query = """
SELECT
so.id,
so.seed_id,
so.score
FROM
seed_obj so
WHERE
so.object_id = %s
"""
cursor.execute(query, (object_id,))
res = cursor.fetchall()
return res
except Exception:
raise Exception(f"Error: could not get seed_object for object {object_id}")
108 changes: 105 additions & 3 deletions datastore/db/queries/picture/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ def get_picture_set_name(cursor, picture_set_id: str):
id = %s
"""
cursor.execute(query, (picture_set_id, ))
return cursor.fetchone()[0]
name = cursor.fetchone()[0]
return name if name is not None else picture_set_id
except Exception:
raise PictureSetNotFoundError(f"Error: PictureSet not found:{picture_set_id}")

Expand Down Expand Up @@ -331,6 +332,58 @@ def get_validated_pictures(cursor, picture_set_id: str):
return result
except Exception:
raise GetPictureError(f"Error: Error while getting validated pictures for picture_set:{picture_set_id}")

def is_picture_validated(cursor, picture_id: str):
"""
This functions check if a picture is validated. Therefore, there should exists picture_seed entity for this picture.
Parameters:
- cursor (cursor): The cursor of the database.
- picture_id (str): The UUID of the picture to check.
"""
try :
query = """
SELECT EXISTS(
SELECT
1
FROM
picture_seed
WHERE
picture_id = %s
)
"""
cursor.execute(query, (picture_id,))
result = cursor.fetchone()[0]
return result
except Exception:
raise GetPictureError(
f"Error: could not check if the picture {picture_id} is validated")

def check_picture_inference_exist(cursor, picture_id: str):
"""
This functions check whether a picture is associated with an inference.
Parameters:
- cursor (cursor): The cursor of the database.
- picture_id (str): The UUID of the picture to check.
"""
try :
query = """
SELECT EXISTS(
SELECT
1
FROM
inference
WHERE
picture_id = %s
)
"""
cursor.execute(query, (picture_id,))
result = cursor.fetchone()[0]
return result
except Exception:
raise GetPictureError(
f"Error: could not check if the picture {picture_id} has an existing inference")

def change_picture_set_id(cursor, user_id, old_picture_set_id, new_picture_set_id):
"""
Expand All @@ -342,8 +395,10 @@ def change_picture_set_id(cursor, user_id, old_picture_set_id, new_picture_set_i
- picture_set_id (str): The UUID of the PictureSet to retrieve the pictures from.
"""
try :
if get_picture_set_owner_id(cursor, old_picture_set_id) != user_id or get_picture_set_owner_id(cursor, new_picture_set_id) != user_id:
raise PictureUpdateError(f"Error: picture set not own by user :{user_id}")
if get_picture_set_owner_id(cursor, old_picture_set_id) != user_id :
raise PictureUpdateError(f"Error: old picture set not own by user :{user_id}")
if get_picture_set_owner_id(cursor, new_picture_set_id) != user_id :
raise PictureUpdateError(f"Error: new picture set not own by user :{user_id}")

query = """
UPDATE picture
Expand Down Expand Up @@ -440,6 +495,53 @@ def is_a_picture_set_id(cursor, picture_set_id):
except Exception:
raise Exception("unhandled error")

def is_a_picture_id(cursor, picture_id):
"""
This function checks if a picture_id exists in the database.
Parameters:
- cursor (cursor): The cursor of the database.
- picture_id (str): The UUID of the picture to check.
"""
try:
query = """
SELECT EXISTS(
SELECT
1
FROM
picture
WHERE
id = %s
)
"""
cursor.execute(query, (picture_id,))
res = cursor.fetchone()[0]
return res
except Exception:
raise Exception("unhandled error")

def get_picture_picture_set_id(cursor, picture_id):
"""
This function retrieves the picture_set_id of a picture in the database.
Parameters:
- cursor (cursor): The cursor of the database.
- picture_id (str): The UUID of the picture to retrieve the picture_set_id from.
"""
try:
query = """
SELECT
picture_set_id
FROM
picture
WHERE
id = %s
"""
cursor.execute(query, (picture_id,))
return str(cursor.fetchone()[0])
except Exception:
raise PictureNotFoundError(f"Error: Picture not found:{picture_id}")

def get_picture_set_owner_id(cursor, picture_set_id):
"""
This function retrieves the owner_id of a picture_set.
Expand Down
Loading

0 comments on commit 841b97b

Please sign in to comment.