Nunzio commited on
Commit
37570fa
Β·
1 Parent(s): dcb04c4

fixed errors

Browse files
Files changed (2) hide show
  1. app.py +11 -13
  2. utils/imageHandling.py +1 -1
app.py CHANGED
@@ -25,19 +25,18 @@ def predict(inputImage: torch.Tensor, model) -> torch.Tensor:
25
  # %% Gradio interface
26
  def run_prediction(image: gr.Image, selected_model: str)-> tuple[torch.Tensor]:
27
  if image is None:
28
- raise ValueError("No image provided for prediction.")
29
 
30
  if selected_model is None:
31
- raise ValueError("No model selected for prediction.")
32
-
33
  try:
34
  model = loadModel(selected_model, device)
35
- except NotImplementedError as e:
36
- raise ValueError(f"Model loading failed: {e}")
37
-
38
- image = hfImageToTensor(image, width=1024, height=512)
39
- prediction = predict(image, model)
40
- return postprocessing(prediction)
41
 
42
  # Gradio UI
43
  with gr.Blocks(title="πŸ”€ BiSeNet | BiSeNetV2 Predictor") as demo:
@@ -56,14 +55,13 @@ with gr.Blocks(title="πŸ”€ BiSeNet | BiSeNetV2 Predictor") as demo:
56
  submit_btn = gr.Button("Run prediction")
57
  with gr.Column():
58
  result_display = gr.Image(label="Model prediction")
59
-
60
- result_display = gr.Image(label="Output")
61
- error_text = gr.Textbox(label="Messaggio", interactive=False)
62
 
63
  submit_btn.click(
64
  fn=run_prediction,
65
  inputs=[image_input, model_selector],
66
- outputs=[result_display]
67
  )
68
 
69
  gr.Markdown("Made by group 21 semantic segmentation project. ")
 
25
  # %% Gradio interface
26
  def run_prediction(image: gr.Image, selected_model: str)-> tuple[torch.Tensor]:
27
  if image is None:
28
+ return (gr.update(value=None, visible=False), gr.update(value=f"❌ No image provided for prediction.", visible=True))
29
 
30
  if selected_model is None:
31
+ return (gr.update(value=None, visible=False), gr.update(value=f"❌ No model selected for prediction.", visible=True))
 
32
  try:
33
  model = loadModel(selected_model, device)
34
+ image = hfImageToTensor(image, width=1024, height=512)
35
+ prediction = predict(image, model)
36
+ prediction = postprocessing(prediction)
37
+ except Exception as e:
38
+ return (gr.update(value=None, visible=False), gr.update(value=f"❌ {str(e)}.", visible=True))
39
+ return (gr.update(value=prediction, visible=True), gr.update(value="", visible=False))
40
 
41
  # Gradio UI
42
  with gr.Blocks(title="πŸ”€ BiSeNet | BiSeNetV2 Predictor") as demo:
 
55
  submit_btn = gr.Button("Run prediction")
56
  with gr.Column():
57
  result_display = gr.Image(label="Model prediction")
58
+
59
+ error_text = gr.Textbox(label="Message", interactive=False, visible=False)
 
60
 
61
  submit_btn.click(
62
  fn=run_prediction,
63
  inputs=[image_input, model_selector],
64
+ outputs=[result_display, error_text],
65
  )
66
 
67
  gr.Markdown("Made by group 21 semantic segmentation project. ")
utils/imageHandling.py CHANGED
@@ -43,4 +43,4 @@ def postprocessing(pred: torch.Tensor) -> torch.Tensor:
43
  Returns:
44
  torch.Tensor: Processed tensor of shape (3, H, W) for visualization.
45
  """
46
- return torchvision.transforms.functional.to_pil_image(pred.squeeze(0).cpu().clamp(0, 1))
 
43
  Returns:
44
  torch.Tensor: Processed tensor of shape (3, H, W) for visualization.
45
  """
46
+ return torchvision.transforms.functional.to_pil_image(pred.squeeze(0).cpu().to(torch.uint8))