File size: 4,935 Bytes
fef5934
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11aea5a
fef5934
 
 
11aea5a
 
 
fef5934
 
 
11aea5a
 
 
 
fef5934
 
 
 
 
 
 
11aea5a
fef5934
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import streamlit as st
import cv2
import numpy as np
import os
import tempfile
from PIL import Image
from ultralytics import YOLO
import re

# ----------- Page config ----------------
st.set_page_config(
    page_title='Fire Detection',
    page_icon='',
    layout='wide'
)

st.title("Fire Detection")
st.write('Upload an image or video to detect Fire')

# -------------- load model detection -----------
@st.cache_resource
def load_model():
    return YOLO('best.pt')

model = load_model()

# switching tabs
x, y = st.tabs(['Image Detection', 'Video Detection'])

# ==================================================
# Image Detection
# ==================================================
with x:
    st.header('Image Detection')

    img_path = st.file_uploader('Please upload an image')

    if img_path is not None:
        image = Image.open(img_path)
        image_np = np.array(image)

        # YOLO inference
        result = model(image_np, conf=0.4)
        annot_img = result[0].plot()

        # Convert BGR to RGB
        annot_img = cv2.cvtColor(annot_img, cv2.COLOR_BGR2RGB)
        annot_img = cv2.cvtColor(annot_img, cv2.COLOR_BGR2RGB)


        # Display side by side
        ori_img, pre_img = st.columns(2)

        with ori_img:
            st.markdown('#### ***Original Image***')
            st.image(image, width=400)

        with pre_img:
            st.markdown('#### ***Detected Image***')
            st.image(annot_img, width=400)

# ==================================================
# Video Detection
# ==================================================
with y:
    st.header("Video Fire Detection")

    video_file = st.file_uploader(
        "Upload a Video",
        type=["mp4", "avi", "mov"]
    )

    if video_file is not None:
        temp_video = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
        temp_video.write(video_file.read())
        temp_video.close()

        cap = cv2.VideoCapture(temp_video.name)

        col1, col2 = st.columns(2)

        with col1:
            st.markdown("#### **Original Video**")
            orig_frame = st.empty()

        with col2:
            st.markdown("#### **Detected Video**")
            pred_frame = st.empty()

        while cap.isOpened():
            ret, frame = cap.read()
            if not ret:
                break

            results = model(frame, conf=0.4)
            annotated_frame = results[0].plot()

            orig_frame.image(frame, channels="BGR", width=400)
            pred_frame.image(annotated_frame, channels="BGR", width=400)

        cap.release()
        os.remove(temp_video.name)
        st.success("Video processing completed")

# ---------------- Sample Test Images & Videos ----------------
st.markdown("---")
st.subheader("Try with Sample Images / Videos")
st.write("Don't have files? Use the samples below to test the model.")

SAMPLE_IMAGES = {
    "Fire Image 1": "pcb1.jpg",
    "Fire Image 2": "pcb4.jpg",
    "Fire Image 3": "pcb5.jpg"
}

SAMPLE_VIDEOS = {
    "Fire Video 1": "v1.mp4",
    "Fire Video 2": "v2.mp4",
    "Fire Video 3": "v3.mp4",
    "Fire Video 4": "v4.mp4"
}


col1, col2 = st.columns(2)

# -------- Sample Images --------
with col1:
    st.markdown("###  Sample Images")
    selected_img = st.selectbox(
        "Choose a sample image",
        ["None"] + list(SAMPLE_IMAGES.keys())
    )

    if selected_img != "None":
        img_path = SAMPLE_IMAGES[selected_img]
        image = Image.open(img_path)
        st.image(image, caption=selected_img, use_container_width=True)

        if st.button("Detect Fire in Image"):
            results = model(image)
            annotated_img = results[0].plot()
            st.image(annotated_img, caption="Detection Result", use_container_width=True)

# -------- Sample Videos --------
with col2:
    st.markdown("### 🎥 Sample Videos")
    selected_vid = st.selectbox(
        "Choose a sample video",
        ["None"] + list(SAMPLE_VIDEOS.keys())
    )

    if selected_vid != "None":
        video_path = SAMPLE_VIDEOS[selected_vid]
        st.video(video_path)

        if st.button("Detect Fire in Video"):
            cap = cv2.VideoCapture(video_path)
            stframe = st.empty()

            while cap.isOpened():
                ret, frame = cap.read()
                if not ret:
                    break

                results = model(frame)
                annotated_frame = results[0].plot()
                stframe.image(annotated_frame, channels="BGR", use_container_width=True)

            cap.release()





# ---------------- Footer ----------------
st.markdown("""
<br>
<div style='text-align:center; padding:12px; background-color:#111111; border-radius:10px;'>
<span style='color:#AAAAAA; font-size:16px;'>
Designed & Developed by <b style='color:#CCCCCC;'>Yedeedya Injeti</b><br>
Under <b style='color:#B8860B;'>Innomatics Research Labs</b>
</span>
</div>
<br>
""", unsafe_allow_html=True)