davelop commited on
Commit
43536a2
·
verified ·
1 Parent(s): 8d3df34

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +124 -0
app.py ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+ from sklearn.cluster import KMeans
4
+ import numpy as np
5
+ import cv2
6
+ from webcolors import hex_to_rgb, rgb_to_hex
7
+ from scipy.spatial import KDTree
8
+ from collections import Counter
9
+
10
+ model = tf.keras.models.load_model("model.h5")
11
+
12
+ classes = [
13
+ "background", "skin", "left eyebrow", "right eyebrow",
14
+ "left eye", "right eye", "nose", "upper lip", "inner mouth",
15
+ "lower lip", "hair"
16
+ ]
17
+
18
+ def face_skin_extract(pred, image_x):
19
+ output = np.zeros_like(image_x, dtype=np.uint8)
20
+ mask = (pred == 1)
21
+ output[mask] = image_x[mask]
22
+ return output
23
+
24
+ def extract_dom_color_kmeans(img):
25
+
26
+
27
+ mask = ~np.all(img == [0, 0, 0], axis=-1) # Mask to exclude black pixels
28
+
29
+
30
+ non_black_pixels = img[mask] # Extract non-black pixels
31
+
32
+
33
+ k_cluster = KMeans(n_clusters=3, n_init="auto") # Apply KMeans clustering on non-black pixels
34
+ k_cluster.fit(non_black_pixels)
35
+
36
+
37
+
38
+
39
+ width = 300
40
+ palette = np.zeros((50, width, 3), dtype=np.uint8)
41
+
42
+ n_pixels = len(k_cluster.labels_)
43
+ counter = Counter(k_cluster.labels_)
44
+
45
+ perc = {i: np.round(counter[i] / n_pixels, 2) for i in counter}
46
+ perc = dict(sorted(perc.items()))
47
+
48
+ cluster_centers = k_cluster.cluster_centers_
49
+
50
+ # print("Cluster Percentages:", perc)
51
+ # print("Cluster Centers (RGB):", cluster_centers)
52
+
53
+ val = list(perc.values())
54
+ val.sort()
55
+ res = val[-1]
56
+ print(res)
57
+ sec_high_val = list(perc.keys())[list(perc.values()).index(res)]
58
+ rgb_list = cluster_centers[sec_high_val]
59
+
60
+ step = 0
61
+ for idx, center in enumerate(k_cluster.cluster_centers_):
62
+ width_step = int(perc[idx] * width + 1)
63
+ palette[:, step:step + width_step, :] = center
64
+ step += width_step
65
+
66
+ return rgb_list
67
+
68
+ def closest_tone_match(rgb_tuple):
69
+ skin_tones = {'Monk 10': '#292420', 'Monk 9': '#3a312a', 'Monk 8':'#604134', 'Monk 7':'#825c43', 'Monk 6':'#a07e56', 'Monk 5':'#d7bd96', 'Monk 4':'#eadaba', 'Monk 3':'#f7ead0', 'Monk 2':'#f3e7db', 'Monk 1':'#f6ede4'}
70
+
71
+ rgb_values = []
72
+ names = []
73
+ for monk in skin_tones:
74
+ names.append(monk)
75
+ rgb_values.append(hex_to_rgb(skin_tones[monk]))
76
+
77
+ kdt_db = KDTree(rgb_values)
78
+ distance, index = kdt_db.query(rgb_tuple)
79
+ monk_hex = skin_tones[names[index]]
80
+ derived_hex = rgb_to_hex((int(rgb_tuple[0]), int(rgb_tuple[1]), int(rgb_tuple[2])))
81
+ return names[index],monk_hex,derived_hex
82
+
83
+ def process_image(image_path):
84
+
85
+ image = cv2.imread(image_path)
86
+ image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
87
+
88
+ image_x = cv2.resize(image, (512, 512))
89
+ image_norm = image_x / 255.0
90
+ image_norm = np.expand_dims(image_norm, axis=0).astype(np.float32)
91
+
92
+ pred = model.predict(image_norm)[0]
93
+ pred = np.argmax(pred, axis=-1).astype(np.int32)
94
+
95
+ face_skin = face_skin_extract(pred, image_x)
96
+ face_skin_vis = cv2.imread(face_skin)
97
+ dominant_color_rgb = extract_dom_color_kmeans(face_skin)
98
+
99
+ monk_tone, monk_hex, derived_hex = closest_tone_match(
100
+ (dominant_color_rgb[0], dominant_color_rgb[1], dominant_color_rgb[2])
101
+ )
102
+
103
+ return [monk_tone,derived_hex,monk_hex,face_skin_vis]
104
+
105
+
106
+ inputs = gr.Image(type="filepath", label="Upload Face Image")
107
+ outputs = [
108
+ gr.Label(label="Monk Skin Tone"),
109
+ gr.ColorPicker(label="Derived Color"),
110
+ gr.ColorPicker(label="Closest Monk Color"),
111
+ # gr.JSON(label="Dominant RGB Values"),
112
+ gr.Image(label="Skin Mask Visualization")
113
+ ]
114
+
115
+ interface = gr.Interface(
116
+ fn=process_image,
117
+ inputs=inputs,
118
+ outputs=outputs,
119
+ title="Skin Tone Analysis",
120
+ description="Upload a face image to analyse skin tone and find closest Monk skin tone match."
121
+ )
122
+
123
+ if __name__ == "__main__":
124
+ interface.launch()