EdBianchi commited on
Commit
279dc99
1 Parent(s): 50941c2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -0
app.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline as pip
3
+ from PIL import Image
4
+
5
+ # set page setting
6
+ st.set_page_config(page_title='Smoke & Fire Detection')
7
+
8
+ # set history var
9
+ if 'history' not in st.session_state:
10
+ st.session_state.history = []
11
+
12
+ @st.cache(persist=True)
13
+ def loadModel():
14
+ pipeline = pip(task="image-classification", model="EdBianchi/vit-fire-detection")
15
+ return pipeline
16
+
17
+ # PROCESSING
18
+ def compute(image):
19
+ predictions = pipeline(image)
20
+
21
+ with st.container():
22
+ st.image(image, use_column_width=True)
23
+
24
+ with st.container():
25
+ st.write("### Classification Outputs:")
26
+ col1, col2, col6 = st.columns(3)
27
+ col1.metric(predictions[0]['label'], str(round(predictions[0]['score']*100, 1))+"%")
28
+ col2.metric(predictions[1]['label'], str(round(predictions[1]['score']*100, 1))+"%")
29
+ col6.metric(predictions[2]['label'], str(round(predictions[2]['score']*100, 1))+"%")
30
+ return None
31
+
32
+ # INIT
33
+ with st.spinner('Loading the model, this could take some time...'):
34
+ pipeline = loadModel()
35
+
36
+ # TITLE
37
+ st.write("# 🌲 Smoke and Fire in Forests 🌲")
38
+ st.write("""Wildfires or forest fires are **unpredictable catastrophic and destructive** events that affect **rural areas**.
39
+ The impact of these events affects both **vegetation and wildlife**.
40
+
41
+ This application showcases the **vit-fire-detection** model, a version of google **vit-base-patch16-224-in21k** vision transformer fine-tuned for **smoke and fire detection**. In particular, we can imagine a setup in which webcams, drones, or other recording devices **take pictures of a wild environment every t seconds or minutes**. The proposed system is then able to classify the current situation as **normal, smoke, or fire**.
42
+ """)
43
+
44
+ st.write("### Upload an image to see the classifier in action")
45
+ # INPUT IMAGE
46
+ file_name = st.file_uploader("")
47
+ if file_name is not None:
48
+ # USER IMAGE
49
+ image = Image.open(file_name)
50
+ compute(image)
51
+ else:
52
+ # DEMO IMAGE
53
+ demo_img = Image.open("./demo.jpg")
54
+ compute(demo_img)
55
+
56
+ # SIDEBAR
57
+ st.sidebar.write("""
58
+ The fine-tuned model is hosted on the [Hugging Face Hub](https://huggingface.co/EdBianchi/vit-fire-detection).
59
+
60
+ The dataset for fine-tuning process was custom made from different datasets, in particular:
61
+
62
+ - Samples from "train_fire" and samples from "train_smoke" from https://www.kaggle.com/datasets/kutaykutlu/forest-fire?select=train_fire
63
+ - All the samples (mixed together from further splitting) from https://www.kaggle.com/datasets/mohnishsaiprasad/forest-fire-images
64
+
65
+ The custom dataset is hosted on the [Hugging Face Hub](https://huggingface.co/datasets/EdBianchi/SmokeFire).
66
+ """)