File size: 2,089 Bytes
5b87ea7
 
 
 
 
 
b05691d
5b87ea7
b05691d
 
5b87ea7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ea56f13
5b87ea7
 
 
 
 
 
 
 
 
 
 
 
eeb64db
 
5b87ea7
 
 
 
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
45
46
47
48
49
50
51
52
53
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()