Spaces:
Sleeping
Sleeping
Merge pull request #1 from ia-labo/feat/add_streamlit
Browse filesfeat: add Streamlit app with image display and dropdown selection
- front/app.py +49 -0
- front/image_preprocessing.py +52 -0
front/app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""App to display images in a gallery"""
|
2 |
+
|
3 |
+
import streamlit as st
|
4 |
+
from image_preprocessing import get_image_caption, get_images, resize_image
|
5 |
+
|
6 |
+
|
7 |
+
def image_gallery(images: list) -> None:
|
8 |
+
"""Display a gallery of images in a streamlit app
|
9 |
+
|
10 |
+
Args:
|
11 |
+
images (list): list of images to display
|
12 |
+
"""
|
13 |
+
st.title("Welcome")
|
14 |
+
|
15 |
+
columns = st.columns(3)
|
16 |
+
for index, image in enumerate(images):
|
17 |
+
with columns[index % 3]:
|
18 |
+
resized_image = resize_image(image)
|
19 |
+
image_caption = get_image_caption(image)
|
20 |
+
st.image(
|
21 |
+
resized_image,
|
22 |
+
width=200,
|
23 |
+
use_container_width=True,
|
24 |
+
caption=image_caption,
|
25 |
+
)
|
26 |
+
|
27 |
+
|
28 |
+
def sidebar() -> str:
|
29 |
+
"""Create a sidebar to select the park
|
30 |
+
|
31 |
+
Returns:
|
32 |
+
selected_park: selected park in the sidebar
|
33 |
+
"""
|
34 |
+
selected_park = st.sidebar.selectbox(
|
35 |
+
"Park List", ["Al Khaldiyah Park", "Family Park"]
|
36 |
+
)
|
37 |
+
return selected_park
|
38 |
+
|
39 |
+
|
40 |
+
def main() -> None:
|
41 |
+
"""Main function to run the app"""
|
42 |
+
path = "../images/"
|
43 |
+
park = sidebar()
|
44 |
+
images = get_images(path + park)
|
45 |
+
image_gallery(images)
|
46 |
+
|
47 |
+
|
48 |
+
if __name__ == "__main__":
|
49 |
+
main()
|
front/image_preprocessing.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""File to preprocess images"""
|
2 |
+
|
3 |
+
import os
|
4 |
+
|
5 |
+
from PIL import Image
|
6 |
+
|
7 |
+
|
8 |
+
def get_images(folder: str) -> list:
|
9 |
+
"""Get images from a folder
|
10 |
+
|
11 |
+
Args:
|
12 |
+
folder (str): path to the folder
|
13 |
+
|
14 |
+
Returns:
|
15 |
+
images: list of images in the folder
|
16 |
+
"""
|
17 |
+
images = []
|
18 |
+
extetntions = [".png", ".jpeg", ".jpg"]
|
19 |
+
for filename in os.listdir(folder):
|
20 |
+
if any(filename.endswith(ext) for ext in extetntions):
|
21 |
+
images.append(os.path.join(folder, filename))
|
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:
|
29 |
+
img_path (str): path to the image
|
30 |
+
target_w (int): int to resize the width of the image
|
31 |
+
target_h (int): int to resize the height of the image
|
32 |
+
|
33 |
+
Returns:
|
34 |
+
img: resized images
|
35 |
+
"""
|
36 |
+
img = Image.open(img_path)
|
37 |
+
img = img.resize((target_w, target_h))
|
38 |
+
return img
|
39 |
+
|
40 |
+
|
41 |
+
def get_image_caption(image_path):
|
42 |
+
"""Get the caption of an image
|
43 |
+
|
44 |
+
Args:
|
45 |
+
image_path (str): path to the image
|
46 |
+
|
47 |
+
Returns:
|
48 |
+
image_path: caption of the image
|
49 |
+
"""
|
50 |
+
image_path = image_path.split("/")[-1]
|
51 |
+
image_path = image_path.split(".")[0]
|
52 |
+
return image_path
|