Umang427 commited on
Commit
4fa9cfc
Β·
verified Β·
1 Parent(s): ff7dd16

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -75
app.py CHANGED
@@ -1,75 +1,76 @@
1
- import streamlit as st
2
- import numpy as np
3
- from PIL import Image
4
- import os
5
- from ultralytics import YOLO
6
-
7
- # ── PAGE CONFIG ─────────────────────────────
8
- st.set_page_config(
9
- page_title="SAR Ship Detection",
10
- page_icon="πŸ›°οΈ",
11
- layout="wide"
12
- )
13
-
14
- st.title("πŸ›°οΈ SAR Ship Detection")
15
-
16
- # ── LOAD MODEL ─────────────────────────────
17
- @st.cache_resource
18
- def load_model():
19
- try:
20
- BASE_DIR = os.path.dirname(os.path.abspath(__file__))
21
- MODEL_PATH = os.path.join(BASE_DIR, "model", "best.pt")
22
-
23
- if not os.path.exists(MODEL_PATH):
24
- st.error(f"Model not found at: {MODEL_PATH}")
25
- return None
26
-
27
- model = YOLO(MODEL_PATH)
28
- return model
29
-
30
- except Exception as e:
31
- st.error(f"Error loading model: {e}")
32
- return None
33
-
34
-
35
- model = load_model()
36
-
37
- # ── IMAGE UPLOAD ───────────────────────────
38
- uploaded_file = st.file_uploader(
39
- "Upload SAR Image",
40
- type=["png", "jpg", "jpeg", "tif", "tiff"]
41
- )
42
-
43
- # ── DETECTION ─────────────────────────────
44
- if uploaded_file is not None:
45
-
46
- image = Image.open(uploaded_file).convert("RGB")
47
- st.subheader("Original Image")
48
- st.image(image, use_container_width=True)
49
-
50
- if model is None:
51
- st.error("Model not loaded.")
52
- else:
53
- img_np = np.array(image)
54
-
55
- with st.spinner("Running detection..."):
56
- results = model.predict(img_np, conf=0.35)
57
-
58
- result = results[0]
59
-
60
- # Annotated image
61
- annotated = result.plot()
62
- annotated = annotated[:, :, ::-1]
63
-
64
- st.subheader("Detection Result")
65
- st.image(annotated, use_container_width=True)
66
-
67
- # Detection info
68
- st.subheader("Detections")
69
- if len(result.boxes) == 0:
70
- st.write("No ships detected.")
71
- else:
72
- for i, box in enumerate(result.boxes):
73
- conf = float(box.conf[0])
74
- cls = int(box.cls[0])
75
- st.write(f"Ship {i+1} β†’ Confidence: {conf:.2f}")
 
 
1
+ import streamlit as st
2
+ import numpy as np
3
+ from PIL import Image
4
+ import os
5
+ from ultralytics import YOLO
6
+
7
+ # ── PAGE CONFIG ─────────────────────────────
8
+ st.set_page_config(
9
+ page_title="SAR Ship Detection",
10
+ page_icon="πŸ›°οΈ",
11
+ layout="wide"
12
+ )
13
+
14
+ st.title("πŸ›°οΈ SAR Ship Detection")
15
+
16
+ # ── LOAD MODEL ─────────────────────────────
17
+ @st.cache_resource
18
+ def load_model():
19
+ try:
20
+ BASE_DIR = os.path.dirname(os.path.abspath(__file__))
21
+ MODEL_PATH = os.path.join(BASE_DIR, "model", "best.pt")
22
+
23
+ if not os.path.exists(MODEL_PATH):
24
+ st.error(f"Model not found at: {MODEL_PATH}")
25
+ return None
26
+
27
+ model = YOLO(MODEL_PATH)
28
+ return model
29
+
30
+ except Exception as e:
31
+ st.error(f"Error loading model: {e}")
32
+ return None
33
+
34
+
35
+ model = load_model()
36
+
37
+ # ── IMAGE UPLOAD ───────────────────────────
38
+ uploaded_file = st.file_uploader(
39
+ "Upload SAR Image",
40
+ type=["png", "jpg", "jpeg", "tif", "tiff"]
41
+ )
42
+
43
+ # ── DETECTION ─────────────────────────────
44
+ if uploaded_file is not None:
45
+
46
+ image = Image.open(uploaded_file).convert("RGB")
47
+ st.subheader("Original Image")
48
+ st.image(image, use_container_width=True)
49
+
50
+ if model is None:
51
+ st.error("Model not loaded.")
52
+ else:
53
+ img_np = np.array(image)
54
+
55
+ with st.spinner("Running detection..."):
56
+ results = model.predict(img_np, conf=0.35)
57
+
58
+ result = results[0]
59
+
60
+ # Annotated image
61
+ annotated = result.plot()
62
+ annotated = annotated[:, :, ::-1]
63
+
64
+ st.subheader("Detection Result")
65
+ st.image(annotated, use_container_width=True)
66
+
67
+ # Detection info
68
+ st.subheader("Detections")
69
+ if len(result.boxes) == 0:
70
+ st.write("No ships detected.")
71
+ else:
72
+ for i, box in enumerate(result.boxes):
73
+ conf = float(box.conf[0])
74
+ cls = int(box.cls[0])
75
+ st.write(f"Ship {i+1} β†’ Confidence: {conf:.2f}")
76
+ st.write("Model status:", "Loaded βœ…" if model else "Not Loaded ❌")