Muhamadinsani17 commited on
Commit
f05a99e
·
1 Parent(s): 7705d2a

Upload 4 files

Browse files
Files changed (4) hide show
  1. model_age.keras +3 -0
  2. model_ethnicity.keras +3 -0
  3. model_gender.keras +3 -0
  4. prediction.py +79 -0
model_age.keras ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3cdea63b1fde6c352fd0c827c6bfee2fb1146a8c5a37a71b50f681365ca37954
3
+ size 3870295
model_ethnicity.keras ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9685cab5fd9a4e6c211a3e894f430bd7771ba78c31fa80e8685116b76dfdf49f
3
+ size 3870294
model_gender.keras ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:78aa4430f3fea9d61a918257df06f0026a6646db28a756d1f79174eb8261934e
3
+ size 489293
prediction.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL import Image
2
+ import requests
3
+ from io import BytesIO
4
+ import numpy as np
5
+ import streamlit as st
6
+ from tensorflow.keras.models import load_model
7
+ import io
8
+
9
+ model_gender = load_model('model_gender.keras')
10
+ model_ethnicity = load_model('model_ethnicity.keras')
11
+ model_age = load_model('model_age.keras')
12
+
13
+
14
+ # Mengubah ukuran gambar dan mengonversinya ke mode grayscale
15
+ def preprocess_image(img,):
16
+ img = img.resize((48, 48))
17
+ img_array = np.array(img.convert("L")) # Convert ke mode grayscale
18
+ img_array = img_array.reshape((*(48, 48), 1)) # Menambahkan dimensi kedalam
19
+ img_array = img_array / 255.0 # Normalisasi
20
+ input_image = np.expand_dims(img_array, axis=0)
21
+ classes = model_gender.predict(input_image)
22
+ classes1 = model_ethnicity.predict(input_image)
23
+ classes2 = model_age.predict(input_image)
24
+ idx = np.where(classes >= 0.32, 1, 0).item()
25
+ idx1 = np.argmax(classes1, axis=1).item()
26
+ idx2 = np.argmax(classes2, axis=1).item()
27
+ label = ['Man','Woman']
28
+ label1 = ['White', 'Black', 'Asian', 'Indian', 'Others']
29
+ label2 = ['0-2','3-12', '13-18', '19-60', '60-116']
30
+ return st.write(f'''
31
+ Prediction Gender is a : {label[idx]}
32
+
33
+ Prediction Ethnicity is a : {label1[idx1]}
34
+
35
+ Prediction Age is a : {label2[idx2]}
36
+ ''')
37
+
38
+ def run():
39
+ st.image('https://biology.missouri.edu/sites/default/files/icons/2020-10/noun_community_2739772_1.png')
40
+ st.markdown("<h1 style='text-align: center;'>Welcome to Gender, Ethnicity, and Age Prediction Models</h1>", unsafe_allow_html=True)
41
+ st.markdown("========================================================================================")
42
+ st.write('')
43
+
44
+ option = st.radio("Choose an option:", ["Upload Image", "Use Image URL"])
45
+
46
+ if option == "Upload Image":
47
+ # Unggah gambar dari widget Streamlit
48
+ uploaded_file = st.file_uploader("Choose a file", type=["jpg", "jpeg", "png"])
49
+
50
+ if uploaded_file is not None:
51
+ img = Image.open(uploaded_file)
52
+ preprocess_image(img)
53
+
54
+ image = Image.open(uploaded_file)
55
+ st.image(image, caption="Uploaded Image", use_column_width=True)
56
+
57
+
58
+ elif option == "Use Image URL":
59
+
60
+ # Masukkan URL gambar menggunakan widget input
61
+ image_url = st.text_input("Enter the URL of the Image:")
62
+
63
+ if st.button("Image Prediction"):
64
+ if image_url:
65
+ # Coba memuat dan memproses gambar dari URL
66
+ try:
67
+ response = requests.get(image_url)
68
+ img = Image.open(BytesIO(response.content))
69
+ preprocess_image(img)
70
+
71
+ image = Image.open(io.BytesIO(requests.get(image_url).content))
72
+ st.image(image, caption="Image from URL", use_column_width=True)
73
+
74
+ except Exception as e:
75
+ st.error(f"Error: {e}")
76
+ else:
77
+ st.warning("Enter the image URL first.")
78
+
79
+