File size: 3,044 Bytes
7c4fac8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
231fa06
485a795
 
 
 
 
 
 
 
 
7c4fac8
 
 
485a795
 
 
 
 
 
 
 
 
 
 
 
 
 
da59ce7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
485a795
7c4fac8
 
485a795
 
 
dc6761e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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)