NaveenKumar5 commited on
Commit
f7daa77
Β·
verified Β·
1 Parent(s): ad232ed

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +30 -23
src/streamlit_app.py CHANGED
@@ -7,16 +7,21 @@ import pandas as pd
7
  from PIL import Image
8
  import torch
9
  from transformers import DetrImageProcessor, DetrForObjectDetection
 
10
 
11
- # === Load Hugging Face Model using default cache (handles permissions safely) ===
12
- processor = DetrImageProcessor.from_pretrained("NaveenKumar5/Solar_panel_fault_detection")
13
- model = DetrForObjectDetection.from_pretrained("NaveenKumar5/Solar_panel_fault_detection")
 
 
 
 
14
  model.eval()
15
 
16
- # === Streamlit App Setup ===
17
  st.set_page_config(page_title="Solar Panel Fault Detection", layout="wide")
18
- st.title("πŸ” Solar Panel Fault Detection (DETR - Hugging Face)")
19
- st.write("Upload a thermal video (MP4). Faults will be detected using a DETR model from Hugging Face.")
20
 
21
  # === Fault Detection Function ===
22
  def detect_faults(frame, frame_idx, fps):
@@ -34,7 +39,7 @@ def detect_faults(frame, frame_idx, fps):
34
  x1, y1, x2, y2 = map(int, box.tolist())
35
  conf = score.item()
36
  label_id = label.item()
37
- label_name = f"class_{label_id}"
38
 
39
  # Draw on frame
40
  color = (0, 0, 255)
@@ -49,9 +54,10 @@ def detect_faults(frame, frame_idx, fps):
49
  "Confidence": round(conf, 2),
50
  "Box": f"({x1},{y1},{x2},{y2})"
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))
@@ -94,21 +100,22 @@ if uploaded_file:
94
  with open(temp_input_path, "wb") as f:
95
  f.write(uploaded_file.read())
96
 
97
- output_path, log = process_video(temp_input_path)
98
-
99
- st.subheader("πŸ§ͺ Processed Output")
100
- st.video(output_path)
101
-
102
- if log:
103
- df = pd.DataFrame(log)
104
- st.write("### πŸ“Š Detected Faults Table")
105
- st.dataframe(df)
106
- st.download_button("πŸ“₯ Download Fault Log CSV", convert_df(df), "fault_log.csv", "text/csv")
107
- else:
108
- st.success("βœ… No faults detected.")
109
-
110
- os.unlink(temp_input_path)
111
- os.unlink(output_path)
 
112
 
113
  st.markdown("---")
114
  st.caption("Built with Streamlit + Hugging Face DETR + OpenCV")
 
7
  from PIL import Image
8
  import torch
9
  from transformers import DetrImageProcessor, DetrForObjectDetection
10
+ from huggingface_hub import login
11
 
12
+ # Get token from environment (set as secret in HF Spaces)
13
+ HUGGINGFACE_TOKEN = os.environ.get("HF_TOKEN")
14
+ login(token=HUGGINGFACE_TOKEN)
15
+
16
+ # === Load Model and Processor ===
17
+ processor = DetrImageProcessor.from_pretrained("NaveenKumar5/Solar_panel_fault_detection", use_auth_token=True)
18
+ model = DetrForObjectDetection.from_pretrained("NaveenKumar5/Solar_panel_fault_detection", use_auth_token=True)
19
  model.eval()
20
 
21
+ # === Streamlit UI Setup ===
22
  st.set_page_config(page_title="Solar Panel Fault Detection", layout="wide")
23
+ st.title("πŸ” Solar Panel Fault Detection (Hugging Face Model)")
24
+ st.write("Upload a thermal video (MP4). Faults will be detected using your Hugging Face DETR model.")
25
 
26
  # === Fault Detection Function ===
27
  def detect_faults(frame, frame_idx, fps):
 
39
  x1, y1, x2, y2 = map(int, box.tolist())
40
  conf = score.item()
41
  label_id = label.item()
42
+ label_name = f"class_{label_id}" # Customize if label mapping is available
43
 
44
  # Draw on frame
45
  color = (0, 0, 255)
 
54
  "Confidence": round(conf, 2),
55
  "Box": f"({x1},{y1},{x2},{y2})"
56
  })
57
+
58
  return frame, faults
59
 
60
+ # === Process Uploaded Video ===
61
  def process_video(video_path):
62
  cap = cv2.VideoCapture(video_path)
63
  fps = int(cap.get(cv2.CAP_PROP_FPS))
 
100
  with open(temp_input_path, "wb") as f:
101
  f.write(uploaded_file.read())
102
 
103
+ try:
104
+ output_path, log = process_video(temp_input_path)
105
+
106
+ st.subheader("πŸ§ͺ Processed Output")
107
+ st.video(output_path)
108
+
109
+ if log:
110
+ df = pd.DataFrame(log)
111
+ st.write("### πŸ“Š Detected Faults Table")
112
+ st.dataframe(df)
113
+ st.download_button("πŸ“₯ Download Fault Log CSV", convert_df(df), "fault_log.csv", "text/csv")
114
+ else:
115
+ st.success("βœ… No faults detected.")
116
+ finally:
117
+ os.unlink(temp_input_path)
118
+ os.unlink(output_path)
119
 
120
  st.markdown("---")
121
  st.caption("Built with Streamlit + Hugging Face DETR + OpenCV")