Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image
|
3 |
+
from tensorflow.keras.models import load_model
|
4 |
+
import tensorflow as tf
|
5 |
+
import numpy as np
|
6 |
+
from keras.preprocessing.image import img_to_array
|
7 |
+
import os
|
8 |
+
|
9 |
+
|
10 |
+
@st.cache_resource
|
11 |
+
def load_model():
|
12 |
+
model_path = "model.keras" # Update with the absolute file path
|
13 |
+
return tf.keras.models.load_model(model_path)
|
14 |
+
|
15 |
+
model = load_model()
|
16 |
+
|
17 |
+
def prepare_image(img):
|
18 |
+
img = img.resize((220, 220))
|
19 |
+
img_array = img_to_array(img)
|
20 |
+
img_array = np.expand_dims(img_array, axis=0)
|
21 |
+
|
22 |
+
prediction = model.predict(img_array)
|
23 |
+
predicted_class = "Smoking" if prediction > 0.5 else "Not Smoking"
|
24 |
+
|
25 |
+
return predicted_class, prediction[0]
|
26 |
+
|
27 |
+
def run():
|
28 |
+
st.title("Smoking or Not Smoking Detection")
|
29 |
+
img_file = st.file_uploader("Choose an Image", type=["jpg", "png"])
|
30 |
+
|
31 |
+
if img_file is not None:
|
32 |
+
img = Image.open(img_file).resize((250, 250))
|
33 |
+
st.image(img, use_column_width=False)
|
34 |
+
|
35 |
+
# Create the directory if it doesn't exist
|
36 |
+
upload_dir = './upload_images/'
|
37 |
+
os.makedirs(upload_dir, exist_ok=True)
|
38 |
+
|
39 |
+
save_image_path = os.path.join(upload_dir, img_file.name)
|
40 |
+
with open(save_image_path, "wb") as f:
|
41 |
+
f.write(img_file.getbuffer())
|
42 |
+
|
43 |
+
predicted_class, score = prepare_image(img)
|
44 |
+
st.success(f"**Predicted : {predicted_class}, Score: {score}**")
|
45 |
+
|
46 |
+
run()
|