bf_predictor / app.py
SegevC's picture
Update app.py
da59ce7
from fastai import *
from fastcore.all import *
from fastai.vision.all import *
import pandas as pd
import re
import gradio as gr
def get_x(data_set):
return Path(data_set['image_path'])
def get_y(data_set):
return data_set['bf_est']
def new_splitter(df):
# Get the unique values in the 'id' column
unique_ids = df['id'].unique()
# Shuffle the unique values
np.random.seed(42)
np.random.shuffle(unique_ids)
# Calculate the number of unique values to be included in the first dataframe
num_unique_in_test = int(np.ceil(len(unique_ids) * 0.8))
# Get the first 'num_unique_in_df1' unique values
test_ids = unique_ids[:num_unique_in_test]
# Get the rows of the original dataframe that contain the 'df1_ids'
test = df.index[df['id'].isin(test_ids)].tolist()
# Get the rest of the rows from the original dataframe
valid = df.index[~df['id'].isin(test_ids)].tolist()
return test, valid
title = "Body Fat Predictor"
description = "A Body Fat Predictor trained on the subreddit \"guessmybf\". \
For best preformence upload a front facing photo. \n The Range button allows \
you to adjust the range of the predicted body fat percentage. \
Choosing a higher range results in a less specific prediction but a more ecurate estimate, \
while a lower range provides a less precise estimate. \n The Examples section \
provides visual examples of different body fat percentages and how they appear \
on an individual's body. It is important to note that two individuals can have \
the same body fat percentage but look different due to differences in body composition \
(such as muscle mass)."
learner = load_learner("bf_model.pkl")
# def predict_bf(img):
# return round(float(learner.predict(img)[1]),2)
def predict_bf(img, range=0):
if range not in [0,1,2,3,4]:
range = 1
prediction = math.floor(float(learner.predict(img)[1]))
if range ==0:
return f"{str(prediction)}%"
else:
return f"{str(round(prediction) - range)}-{str(round(prediction) + range)}%"
examples=[
['./exp/6-8.jpg', '6-8%'],
['./exp/7-9.jpg', '7-9%'],
['./exp/9-11.jpg', '9-11%'],
['./exp/9-11v2.jpg', '9-11%'],
['./exp/10-12.jpg', '10-12%'],
['./exp/10-12v2.jpg', '10-12%'],
['./exp/11-13.jpg', '11-13%'],
['./exp/13-15.jpg', '13-15%'],
['./exp/13-15v2.jpg', '13-15%'],
['./exp/15-17.jpg', '15-17%'],
['./exp/15-17v2.jpg', '15-17%'],
['./exp/15-17v3.jpg', '15-17%'],
['./exp/17-19.jpg', '17-19%'],
['./exp/19-20.jpg', '19-20%'],
['./exp/19-20v2.jpg', '19-20%'],
['./exp/21-23.jpg', '21-23%'],
['./exp/22-24.jpg', '22-24%'],
['./exp/23-25.jpg', '23-25%'],
['./exp/24-26.jpg', '24-26%']
]
image = gr.Image(shape=(192,192))
intf = gr.Interface(fn =predict_bf, inputs = [image, gr.Radio([0,1,2,3,4])], outputs = gr.outputs.Textbox(),
title = title, description = description, examples=examples)
intf.launch(inline=True, share=False)