Nunzio commited on
Commit
e5d95e7
·
1 Parent(s): 0096bcd

addedo images

Browse files
Files changed (2) hide show
  1. app.py +14 -31
  2. utils/imageHandling.py +5 -4
app.py CHANGED
@@ -1,6 +1,5 @@
1
- import os, torch
2
  import gradio as gr
3
- from PIL import Image
4
 
5
  from utils.imageHandling import hfImageToTensor, preprocessing, postprocessing, loadPreloadedImages
6
  from model.modelLoading import loadBiSeNet, loadBiSeNetV2
@@ -9,6 +8,7 @@ from model.modelLoading import loadBiSeNet, loadBiSeNetV2
9
  ## %% CONSTANTS
10
  gta_image_dir = "./preloadedImages/GTAV"
11
  city_image_dir = "./preloadedImages/cityScapes"
 
12
  device = 'cuda' if torch.cuda.is_available() else 'cpu'
13
 
14
 
@@ -18,9 +18,13 @@ MODELS = {
18
  }
19
 
20
 
21
- def load_example(index:int=0, useGta:bool=True)-> gr.Image:
22
- example_img = loadPreloadedImages(gta_image_dir if useGta else city_image_dir)
23
- return gr.update(value=example_img[index if 0 <= index < len(example_img) else 0], visible=True)
 
 
 
 
24
 
25
 
26
  # %% prediction on an image
@@ -81,35 +85,14 @@ with gr.Blocks(title="Semantic Segmentation Predictors") as demo:
81
  error_text = gr.Markdown("", visible=False)
82
 
83
  with gr.Row():
84
- gr.Markdown("## Preloaded GTA V images to be used for testing the model")
85
- with gr.Row():
86
- gta_gallery = gr.Gallery(
87
- value=loadPreloadedImages(gta_image_dir),
88
- label="GTA V Examples",
89
- show_label=False,
90
- columns=5,
91
- type="index",
92
- rows=1,
93
- height=200,
94
- allow_preview=False
95
- )
96
-
97
- with gr.Row():
98
- gr.Markdown("## Preloaded Cityscapes images to be used for testing the model")
99
  with gr.Row():
100
- city_gallery = gr.Gallery(
101
- value=loadPreloadedImages(city_image_dir),
102
- label="Cityscapes Examples",
103
- show_label=False,
104
- columns=5,
105
- type="index",
106
- rows=1,
107
- height=256,
108
- allow_preview=False
109
  )
110
 
111
- gta_gallery.select(fn=lambda i: load_example(i, True), inputs=[], outputs=image_input)
112
- city_gallery.select(fn=lambda i: load_example(i, False), inputs=[], outputs=image_input)
113
 
114
  submit_btn.click(
115
  fn=run_prediction,
 
1
+ import torch, uuid
2
  import gradio as gr
 
3
 
4
  from utils.imageHandling import hfImageToTensor, preprocessing, postprocessing, loadPreloadedImages
5
  from model.modelLoading import loadBiSeNet, loadBiSeNetV2
 
8
  ## %% CONSTANTS
9
  gta_image_dir = "./preloadedImages/GTAV"
10
  city_image_dir = "./preloadedImages/cityScapes"
11
+ turin_image_dir = "./preloadedImages/turin"
12
  device = 'cuda' if torch.cuda.is_available() else 'cpu'
13
 
14
 
 
18
  }
19
 
20
 
21
+ image_list = loadPreloadedImages(gta_image_dir, city_image_dir, turin_image_dir)
22
+ uuid_to_path = dict()
23
+
24
+ for i in range(len(image_list)):
25
+ uid = str(uuid.uuid5(uuid.NAMESPACE_URL, image_list[i][1]))
26
+ uuid_to_path[uid] = image_list[i][1]
27
+ image_list[i][1] = uid
28
 
29
 
30
  # %% prediction on an image
 
85
  error_text = gr.Markdown("", visible=False)
86
 
87
  with gr.Row():
88
+ gr.Markdown("## Preloaded images to be used for testing the model")
89
+ gr.Markdown("You can use images from the Grand Theft Auto V video game, the Cityscapes dataset or even from Turin")
 
 
 
 
 
 
 
 
 
 
 
 
 
90
  with gr.Row():
91
+ image_gallery = gr.Gallery(value=image_list, label="Preloaded Examples",
92
+ type="value", columns=5, rows=4,
93
+ height=1200, allow_preview=False
 
 
 
 
 
 
94
  )
95
 
 
 
96
 
97
  submit_btn.click(
98
  fn=run_prediction,
utils/imageHandling.py CHANGED
@@ -85,14 +85,15 @@ def postprocessing(pred: torch.Tensor) -> torch.Tensor:
85
 
86
 
87
  # %% preloaded images
88
- def loadPreloadedImages(image_dir: str) -> list[Image.Image]:
89
  """
90
  Load preloaded images from a directory.
91
 
92
  Args:
93
- image_dir (str): Path to the directory containing images.
94
 
95
  Returns:
96
- list[Image.Image]: List of loaded images.
97
  """
98
- return list(map(lambda f: Image.open(os.path.join(image_dir, f)).convert("RGB"), sorted([f for f in os.listdir(image_dir) if f.endswith(".png")])))
 
 
85
 
86
 
87
  # %% preloaded images
88
+ def loadPreloadedImages(*args:str) -> list[tuple[Image.Image, str]]:
89
  """
90
  Load preloaded images from a directory.
91
 
92
  Args:
93
+ args (str): Path to the directory containing images.
94
 
95
  Returns:
96
+ images (list[tuple[Image.Image, str]]): List of loaded images with their original paths.
97
  """
98
+ return sorted([(Image.open(os.path.join(imageDir, image)).convert("RGB"), os.path.join(imageDir, image))
99
+ for imageDir in args for image in os.listdir(imageDir) if image.endswith([".png", ".jpg", "jpeg"])])