NaveenKumar5 commited on
Commit
3d8126c
·
verified ·
1 Parent(s): 9f138a6

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +14 -10
src/streamlit_app.py CHANGED
@@ -5,8 +5,8 @@ import numpy as np
5
  import tempfile
6
  import torch
7
  import matplotlib.pyplot as plt
8
- from transformers import AutoImageProcessor, AutoModelForObjectDetection
9
  from PIL import Image, ImageDraw
 
10
 
11
  # Fix cache permission issue
12
  os.environ['TRANSFORMERS_CACHE'] = '/tmp/huggingface'
@@ -15,11 +15,10 @@ model_id = "NaveenKumar5/Solar_panel_fault_detection"
15
 
16
  @st.cache_resource
17
  def load_model():
18
- processor = AutoImageProcessor.from_pretrained(model_id)
19
  model = AutoModelForObjectDetection.from_pretrained(model_id)
20
- return processor, model
21
 
22
- processor, model = load_model()
23
  model.eval()
24
 
25
  st.title("🔍 Solar Panel Fault Detection")
@@ -42,19 +41,25 @@ def generate_heatmap(image, boxes):
42
  heatmap = np.clip(heatmap / np.max(heatmap), 0, 1)
43
  return heatmap
44
 
 
 
 
 
 
 
45
  if uploaded_file is not None:
46
  if uploaded_file.type.startswith("image"):
47
  image = Image.open(uploaded_file).convert("RGB")
48
- inputs = processor(images=image, return_tensors="pt")
49
 
50
  with torch.no_grad():
51
- outputs = model(**inputs)
52
 
53
- scores = outputs.logits.softmax(-1)[0].max(-1).values
54
  keep = scores > 0.5
55
 
56
- boxes = outputs.pred_boxes[0][keep].cpu().numpy()
57
- labels = outputs.logits.argmax(-1)[0][keep].cpu().numpy()
58
  scores = scores[keep].cpu().numpy()
59
 
60
  image_np = np.array(image)
@@ -82,4 +87,3 @@ if uploaded_file is not None:
82
 
83
  elif uploaded_file.type.startswith("video"):
84
  st.warning("Video support coming soon. For now, please upload an image.")
85
-
 
5
  import tempfile
6
  import torch
7
  import matplotlib.pyplot as plt
 
8
  from PIL import Image, ImageDraw
9
+ from transformers import AutoModelForObjectDetection
10
 
11
  # Fix cache permission issue
12
  os.environ['TRANSFORMERS_CACHE'] = '/tmp/huggingface'
 
15
 
16
  @st.cache_resource
17
  def load_model():
 
18
  model = AutoModelForObjectDetection.from_pretrained(model_id)
19
+ return model
20
 
21
+ model = load_model()
22
  model.eval()
23
 
24
  st.title("🔍 Solar Panel Fault Detection")
 
41
  heatmap = np.clip(heatmap / np.max(heatmap), 0, 1)
42
  return heatmap
43
 
44
+ def preprocess_image(image):
45
+ image = image.resize((800, 800))
46
+ image_np = np.array(image).astype(np.float32) / 255.0
47
+ image_tensor = torch.tensor(image_np).permute(2, 0, 1).unsqueeze(0)
48
+ return image_tensor
49
+
50
  if uploaded_file is not None:
51
  if uploaded_file.type.startswith("image"):
52
  image = Image.open(uploaded_file).convert("RGB")
53
+ inputs = preprocess_image(image)
54
 
55
  with torch.no_grad():
56
+ outputs = model(pixel_values=inputs)
57
 
58
+ scores = outputs["logits"].softmax(-1)[0].max(-1).values
59
  keep = scores > 0.5
60
 
61
+ boxes = outputs["pred_boxes"][0][keep].cpu().numpy()
62
+ labels = outputs["logits"].argmax(-1)[0][keep].cpu().numpy()
63
  scores = scores[keep].cpu().numpy()
64
 
65
  image_np = np.array(image)
 
87
 
88
  elif uploaded_file.type.startswith("video"):
89
  st.warning("Video support coming soon. For now, please upload an image.")