NaveenKumar5 commited on
Commit
67a8963
Β·
verified Β·
1 Parent(s): 58a9554

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +24 -15
src/streamlit_app.py CHANGED
@@ -1,4 +1,3 @@
1
- # src/streamlit_app.py
2
  import streamlit as st
3
  import cv2
4
  import numpy as np
@@ -9,15 +8,24 @@ from PIL import Image
9
  import torch
10
  from transformers import DetrImageProcessor, DetrForObjectDetection
11
 
12
- # === Hugging Face DETR Configuration ===
13
- processor = DetrImageProcessor.from_pretrained("NaveenKumar5/Solar_panel_fault_detection")
14
- model = DetrForObjectDetection.from_pretrained("NaveenKumar5/Solar_panel_fault_detection")
 
 
 
 
 
 
 
 
 
15
  model.eval()
16
 
17
- # === Streamlit App Configuration ===
18
  st.set_page_config(page_title="Solar Panel Fault Detection", layout="wide")
19
- st.title("\U0001F50D Solar Panel Fault Detection (Hugging Face DETR)")
20
- st.write("Upload a thermal video (MP4). Faults will be detected using your custom DETR model.")
21
 
22
  # === Fault Detection Function ===
23
  def detect_faults(frame, frame_idx, fps):
@@ -37,6 +45,7 @@ def detect_faults(frame, frame_idx, fps):
37
  label_id = label.item()
38
  label_name = f"class_{label_id}"
39
 
 
40
  color = (0, 0, 255)
41
  cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)
42
  cv2.putText(frame, f"{label_name} ({conf:.2f})", (x1, y1 - 5),
@@ -51,7 +60,7 @@ def detect_faults(frame, frame_idx, fps):
51
  })
52
  return frame, faults
53
 
54
- # === Video Processing Function ===
55
  def process_video(video_path):
56
  cap = cv2.VideoCapture(video_path)
57
  fps = int(cap.get(cv2.CAP_PROP_FPS))
@@ -81,12 +90,12 @@ def process_video(video_path):
81
  writer.release()
82
  return output_path, fault_log
83
 
84
- # === CSV Conversion ===
85
  def convert_df(df):
86
  return df.to_csv(index=False).encode('utf-8')
87
 
88
- # === Streamlit UI ===
89
- uploaded_file = st.file_uploader("\U0001F4E4 Upload thermal video", type=["mp4"])
90
  if uploaded_file:
91
  st.video(uploaded_file)
92
 
@@ -96,16 +105,16 @@ if uploaded_file:
96
 
97
  output_path, log = process_video(temp_input_path)
98
 
99
- st.subheader("\U0001F9EA Processed Output")
100
  st.video(output_path)
101
 
102
  if log:
103
  df = pd.DataFrame(log)
104
- st.write("### \U0001F4CA Detected Faults Table")
105
  st.dataframe(df)
106
- st.download_button("\U0001F4E5 Download Fault Log CSV", convert_df(df), "fault_log.csv", "text/csv")
107
  else:
108
- st.success("\u2705 No faults detected.")
109
 
110
  os.unlink(temp_input_path)
111
  os.unlink(output_path)
 
 
1
  import streamlit as st
2
  import cv2
3
  import numpy as np
 
8
  import torch
9
  from transformers import DetrImageProcessor, DetrForObjectDetection
10
 
11
+ # === Ensure cache directory for model download ===
12
+ os.makedirs("./model_cache", exist_ok=True)
13
+
14
+ # === Load Hugging Face Model ===
15
+ processor = DetrImageProcessor.from_pretrained(
16
+ "NaveenKumar5/Solar_panel_fault_detection",
17
+ cache_dir="./model_cache"
18
+ )
19
+ model = DetrForObjectDetection.from_pretrained(
20
+ "NaveenKumar5/Solar_panel_fault_detection",
21
+ cache_dir="./model_cache"
22
+ )
23
  model.eval()
24
 
25
+ # === Streamlit App ===
26
  st.set_page_config(page_title="Solar Panel Fault Detection", layout="wide")
27
+ st.title("πŸ” Solar Panel Fault Detection (DETR - Hugging Face)")
28
+ st.write("Upload a thermal video (MP4). Faults will be detected using a DETR model from Hugging Face.")
29
 
30
  # === Fault Detection Function ===
31
  def detect_faults(frame, frame_idx, fps):
 
45
  label_id = label.item()
46
  label_name = f"class_{label_id}"
47
 
48
+ # Draw on frame
49
  color = (0, 0, 255)
50
  cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)
51
  cv2.putText(frame, f"{label_name} ({conf:.2f})", (x1, y1 - 5),
 
60
  })
61
  return frame, faults
62
 
63
+ # === Video Processing ===
64
  def process_video(video_path):
65
  cap = cv2.VideoCapture(video_path)
66
  fps = int(cap.get(cv2.CAP_PROP_FPS))
 
90
  writer.release()
91
  return output_path, fault_log
92
 
93
+ # === CSV Download Helper ===
94
  def convert_df(df):
95
  return df.to_csv(index=False).encode('utf-8')
96
 
97
+ # === Streamlit Interface ===
98
+ uploaded_file = st.file_uploader("πŸ“€ Upload thermal video", type=["mp4"])
99
  if uploaded_file:
100
  st.video(uploaded_file)
101
 
 
105
 
106
  output_path, log = process_video(temp_input_path)
107
 
108
+ st.subheader("πŸ§ͺ Processed Output")
109
  st.video(output_path)
110
 
111
  if log:
112
  df = pd.DataFrame(log)
113
+ st.write("### πŸ“Š Detected Faults Table")
114
  st.dataframe(df)
115
+ st.download_button("πŸ“₯ Download Fault Log CSV", convert_df(df), "fault_log.csv", "text/csv")
116
  else:
117
+ st.success("βœ… No faults detected.")
118
 
119
  os.unlink(temp_input_path)
120
  os.unlink(output_path)