FacialScores / app.py
markchiang's picture
Create app.py
5b87ea7
raw
history blame
2.5 kB
import cv2
import numpy as np
import urllib
import tensorflow as tf
from tensorflow.keras import layers, models, optimizers
import gradio as gr
# Load pre-trained model
model = tf.keras.models.load_model('facial_condition_model.h5')
# Function to preprocess image for model input
def preprocess_image(image):
# Convert image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Resize image to 128x128 pixels
resized = cv2.resize(gray, (128, 128), interpolation=cv2.INTER_AREA)
# Convert image to float32 and normalize pixel values
normalized = np.float32(resized) / 255.0
# Add batch dimension to image
batched = np.expand_dims(normalized, axis=0)
return batched
# Function to calculate facial condition scores
def calculate_scores(image):
# Preprocess image for model input
preprocessed_image = preprocess_image(image)
# Use pre-trained model to predict facial condition scores
score = model.predict(preprocessed_image)[0][0]
# Calculate wrinkle score
wrinkle_score = 1 - (1 / (1 + np.exp(-5*(score-50)/50)))
# Calculate laxity score
laxity_score = np.tanh(score / 10)
# Calculate facial age
facial_age = int(np.round(score / 10) * 10)
# Calculate final score
final_score = np.mean([wrinkle_score, laxity_score, facial_age])
return wrinkle_score, laxity_score, facial_age, final_score
# Define Gradio interface
inputs = gr.inputs.Image(label="Input Image")
outputs = [gr.outputs.Label(label="Wrinkle Score"),
gr.outputs.Label(label="Laxity Score"),
gr.outputs.Label(label="Facial Age"),
gr.outputs.Label(label="Final Score")]
title = "Facial Condition Evaluator"
description = "Upload a photo to evaluate your facial condition. This tool will calculate your wrinkle score, laxity score, facial age, and a final score based on a deep learning model."
examples = [["https://media.licdn.com/dms/image/C5603AQEiW9vZPeekiw/profile-displayphoto-shrink_800_800/0/1516317900912?e=2147483647&v=beta&t=Rs0LpxDfsrHmQFzD0w1vRpCRln0q-sPWrAY9lgiqYeU"],
["https://scontent.ftpe8-1.fna.fbcdn.net/v/t1.6435-9/195542178_605699050027_1477839146608127555_n.jpg?_nc_cat=108&ccb=1-7&_nc_sid=dbeb18&_nc_ohc=fUhwWIuKSb0AX_Irg8T&_nc_ht=scontent.ftpe8-1.fna&oh=00_AfAg2ep5yEM_fzNubC3hS7P22hGWq_rR0WluvTBRkrSGvA&oe=6465EF97"]]
iface = gr.Interface(calculate_scores, inputs, outputs, title=title, description=description, examples=examples)
# Launch Gradio
iface.launch()