File size: 1,222 Bytes
cd83a2a dabc461 6e4fcd7 3d83403 4f92b40 cd83a2a a5a461a 73f359f a5a461a 73f359f a5a461a 73f359f 9831883 73f359f 5056ec5 73f359f 5056ec5 73f359f 5056ec5 16eeb0e 5056ec5 73f359f 16eeb0e 535a27e |
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 |
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 |