import gradio as gr from keras.models import model_from_json import keras import numpy as np def predict_age_gender(image): json_file = open('final_mobilenet.json', 'r') loaded_file_json = json_file.read() json_file.close() model = model_from_json(loaded_file_json) model.load_weights('final_mobilenet.h5') # img_pixels = image.img_to_array(image) # img = tf.reshape(image, shape=(-1, 128, 128, 3)) img_pixels = np.expand_dims(image, axis=0) img_pixels = image.astype('float') img_pixels = img_pixels.reshape((1, 128, 128, 3)) img_pixels /= 255 predict = model.predict(img_pixels) gender_predict = predict[0] age_predict = predict[1] return {'Gender': ['Fmale' if gender_predict > 0.5 else 'Male'], 'Age': age_predict[0][0]} iface = gr.Interface(predict_age_gender, gr.Image(), gr.Text()) iface.launch(share=True)