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