Spaces:
Runtime error
Runtime error
Commit ·
06bd430
1
Parent(s): 4c138bb
First Trial
Browse files
app.py
CHANGED
|
@@ -1,7 +1,75 @@
|
|
| 1 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
return "Hello " + name + "!!"
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
iface.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import numpy as np
|
| 5 |
+
from tqdm import tqdm
|
| 6 |
|
| 7 |
+
import matplotlib.pyplot as plt
|
|
|
|
| 8 |
|
| 9 |
+
import tensorflow as tf
|
| 10 |
+
from tensorflow import keras
|
| 11 |
+
from tensorflow.keras.preprocessing.image import ImageDataGenerator
|
| 12 |
+
from models import *
|
| 13 |
+
|
| 14 |
+
import warnings
|
| 15 |
+
warnings.filterwarnings("ignore")
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
# preparing paths for functions
|
| 19 |
+
current_directory = os.getcwd()
|
| 20 |
+
all_images_path = '.'
|
| 21 |
+
|
| 22 |
+
# get model directory
|
| 23 |
+
model_name = '0508_saved_model_small_sample_vgg16_base.h5'
|
| 24 |
+
model_folder = 'models'
|
| 25 |
+
model_path = os.path.join(current_directory, model_folder, model_name)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def model_init():
|
| 29 |
+
# set model
|
| 30 |
+
mode = 'predict' #'train' or 'predict'
|
| 31 |
+
model_type = 'vgg16'
|
| 32 |
+
model_tag = 'base'
|
| 33 |
+
model_id = '{:s}_{:s}'.format(model_type, model_tag)
|
| 34 |
+
# set params
|
| 35 |
+
bs = 8
|
| 36 |
+
epochs = 20
|
| 37 |
+
freeze_backbone = True
|
| 38 |
+
|
| 39 |
+
# init model
|
| 40 |
+
model = FacePrediction(img_dir=all_images_path, model_type=model_type)
|
| 41 |
+
model.define_model(freeze_backbone=freeze_backbone)
|
| 42 |
+
# model.model.summary()
|
| 43 |
+
|
| 44 |
+
# use our own load model function to load
|
| 45 |
+
model.load_weights(model_path)
|
| 46 |
+
return model
|
| 47 |
+
|
| 48 |
+
def model_predict_bmi(image):
|
| 49 |
+
# Save the uploaded image to a temporary file
|
| 50 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp:
|
| 51 |
+
tmp.write(image.read())
|
| 52 |
+
tmp_path = tmp.name
|
| 53 |
+
|
| 54 |
+
model = model_init()
|
| 55 |
+
bmi = model.predict_external(tmp_path, show_img=False)
|
| 56 |
+
output_bmi = float(bmi[0][0])
|
| 57 |
+
output_bmi = np.round(output_bmi, 3)
|
| 58 |
+
|
| 59 |
+
# Remove the temporary file
|
| 60 |
+
os.remove(tmp_path)
|
| 61 |
+
|
| 62 |
+
return output_bmi
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
# Create the Gradio interface
|
| 66 |
+
iface = gr.Interface(
|
| 67 |
+
fn=model_predict_bmi,
|
| 68 |
+
inputs="upload",
|
| 69 |
+
outputs="text",
|
| 70 |
+
title="BMI Prediction",
|
| 71 |
+
description="Upload an image and get the predicted BMI.",
|
| 72 |
+
examples=["lion.jpg", "cheetah.jpg"])
|
| 73 |
+
|
| 74 |
+
# Run the interface
|
| 75 |
iface.launch()
|