deekshithabandam commited on
Commit
6e2fcfd
·
verified ·
1 Parent(s): 8147986

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -12
app.py CHANGED
@@ -1,21 +1,20 @@
 
1
  import gradio as gr
2
  import torch
3
  import numpy as np
4
  import cv2
5
- import matplotlib.pyplot as plt
6
  from arch import SegFormerUNet
7
  from albumentations import Compose, Resize, Normalize
8
  from albumentations.pytorch import ToTensorV2
9
 
 
10
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
11
  model = SegFormerUNet().to(device)
12
- checkpoint_path = "model/segformer_unet_focal_loss_97_63.pth"
13
- checkpoint = torch.load(checkpoint_path, map_location=device)
14
  model.load_state_dict(checkpoint)
15
  model.eval()
16
- print("Model weights loaded successfully!")
17
 
18
- # Image Transformation
19
  transform = Compose([
20
  Resize(256, 256),
21
  Normalize(mean=[0.5], std=[0.5]),
@@ -23,26 +22,23 @@ transform = Compose([
23
  ])
24
 
25
  def process_image(image):
26
- """Process uploaded image, perform segmentation, and compute energy output."""
27
  image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
28
  transformed = transform(image=image)['image'].unsqueeze(0).to(device)
29
-
30
  with torch.no_grad():
31
  output = model(transformed)
32
  pred_mask = torch.sigmoid(output).squeeze().cpu().numpy()
33
  pred_mask = (pred_mask > 0.5).astype(np.uint8)
34
-
35
  area_m2 = np.sum(pred_mask) * (0.125 ** 2)
36
  energy_kwh = area_m2 * 0.19 * 1676.2 * 0.935 / 1000
37
-
38
  return pred_mask * 255, f"Estimated Solar Panel Area: {area_m2:.2f} m²", f"Estimated Energy Output: {energy_kwh:.2f} MWh per year"
39
 
 
40
  demo = gr.Interface(
41
  fn=process_image,
42
- inputs=gr.Image(type="numpy"),
43
  outputs=[gr.Image(type="numpy"), gr.Text(), gr.Text()],
44
  title="Solar Panel Segmentation",
45
- description="Upload an image to detect solar panels and estimate energy output.",
46
  )
47
 
48
- demo.launch()
 
1
+
2
  import gradio as gr
3
  import torch
4
  import numpy as np
5
  import cv2
 
6
  from arch import SegFormerUNet
7
  from albumentations import Compose, Resize, Normalize
8
  from albumentations.pytorch import ToTensorV2
9
 
10
+ # Load model
11
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
12
  model = SegFormerUNet().to(device)
13
+ checkpoint = torch.load("model/segformer_unet_focal_loss_97_63.pth", map_location=device)
 
14
  model.load_state_dict(checkpoint)
15
  model.eval()
 
16
 
17
+ # Transform
18
  transform = Compose([
19
  Resize(256, 256),
20
  Normalize(mean=[0.5], std=[0.5]),
 
22
  ])
23
 
24
  def process_image(image):
 
25
  image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
26
  transformed = transform(image=image)['image'].unsqueeze(0).to(device)
 
27
  with torch.no_grad():
28
  output = model(transformed)
29
  pred_mask = torch.sigmoid(output).squeeze().cpu().numpy()
30
  pred_mask = (pred_mask > 0.5).astype(np.uint8)
 
31
  area_m2 = np.sum(pred_mask) * (0.125 ** 2)
32
  energy_kwh = area_m2 * 0.19 * 1676.2 * 0.935 / 1000
 
33
  return pred_mask * 255, f"Estimated Solar Panel Area: {area_m2:.2f} m²", f"Estimated Energy Output: {energy_kwh:.2f} MWh per year"
34
 
35
+ # Gradio Interface
36
  demo = gr.Interface(
37
  fn=process_image,
38
+ inputs=gr.Image(type="numpy", value="PV08_315125_1194663.bmp"), # <-- default image here
39
  outputs=[gr.Image(type="numpy"), gr.Text(), gr.Text()],
40
  title="Solar Panel Segmentation",
41
+ description="Upload an image to detect solar panels and estimate energy output."
42
  )
43
 
44
+ demo.launch()