|
import gradio as gr |
|
import tensorflow as tf |
|
import numpy as np |
|
import os |
|
import pandas as pd |
|
|
|
|
|
model_gender = tf.keras.models.load_model('model_gender.h5') |
|
model_age = tf.keras.models.load_model('model_age.h5') |
|
|
|
actual_data = { |
|
"000000.png": {"Image": 1,"age": 61.0, "gender": "Female"}, |
|
"000001.png": {"Image": 2,"age": 63.0, "gender": "Male"}, |
|
"000002.png": {"Image": 3,"age": 45.0, "gender": "Male"}, |
|
"000003.png": {"Image": 4,"age": 59.0, "gender": "Female"}, |
|
"000004.png": {"Image": 5,"age": 37.0, "gender": "Female"} |
|
} |
|
|
|
df = pd.DataFrame(actual_data).T |
|
|
|
data = {'Name': ['Accuracy', 'Precision', 'Recall', 'F1-score'], |
|
'Value': [96.11 , 0.9368, 0.9731, 0.9546]} |
|
|
|
df1 = pd.DataFrame(data) |
|
|
|
def preprocess_image(image): |
|
|
|
img = image.convert('L') |
|
img = img.resize((128, 128)) |
|
img = np.array(img) / 255.0 |
|
img = img.reshape((1, 128, 128, 1)) |
|
return img |
|
|
|
def predict(image): |
|
preprocessed_image = preprocess_image(image) |
|
gender_pred = model_gender.predict(preprocessed_image)[0][0] |
|
age_pred = model_age.predict(preprocessed_image)[0][0] |
|
gender = "Male" if gender_pred > 0.68 else "Female" |
|
list = "{:.2f}".format(age_pred),gender,df |
|
return list |
|
|
|
|
|
|
|
|
|
text_age = gr.components.Textbox(label="Predicted Age") |
|
text_gender = gr.components.Textbox(label="Predicted Gender") |
|
|
|
def predictor_tab(): |
|
interface = gr.Interface(predict, gr.components.Image(height=440,width=1000,label="Upload Image", type="pil"), |
|
outputs=[text_age, text_gender, gr.DataFrame(value=df)], |
|
examples=[ |
|
os.path.join(os.path.dirname(__file__),"00000.png"), |
|
os.path.join(os.path.dirname(__file__),"00001.png"), |
|
os.path.join(os.path.dirname(__file__),"00002.png"), |
|
os.path.join(os.path.dirname(__file__),"00003.png"), |
|
os.path.join(os.path.dirname(__file__),"00004.png")], |
|
|
|
allow_flagging='never') |
|
|
|
return interface |
|
|
|
def about_tab(): |
|
with gr.Blocks() as about: |
|
|
|
gr.Markdown("# Age and Gender Prediction with Deep Learning!") |
|
gr.Markdown("This awesome app uses deep learning magic β¨ to predict someone's age and gender based on a x-ray image! Just upload a photo, and our clever models will do their best detective work to unveil the mystery.") |
|
|
|
|
|
with gr.Row(): |
|
with gr.Column(): |
|
gr.Markdown("**DATASET π**") |
|
gr.Markdown( |
|
""" |
|
The lung scans used in this project come from a publicly available dataset. |
|
It contains approximately 10,700 scans for training and 11,700 scans for testing. |
|
This dataset was part of a competition held by The Radiology and Diagnostic Imaging Society of SΓ£o Paulo (SPR) with Amazon Web Services. |
|
""" |
|
) |
|
gr.Text("https://www.kaggle.com/datasets/felipekitamura/spr-x-ray-age-and-gender-dataset",label="link") |
|
|
|
|
|
gr.Markdown("**Model Performance π‘**") |
|
table = gr.DataFrame(value=df1) |
|
gr.Markdown("β **Model Accuracy for Genders**") |
|
gender_img = 'gender.png' |
|
gr.Image(value=gender_img, width=500, height=450) |
|
gr.Markdown("β **Model Accuracy for Age**") |
|
age_img = 'age.png' |
|
gr.Image(value=age_img, width=500, height=450) |
|
gr.Markdown("β **Confusion matrix**") |
|
matrix_img = 'matrix.png' |
|
gr.Image(value=matrix_img, width=500, height=450) |
|
|
|
|
|
|
|
|
|
|
|
with gr.Row(): |
|
with gr.Column(): |
|
gr.Markdown("**Created by π€**") |
|
gr.Markdown("Uday Jawheri") |
|
with gr.Row(): |
|
gr.Text("https://www.linkedin.com/in/uday-jawheri/", label="LinkedIn") |
|
gr.Text("https://xudayx.github.io/Portfolio/", label="Website") |
|
|
|
return about |
|
|
|
|
|
with gr.Blocks(theme=gr.themes.Soft(), title="Age and Gender Prediction") as app: |
|
with gr.Tab("Predictor"): |
|
predictor_tab() |
|
|
|
with gr.Tab("About"): |
|
about_tab() |
|
|
|
|
|
app.launch() |