markchiang commited on
Commit
5b87ea7
·
1 Parent(s): d8b103d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -0
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+ import urllib
4
+ import tensorflow as tf
5
+ from tensorflow.keras import layers, models, optimizers
6
+ import gradio as gr
7
+
8
+ # Load pre-trained model
9
+ model = tf.keras.models.load_model('facial_condition_model.h5')
10
+
11
+ # Function to preprocess image for model input
12
+ def preprocess_image(image):
13
+ # Convert image to grayscale
14
+ gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
15
+ # Resize image to 128x128 pixels
16
+ resized = cv2.resize(gray, (128, 128), interpolation=cv2.INTER_AREA)
17
+ # Convert image to float32 and normalize pixel values
18
+ normalized = np.float32(resized) / 255.0
19
+ # Add batch dimension to image
20
+ batched = np.expand_dims(normalized, axis=0)
21
+ return batched
22
+
23
+ # Function to calculate facial condition scores
24
+ def calculate_scores(image):
25
+ # Preprocess image for model input
26
+ preprocessed_image = preprocess_image(image)
27
+ # Use pre-trained model to predict facial condition scores
28
+ score = model.predict(preprocessed_image)[0][0]
29
+ # Calculate wrinkle score
30
+ wrinkle_score = 1 - (1 / (1 + np.exp(-5*(score-50)/50)))
31
+ # Calculate laxity score
32
+ laxity_score = np.tanh(score / 10)
33
+ # Calculate facial age
34
+ facial_age = int(np.round(score / 10) * 10)
35
+ # Calculate final score
36
+ final_score = np.mean([wrinkle_score, laxity_score, facial_age])
37
+ return wrinkle_score, laxity_score, facial_age, final_score
38
+
39
+ # Define Gradio interface
40
+ inputs = gr.inputs.Image(label="Input Image")
41
+ outputs = [gr.outputs.Label(label="Wrinkle Score"),
42
+ gr.outputs.Label(label="Laxity Score"),
43
+ gr.outputs.Label(label="Facial Age"),
44
+ gr.outputs.Label(label="Final Score")]
45
+ title = "Facial Condition Evaluator"
46
+ 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."
47
+ examples = [["https://media.licdn.com/dms/image/C5603AQEiW9vZPeekiw/profile-displayphoto-shrink_800_800/0/1516317900912?e=2147483647&v=beta&t=Rs0LpxDfsrHmQFzD0w1vRpCRln0q-sPWrAY9lgiqYeU"],
48
+ ["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"]]
49
+ iface = gr.Interface(calculate_scores, inputs, outputs, title=title, description=description, examples=examples)
50
+
51
+ # Launch Gradio
52
+ iface.launch()