File size: 1,872 Bytes
06bd430
 
 
 
 
f20adff
06bd430
f20adff
06bd430
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f20adff
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
import os
import sys
import pandas as pd
import numpy as np
from tqdm import tqdm

import matplotlib.pyplot as plt

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from models import *

import warnings
warnings.filterwarnings("ignore")


# preparing paths for functions
current_directory = os.getcwd()
all_images_path = '.'

# get model directory
model_name = '0508_saved_model_small_sample_vgg16_base.h5'
model_folder = 'models'
model_path = os.path.join(current_directory, model_folder, model_name)


def model_init():
    # set model
    mode = 'predict' #'train' or 'predict'
    model_type = 'vgg16'
    model_tag = 'base'
    model_id = '{:s}_{:s}'.format(model_type, model_tag)
    # set params
    bs = 8
    epochs = 20
    freeze_backbone = True
    
    # init model
    model = FacePrediction(img_dir=all_images_path, model_type=model_type)
    model.define_model(freeze_backbone=freeze_backbone)
    # model.model.summary()
    
    # use our own load model function to load
    model.load_weights(model_path)
    return model
    
def model_predict_bmi(image):
    # Save the uploaded image to a temporary file
    with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp:
        tmp.write(image.read())
        tmp_path = tmp.name
    
    model = model_init()
    bmi = model.predict_external(tmp_path, show_img=False)
    output_bmi = float(bmi[0][0])
    output_bmi = np.round(output_bmi, 3)
    
    # Remove the temporary file
    os.remove(tmp_path)
    
    return output_bmi


# Create the Gradio interface
iface = gr.Interface(
    fn=model_predict_bmi,
    inputs="upload",
    outputs="text",
    title="BMI Prediction",
    description="Upload an image and get the predicted BMI.",
    examples=["lion.jpg", "cheetah.jpg"])

# Run the interface
iface.launch()