Spaces:
Running
Running
Commit
·
ecbbafe
1
Parent(s):
93a69e3
feat: use database as app source
Browse files- src/app.py +53 -28
- src/data_models/image_manager.py +2 -2
- src/data_models/openai_manager.py +17 -0
- src/data_models/park_manager.py +1 -1
- src/image_preprocessing.py +1 -1
src/app.py
CHANGED
@@ -1,13 +1,37 @@
|
|
1 |
"""App to display images in a gallery"""
|
2 |
|
3 |
-
import os
|
4 |
import numpy as np
|
5 |
import streamlit as st
|
6 |
from image_preprocessing import get_image_caption, get_images, resize_image
|
7 |
-
|
|
|
8 |
from data_models.park_manager import ParkManager
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
def image_gallery(images: list) -> None:
|
11 |
"""Display a gallery of images in a streamlit app
|
12 |
|
13 |
Args:
|
@@ -18,52 +42,53 @@ def image_gallery(images: list) -> None:
|
|
18 |
columns = st.columns(3)
|
19 |
for index, image in enumerate(images):
|
20 |
with columns[index % 3]:
|
21 |
-
resized_image = resize_image(image, target_w=1080, target_h=720)
|
22 |
-
image_caption = get_image_caption(image)
|
23 |
st.image(
|
24 |
-
np.array(resized_image),
|
25 |
-
width=200,
|
26 |
-
caption=image_caption,
|
27 |
)
|
|
|
|
|
|
|
|
|
28 |
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
# Returns:
|
34 |
-
# list: list of parks
|
35 |
-
# """
|
36 |
|
37 |
-
# path = "./images/"
|
38 |
-
# parks = os.listdir(path)
|
39 |
-
# return sorted(parks)
|
40 |
|
|
|
|
|
41 |
|
42 |
-
def get_park_list() -> list:
|
43 |
-
park_manager = ParkManager()
|
44 |
-
# return [park["name"] for park in park_manager.get_parks()]
|
45 |
-
parks = [park[1] for park in park_manager.get_parks()]
|
46 |
-
return parks
|
47 |
|
48 |
-
|
49 |
-
def sidebar() -> Any | None:
|
50 |
"""Create a sidebar to select the park
|
51 |
|
52 |
Returns:
|
53 |
selected_park: selected park in the sidebar
|
54 |
"""
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
|
58 |
def main() -> None:
|
59 |
"""Main function to run the app"""
|
60 |
-
path = "./images/"
|
61 |
park = sidebar()
|
62 |
if not park:
|
63 |
st.write("Please select a park")
|
64 |
return
|
65 |
-
|
66 |
-
|
|
|
|
|
67 |
|
68 |
|
69 |
if __name__ == "__main__":
|
|
|
1 |
"""App to display images in a gallery"""
|
2 |
|
|
|
3 |
import numpy as np
|
4 |
import streamlit as st
|
5 |
from image_preprocessing import get_image_caption, get_images, resize_image
|
6 |
+
|
7 |
+
from data_models.sql_connection import get_db_connection
|
8 |
from data_models.park_manager import ParkManager
|
9 |
+
from data_models.image_manager import ImageManager
|
10 |
+
from data_models.openai_manager import OpenAIManager
|
11 |
+
|
12 |
+
|
13 |
+
engine, session = get_db_connection()
|
14 |
+
park_manager = ParkManager()
|
15 |
+
image_manager = ImageManager()
|
16 |
+
openai_manager = OpenAIManager()
|
17 |
+
|
18 |
+
|
19 |
+
def get_image_predictions(images: list[dict]) -> dict:
|
20 |
+
"""Get predictions for a list of images
|
21 |
+
|
22 |
+
Args:
|
23 |
+
images (list): list of images to get predictions for
|
24 |
+
|
25 |
+
Returns:
|
26 |
+
list: list of predictions for the images
|
27 |
+
"""
|
28 |
+
predictions = {}
|
29 |
+
for image in images:
|
30 |
+
predictions[image["id"]] = openai_manager.get_predictions(image["id"])
|
31 |
+
return predictions
|
32 |
+
|
33 |
|
34 |
+
def image_gallery(images: list[dict], predictions:dict) -> None:
|
35 |
"""Display a gallery of images in a streamlit app
|
36 |
|
37 |
Args:
|
|
|
42 |
columns = st.columns(3)
|
43 |
for index, image in enumerate(images):
|
44 |
with columns[index % 3]:
|
45 |
+
resized_image = resize_image(image["name"], target_w=1080, target_h=720)
|
|
|
46 |
st.image(
|
47 |
+
np.array(resized_image), width=200, caption=f"Image id #{image['id']}"
|
|
|
|
|
48 |
)
|
49 |
+
if not image["id"] in predictions or predictions[image["id"]] is None:
|
50 |
+
st.write("No predictions available")
|
51 |
+
continue
|
52 |
+
st.json(predictions[image["id"]])
|
53 |
|
54 |
|
55 |
+
def get_park_list() -> list:
|
56 |
+
return park_manager.get_parks()
|
|
|
|
|
|
|
|
|
57 |
|
|
|
|
|
|
|
58 |
|
59 |
+
def get_park_images(park: str) -> list:
|
60 |
+
return image_manager.get_images_by_park(park)
|
61 |
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
+
def sidebar() -> dict | None:
|
|
|
64 |
"""Create a sidebar to select the park
|
65 |
|
66 |
Returns:
|
67 |
selected_park: selected park in the sidebar
|
68 |
"""
|
69 |
+
park_list = get_park_list()
|
70 |
+
|
71 |
+
def _park_list_formatter(park):
|
72 |
+
return park["name"]
|
73 |
+
|
74 |
+
return st.sidebar.selectbox(
|
75 |
+
label="Park List",
|
76 |
+
options=park_list,
|
77 |
+
index=None,
|
78 |
+
format_func=_park_list_formatter,
|
79 |
+
)
|
80 |
|
81 |
|
82 |
def main() -> None:
|
83 |
"""Main function to run the app"""
|
|
|
84 |
park = sidebar()
|
85 |
if not park:
|
86 |
st.write("Please select a park")
|
87 |
return
|
88 |
+
|
89 |
+
images = get_park_images(park["id"])
|
90 |
+
predictions = get_image_predictions(images)
|
91 |
+
image_gallery(images, predictions)
|
92 |
|
93 |
|
94 |
if __name__ == "__main__":
|
src/data_models/image_manager.py
CHANGED
@@ -56,7 +56,7 @@ class ImageManager:
|
|
56 |
except Exception as e:
|
57 |
raise Exception(f"An error occurred while getting the image ID: {e}")
|
58 |
|
59 |
-
def
|
60 |
"""
|
61 |
Get all images from the `images` table.
|
62 |
Args:
|
@@ -71,7 +71,7 @@ class ImageManager:
|
|
71 |
result = self.session.execute(
|
72 |
query, {"park_id": park_id} if park_id else {}
|
73 |
)
|
74 |
-
return [
|
75 |
except Exception as e:
|
76 |
raise Exception(f"Erreur lors de la récupération des images : {e}")
|
77 |
|
|
|
56 |
except Exception as e:
|
57 |
raise Exception(f"An error occurred while getting the image ID: {e}")
|
58 |
|
59 |
+
def get_images_by_park(self, park_id):
|
60 |
"""
|
61 |
Get all images from the `images` table.
|
62 |
Args:
|
|
|
71 |
result = self.session.execute(
|
72 |
query, {"park_id": park_id} if park_id else {}
|
73 |
)
|
74 |
+
return [row._asdict() for row in result]
|
75 |
except Exception as e:
|
76 |
raise Exception(f"Erreur lors de la récupération des images : {e}")
|
77 |
|
src/data_models/openai_manager.py
CHANGED
@@ -60,6 +60,23 @@ class OpenAIManager:
|
|
60 |
self.session.rollback()
|
61 |
raise Exception(f"An error occurred while adding the predictions: {e}")
|
62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
def close_connection(self):
|
64 |
"""Close the connection."""
|
65 |
self.session.close()
|
|
|
60 |
self.session.rollback()
|
61 |
raise Exception(f"An error occurred while adding the predictions: {e}")
|
62 |
|
63 |
+
def get_predictions(self, img_id):
|
64 |
+
"""
|
65 |
+
Get predictions from the `openai_predictions` table.
|
66 |
+
|
67 |
+
Args:
|
68 |
+
img_id (int): ID of the image where the bounding box was detected.
|
69 |
+
|
70 |
+
Returns:
|
71 |
+
dict: Predictions of the image.
|
72 |
+
"""
|
73 |
+
query = text("SELECT * FROM openai_predictions WHERE img_id = :img_id")
|
74 |
+
try:
|
75 |
+
result = self.session.execute(query, {"img_id": img_id}).fetchone()
|
76 |
+
return result._asdict() if result else None
|
77 |
+
except Exception as e:
|
78 |
+
raise Exception(f"An error occurred while getting the predictions: {e}")
|
79 |
+
|
80 |
def close_connection(self):
|
81 |
"""Close the connection."""
|
82 |
self.session.close()
|
src/data_models/park_manager.py
CHANGED
@@ -36,7 +36,7 @@ class ParkManager:
|
|
36 |
try:
|
37 |
query = text("SELECT * FROM parks")
|
38 |
result = self.session.execute(query)
|
39 |
-
return [row for row in result.fetchall()]
|
40 |
except Exception as e:
|
41 |
raise Exception(f"An error occurred while fetching the parks: {e}")
|
42 |
|
|
|
36 |
try:
|
37 |
query = text("SELECT * FROM parks")
|
38 |
result = self.session.execute(query)
|
39 |
+
return [row._asdict() for row in result.fetchall()]
|
40 |
except Exception as e:
|
41 |
raise Exception(f"An error occurred while fetching the parks: {e}")
|
42 |
|
src/image_preprocessing.py
CHANGED
@@ -22,7 +22,7 @@ def get_images(folder: str) -> list:
|
|
22 |
return images
|
23 |
|
24 |
|
25 |
-
def resize_image(img_path: str, target_w: int = 200, target_h: int = 200) -> Image:
|
26 |
"""Resize an image
|
27 |
|
28 |
Args:
|
|
|
22 |
return images
|
23 |
|
24 |
|
25 |
+
def resize_image(img_path: str, target_w: int = 200, target_h: int = 200) -> Image.Image:
|
26 |
"""Resize an image
|
27 |
|
28 |
Args:
|