farid678's picture
Update app.py
535a27e verified
raw
history blame contribute delete
No virus
1.22 kB
import gradio as gr
from keras.models import model_from_json
from tensorflow.keras.preprocessing import image
import numpy as np
def predict_age_gender(image):
json_file_2 = open('model_gender_2.json', 'r')
loaded_file_json_2 = json_file_2.read()
json_file_2.close()
model_2 = model_from_json(loaded_file_json_2)
model_2.load_weights('model_gender_2.h5')
json_file = open('xception.json', 'r')
loaded_file_json = json_file.read()
json_file.close()
model = model_from_json(loaded_file_json)
model.load_weights('xception_modelv2.h5')
images = np.resize(image, new_shape=(1, 128, 128, 3))
img_pixels = np.expand_dims(images, axis=0)
img_pixels = img_pixels.astype('float64')
img_pixels = img_pixels.reshape((1, 128, 128, 3))
img_pixels /= 255
class_names_gender = ['Fmale', 'Male']
gender_predict = model_2.predict(img_pixels)
predict = model.predict(img_pixels)
age_result = predict[1][0].round()
return 'Female' if gender_predict<0.5 else 'Male', age_result[0]
iface = gr.Interface(predict_age_gender, gr.Image(), [gr.Label(label='Gender'), gr.Text(label='Age')])
iface.launch()
#this is the end