Keziaa commited on
Commit
1424c0a
1 Parent(s): 9b9342f

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -0
app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import numpy as np
3
+ from PIL import Image
4
+ import tensorflow as tf
5
+
6
+ model = tf.keras.models.load_model('model sequential improve.h5')
7
+
8
+ # Custom function to load and predict label for the image
9
+ def predict(img_rel_path):
10
+ # Import Image from the path with size of (300, 300)
11
+ img = Image.open(img_rel_path).resize((150, 150))
12
+
13
+ # Convert Image to a numpy array
14
+ img = np.array(img)
15
+
16
+ # Scaling the Image Array values between 0 and 1
17
+ img = img / 255.0
18
+
19
+ # Get the Predicted Label for the loaded Image
20
+ p = model.predict(img[np.newaxis, ...])
21
+
22
+ # Label array
23
+ labels = {0: 'baby', 1: 'kid', 2: 'young', 3: 'adult'}
24
+
25
+ predicted_class = labels[np.argmax(p[0], axis=-1)]
26
+
27
+ classes=[]
28
+ prob=[]
29
+ for i,j in enumerate (p[0],0):
30
+ classes.append(labels[i])
31
+ prob.append(round(j*100,2))
32
+
33
+ return predicted_class, classes, prob
34
+
35
+ def main():
36
+ st.title("Face Detection")
37
+ uploaded_file = st.file_uploader("Choose a file")
38
+ if uploaded_file is not None:
39
+ image = Image.open(uploaded_file)
40
+ st.image(image, caption='Uploaded Image', use_column_width=True)
41
+ if st.button("Predict"):
42
+ class_, classes, prob = predict(uploaded_file)
43
+ st.write("Age:", class_)
44
+ st.write("Predict:")
45
+ for i in range(len(classes)):
46
+ st.write(f"{classes[i].upper()}: {prob[i]}%")
47
+
48
+ if __name__ == "__main__":
49
+ main()