ianyusuf commited on
Commit
c79b110
Β·
verified Β·
1 Parent(s): b351249

Upload 5 files

Browse files
Files changed (4) hide show
  1. app.py +38 -0
  2. eda.py +101 -0
  3. model.pkl +3 -0
  4. prediction.py +63 -0
app.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import eda
3
+ import prediction
4
+
5
+ sc = st.sidebar.image('fire-res.png')
6
+
7
+ # Sidebar Navigation
8
+ st.sidebar.markdown("### **Fire Res National Forestry Protector**")
9
+ st.sidebar.markdown("Protect the forests, Protect the planet")
10
+
11
+ # Radio button
12
+ nav = st.sidebar.radio("Navigation", options=[
13
+ "πŸ“Š Exploratory Data Analysis",
14
+ "πŸ” Model Prediction"
15
+ ])
16
+
17
+ # Navigation function
18
+ if nav == "πŸ“Š Exploratory Data Analysis":
19
+ eda.run()
20
+ elif nav == "πŸ” Model Prediction":
21
+ prediction.run()
22
+
23
+ # Sidebar Section
24
+ st.sidebar.markdown("---")
25
+ st.sidebar.markdown("### πŸ“Š **About the Model**")
26
+
27
+ # Accuracy
28
+ st.sidebar.markdown("🎯 **Model Accuracy:**")
29
+ model_accuracy = 0.8
30
+ st.sidebar.progress(model_accuracy)
31
+ st.sidebar.write(f"{model_accuracy * 100:.2f}%")
32
+
33
+ # Accuracy Information
34
+ st.sidebar.markdown(
35
+ "ℹ️ This model successfully classifies 80% of the images correctly, showing quite good performance in distinguishing between fire, non-fire, and smoke classes."
36
+ )
37
+
38
+
eda.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import seaborn as sns
4
+ import matplotlib.pyplot as plt
5
+ from PIL import Image
6
+
7
+ def run():
8
+
9
+ #Title
10
+ st.title('🌳 Forest Fire & Smoke Detection')
11
+
12
+ st.write('Forest fires are one of the environmental disasters that have a major impact on life. This disaster can cause ecosystem damage, air pollution, and threaten human safety. One solution that can be used for early mitigation is to utilize Computer Vision technology to automatically detect the presence of fire and smoke through images.')
13
+
14
+ #Image
15
+ image = Image.open('forest.png')
16
+ st.image(image)
17
+
18
+ # Dataset Distribution
19
+ st.write('## πŸ“Š Exploratory Data Analysis')
20
+ st.markdown('---')
21
+
22
+ data = {
23
+ "Class": [
24
+ "Fire", "Non-Fire", "Smoke"
25
+ ],
26
+ "Number of Images": [1000, 1000, 1000]
27
+ }
28
+
29
+ df = pd.DataFrame(data)
30
+
31
+ # Dataset from Google Drive
32
+ images_by_class = {
33
+ "fire": [
34
+ "fire/fire1.jpg",
35
+ "fire/fire2.png",
36
+ "fire/fire3.jpeg",
37
+ "fire/fire4.png",
38
+ "fire/fire5.png"
39
+ ],
40
+ "non-fire": [
41
+ "non-fire/non-fire1.png",
42
+ "non-fire/non-fire2.png",
43
+ "non-fire/non-fire3.png",
44
+ "non-fire/non-fire4.jpg",
45
+ "non-fire/non-fire5.png"
46
+ ],
47
+ "smoke": [
48
+ "smoke/smoke1.png",
49
+ "smoke/smoke2.jpg",
50
+ "smoke/smoke3.jpg",
51
+ "smoke/smoke4.png",
52
+ "smoke/smoke5.jpg"
53
+ ]
54
+ }
55
+
56
+ st.write("## Forest Fire Image Samples")
57
+
58
+ # Display in grid
59
+ for cls, image_paths in images_by_class.items():
60
+ st.subheader(cls.capitalize())
61
+ cols = st.columns(len(image_paths))
62
+ for i, path in enumerate(image_paths):
63
+ img = Image.open(path)
64
+ with cols[i]:
65
+ st.image(img, use_container_width=True)
66
+
67
+ st.write(" ")
68
+
69
+ # Insight text
70
+ st.markdown("""
71
+ **Fire Image**: Defined by the dominant colors of bright red and orange, often accompanied by forms of burning flames or spreading flames. The characteristic of this class is the high intensity of light and a clearly visible burning pattern defining the presence of an active fire.
72
+
73
+ **Smoke Image**: Defined by a whitish gray color that covers the image area. Its shape obscures the objects behind it. The uniqueness of this class lies in its opaque texture and transparent gradation, which indicates the presence of fire smoke.
74
+
75
+ **Non-fire Image**: Defined as an image without any indication of fire or smoke, usually showing a calm forest scene with a blue sky. There are no flaming shapes or smoke haze. Its visual calmness clearly distinguishes it from the other two classes.
76
+ """)
77
+
78
+ st.markdown('---')
79
+
80
+ # Pie Chart
81
+ st.subheader("Class Distribution in Training and Test Set")
82
+
83
+ fig, ax = plt.subplots()
84
+ ax.pie(df["Number of Images"],
85
+ labels=df["Class"],
86
+ autopct='%1.1f%%',
87
+ startangle=140)
88
+ ax.axis('equal') # Equal aspect ratio ensures pie is drawn as a circle
89
+ st.pyplot(fig)
90
+
91
+ # Insight text
92
+ st.markdown("""
93
+ The figure above shows the data distribution in the training set for three classes: fire, smoke, and non-fire.
94
+
95
+ Each class has almost the same proportion, which is around 33.3%.
96
+
97
+ This shows that the dataset is balanced, so the model does not need to be given special treatment such as oversampling or undersampling to overcome class imbalance (imbalanced data).
98
+ """)
99
+
100
+ if __name__ == '__main__':
101
+ run()
model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:28981fcbef2b314afb20a7ac75094d68587ee2c522ce8dcc1f8e105836ae3c84
3
+ size 16784
prediction.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pickle
3
+ import pandas as pd
4
+ from PIL import Image
5
+
6
+ with open('model.pkl', 'rb') as model:
7
+ model = pickle.load(model)
8
+
9
+ def run():
10
+
11
+ #Title
12
+ st.title('πŸ” Forest Fire & Smoke Prediction')
13
+ st.write('Upload your own image')
14
+
15
+ images_by_class = {
16
+ "": [
17
+ "fire/fire1.jpg",
18
+ "non-fire/non-fire1.png",
19
+ "smoke/smoke4.png"
20
+ ]
21
+ }
22
+
23
+ # Display in grid
24
+ for cls, image_paths in images_by_class.items():
25
+ st.subheader(cls.capitalize())
26
+ cols = st.columns(len(image_paths))
27
+ for i, path in enumerate(image_paths):
28
+ img = Image.open(path)
29
+ with cols[i]:
30
+ st.image(img, use_container_width=True)
31
+
32
+ # Upload image
33
+ uploaded_file = st.file_uploader(
34
+ "Upload your own image",
35
+ type=["jpg", "jpeg", "png"],
36
+ label_visibility="visible"
37
+ )
38
+
39
+ # Upload file
40
+ if uploaded_file is not None:
41
+ # Show image
42
+ img = Image.open(uploaded_file).convert('RGB')
43
+ st.image(img, caption="Uploaded Image", use_container_width=True)
44
+
45
+ # Preprocess
46
+ img_resized = img.resize((150, 150))
47
+ img_array = np.expand_dims(np.array(img_resized) / 255.0, axis=0)
48
+
49
+ # Load model
50
+ model = tf.keras.models.load_model("best_model.h5")
51
+ class_names = ["fire", "non-fire", "smoke"]
52
+
53
+ # Predict
54
+ prediction = model.predict(img_array)
55
+ predicted_label = class_names[np.argmax(prediction)]
56
+ confidence = np.max(prediction)
57
+
58
+ # Show Prediction
59
+ st.success(f"βœ… Prediction: **{predicted_label}**")
60
+ st.write(f"Confidence: **{confidence:.2%}**")
61
+
62
+ if __name__ == '__main__':
63
+ run()