Spaces:
Build error
Build error
import cv2 | |
import numpy as np | |
import urllib | |
import tensorflow as tf | |
from tensorflow.keras import layers, models, optimizers | |
import gradio as gr | |
import os | |
# 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)) | |
# 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 = [["before.jpg"], | |
["after.jpg"]] | |
iface = gr.Interface(calculate_scores, inputs, outputs, title=title, description=description, examples=examples) | |
# Launch Gradio | |
iface.launch() |