Spaces:
Sleeping
Sleeping
feat: add class manager for bbox, park and image
Browse files- src/models/bbox_manager.py +64 -0
- src/models/image_manager.py +99 -0
- src/models/park_manager.py +76 -0
src/models/bbox_manager.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Manage bounding boxes in the database."""
|
| 2 |
+
|
| 3 |
+
from sqlalchemy import text
|
| 4 |
+
|
| 5 |
+
from models.sql_connection import get_db_connection
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class BoundingBoxManager:
|
| 9 |
+
def __init__(self):
|
| 10 |
+
"""Initialise connection and session."""
|
| 11 |
+
self.engine, self.session = get_db_connection()
|
| 12 |
+
|
| 13 |
+
def add_bbox(self, confidence, class_id, img_id, x_min, y_min, x_max, y_max):
|
| 14 |
+
"""
|
| 15 |
+
Add a bounding box to the `bboxes` table.
|
| 16 |
+
|
| 17 |
+
Args:
|
| 18 |
+
confidence (float): Confidence of the detection.
|
| 19 |
+
class_id (str): Class ID of the detection.
|
| 20 |
+
img_id (int): ID of the image where the bounding box was detected.
|
| 21 |
+
x_min (float): Minimum X coordinate.
|
| 22 |
+
y_min (float): Minimum Y coordinate.
|
| 23 |
+
x_max (float): Maximum X coordinate.
|
| 24 |
+
y_max (float): Maximum Y coordinate.
|
| 25 |
+
|
| 26 |
+
Returns:
|
| 27 |
+
dict: Information of the added bounding box.
|
| 28 |
+
"""
|
| 29 |
+
query = text(
|
| 30 |
+
"""
|
| 31 |
+
INSERT INTO bboxes (confidence, class_id, img_id, x_min, y_min, x_max, y_max)
|
| 32 |
+
VALUES (:confidence, :class_id, :img_id, :x_min, :y_min, :x_max, :y_max)
|
| 33 |
+
"""
|
| 34 |
+
)
|
| 35 |
+
try:
|
| 36 |
+
self.session.execute(
|
| 37 |
+
query,
|
| 38 |
+
{
|
| 39 |
+
"confidence": confidence,
|
| 40 |
+
"class_id": class_id,
|
| 41 |
+
"img_id": img_id,
|
| 42 |
+
"x_min": x_min,
|
| 43 |
+
"y_min": y_min,
|
| 44 |
+
"x_max": x_max,
|
| 45 |
+
"y_max": y_max,
|
| 46 |
+
},
|
| 47 |
+
)
|
| 48 |
+
self.session.commit()
|
| 49 |
+
return {
|
| 50 |
+
"confidence": confidence,
|
| 51 |
+
"class_id": class_id,
|
| 52 |
+
"img_id": img_id,
|
| 53 |
+
"x_min": x_min,
|
| 54 |
+
"y_min": y_min,
|
| 55 |
+
"x_max": x_max,
|
| 56 |
+
"y_max": y_max,
|
| 57 |
+
}
|
| 58 |
+
except Exception as e:
|
| 59 |
+
self.session.rollback()
|
| 60 |
+
raise Exception(f"An error occurred while adding the bounding box: {e}")
|
| 61 |
+
|
| 62 |
+
def close_connection(self):
|
| 63 |
+
"""Close the connection."""
|
| 64 |
+
self.session.close()
|
src/models/image_manager.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Mnages images table in the database."""
|
| 2 |
+
|
| 3 |
+
from sqlalchemy import text
|
| 4 |
+
|
| 5 |
+
from models.sql_connection import get_db_connection
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class ImageManager:
|
| 9 |
+
"""ImageManager class."""
|
| 10 |
+
|
| 11 |
+
def __init__(self):
|
| 12 |
+
"""Initialise connection and session."""
|
| 13 |
+
self.engine, self.session = get_db_connection()
|
| 14 |
+
|
| 15 |
+
def add_image(self, name, created_at, park_id=None):
|
| 16 |
+
"""
|
| 17 |
+
Add an image to the `images` table.
|
| 18 |
+
|
| 19 |
+
Args:
|
| 20 |
+
name (str): Image name.
|
| 21 |
+
created_at (str): Image creation date. Format: "YYYY-MM-DD HH:MM:SS".
|
| 22 |
+
park_id (int):id of the park where the image was taken.
|
| 23 |
+
Returns:
|
| 24 |
+
dict: Information of the added image.
|
| 25 |
+
"""
|
| 26 |
+
query = text(
|
| 27 |
+
"""
|
| 28 |
+
INSERT INTO images (name, created_at, park_id)
|
| 29 |
+
VALUES (:name, :created_at, :park_id)
|
| 30 |
+
"""
|
| 31 |
+
)
|
| 32 |
+
try:
|
| 33 |
+
self.session.execute(
|
| 34 |
+
query, {"name": name, "created_at": created_at, "park_id": park_id}
|
| 35 |
+
)
|
| 36 |
+
self.session.commit()
|
| 37 |
+
return {
|
| 38 |
+
"name": name,
|
| 39 |
+
"created_at": created_at,
|
| 40 |
+
"park_id": park_id,
|
| 41 |
+
}
|
| 42 |
+
except Exception as e:
|
| 43 |
+
self.session.rollback()
|
| 44 |
+
raise Exception(f"An error occurred while adding the image: {e}")
|
| 45 |
+
|
| 46 |
+
def get_image_id(self, image_name):
|
| 47 |
+
"""
|
| 48 |
+
Get the image ID from the image name.
|
| 49 |
+
Args:
|
| 50 |
+
image_name (str): Name of the image.
|
| 51 |
+
Returns:
|
| 52 |
+
int: ID of the image.
|
| 53 |
+
"""
|
| 54 |
+
query = text("SELECT id FROM images WHERE name = :image_name")
|
| 55 |
+
try:
|
| 56 |
+
result = self.session.execute(query, {"image_name": image_name}).fetchone()
|
| 57 |
+
return result[0] if result else None
|
| 58 |
+
except Exception as e:
|
| 59 |
+
raise Exception(f"An error occurred while getting the image ID: {e}")
|
| 60 |
+
|
| 61 |
+
def get_images(self, park_id):
|
| 62 |
+
"""
|
| 63 |
+
Get all images from the `images` table.
|
| 64 |
+
Args:
|
| 65 |
+
park_id (int): ID of the park to filter images.
|
| 66 |
+
Returns:
|
| 67 |
+
list[dict]: List of images.
|
| 68 |
+
"""
|
| 69 |
+
query = text("SELECT * FROM images")
|
| 70 |
+
if park_id:
|
| 71 |
+
query = text("SELECT * FROM images WHERE park_id = :park_id")
|
| 72 |
+
try:
|
| 73 |
+
result = self.session.execute(
|
| 74 |
+
query, {"park_id": park_id} if park_id else {}
|
| 75 |
+
)
|
| 76 |
+
return [dict(row) for row in result]
|
| 77 |
+
except Exception as e:
|
| 78 |
+
raise Exception(f"Erreur lors de la récupération des images : {e}")
|
| 79 |
+
|
| 80 |
+
def delete_image(self, image_id):
|
| 81 |
+
"""
|
| 82 |
+
Delete an image by its ID.
|
| 83 |
+
Args:
|
| 84 |
+
image_id (int): Image ID.
|
| 85 |
+
Returns:
|
| 86 |
+
bool: True if the deletion was successful, False otherwise.
|
| 87 |
+
"""
|
| 88 |
+
query = text("DELETE FROM images WHERE id = :image_id")
|
| 89 |
+
try:
|
| 90 |
+
result = self.session.execute(query, {"image_id": image_id})
|
| 91 |
+
self.session.commit()
|
| 92 |
+
return result.rowcount > 0
|
| 93 |
+
except Exception as e:
|
| 94 |
+
self.session.rollback()
|
| 95 |
+
raise Exception(f"Erreur lors de la suppression de l'image : {e}")
|
| 96 |
+
|
| 97 |
+
def close_connection(self):
|
| 98 |
+
"""Close the connection."""
|
| 99 |
+
self.session.close()
|
src/models/park_manager.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Manage parks table in the database."""
|
| 2 |
+
|
| 3 |
+
from sqlalchemy import text
|
| 4 |
+
|
| 5 |
+
from models.sql_connection import get_db_connection
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class ParkManager:
|
| 9 |
+
def __init__(self):
|
| 10 |
+
"""Initialise connection and session."""
|
| 11 |
+
self.engine, self.session = get_db_connection()
|
| 12 |
+
|
| 13 |
+
def add_park(self, name):
|
| 14 |
+
query = text(
|
| 15 |
+
"""
|
| 16 |
+
INSERT INTO parks (name)
|
| 17 |
+
VALUES (:name)
|
| 18 |
+
"""
|
| 19 |
+
)
|
| 20 |
+
try:
|
| 21 |
+
self.session.execute(query, {"name": name})
|
| 22 |
+
self.session.commit()
|
| 23 |
+
return {"name": name}
|
| 24 |
+
except Exception as e:
|
| 25 |
+
self.session.rollback()
|
| 26 |
+
raise Exception(f"An error occurred while adding the park: {e}")
|
| 27 |
+
|
| 28 |
+
def get_parks(self):
|
| 29 |
+
"""
|
| 30 |
+
get all parks from the `parks` table.
|
| 31 |
+
|
| 32 |
+
Returns:
|
| 33 |
+
list[dict]: list of parks.
|
| 34 |
+
"""
|
| 35 |
+
query = text("SELECT * FROM parks")
|
| 36 |
+
try:
|
| 37 |
+
result = self.session.execute(query)
|
| 38 |
+
return [dict(row) for row in result]
|
| 39 |
+
except Exception as e:
|
| 40 |
+
raise Exception(f"An error occurred while adding the park: {e}")
|
| 41 |
+
|
| 42 |
+
def get_park_id(self, park_name):
|
| 43 |
+
"""Get the park ID from the park name.
|
| 44 |
+
|
| 45 |
+
Args:
|
| 46 |
+
park_name (str): Name of the park.
|
| 47 |
+
"""
|
| 48 |
+
query = text("SELECT id FROM parks WHERE name = :park_name")
|
| 49 |
+
try:
|
| 50 |
+
result = self.session.execute(query, {"park_name": park_name}).fetchone()
|
| 51 |
+
return result[0] if result else None
|
| 52 |
+
except Exception as e:
|
| 53 |
+
raise Exception(f"An error occurred while getting the park ID: {e}")
|
| 54 |
+
|
| 55 |
+
def delete_park(self, park_id):
|
| 56 |
+
"""
|
| 57 |
+
Delete a park from the `parks` table.
|
| 58 |
+
|
| 59 |
+
Args:
|
| 60 |
+
park_id (int): ID of the park to delete.
|
| 61 |
+
|
| 62 |
+
Returns:
|
| 63 |
+
bool: True if the park was deleted, False otherwise.
|
| 64 |
+
"""
|
| 65 |
+
query = text("DELETE FROM parks WHERE id = :park_id")
|
| 66 |
+
try:
|
| 67 |
+
result = self.session.execute(query, {"park_id": park_id})
|
| 68 |
+
self.session.commit()
|
| 69 |
+
return result.rowcount > 0
|
| 70 |
+
except Exception as e:
|
| 71 |
+
self.session.rollback()
|
| 72 |
+
raise Exception(f"An error occurred while adding the park: {e}")
|
| 73 |
+
|
| 74 |
+
def close_connection(self):
|
| 75 |
+
"""Close the connection."""
|
| 76 |
+
self.session.close()
|