Spaces:
Sleeping
Sleeping
Update app.py (#1)
Browse files- Update app.py (9a0f7e63c0fb52f17a2f278f0443c7bdef132c22)
Co-authored-by: Shreelakshmi <shree2216@users.noreply.huggingface.co>
app.py
CHANGED
|
@@ -1,72 +1,21 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
from sklearn.cluster import KMeans
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
# Skin tone categories based on RGB intensity
|
| 13 |
-
def classify_skin_tone(rgb_color):
|
| 14 |
-
r, g, b = rgb_color
|
| 15 |
-
brightness = (r + g + b) / 3
|
| 16 |
-
if brightness > 200:
|
| 17 |
-
return "Fair"
|
| 18 |
-
elif brightness > 120:
|
| 19 |
-
return "Medium"
|
| 20 |
-
else:
|
| 21 |
-
return "Deep"
|
| 22 |
-
|
| 23 |
-
# Extract cheek region for skin tone detection
|
| 24 |
-
def get_cheek_pixels(image, landmarks):
|
| 25 |
-
h, w, _ = image.shape
|
| 26 |
-
# Cheek landmark indices (approximation)
|
| 27 |
-
cheek_ids = [234, 93, 132, 58] # Left cheek
|
| 28 |
-
pixels = []
|
| 29 |
-
for idx in cheek_ids:
|
| 30 |
-
pt = landmarks[idx]
|
| 31 |
-
x, y = int(pt.x * w), int(pt.y * h)
|
| 32 |
-
# Sample a small patch around each point
|
| 33 |
-
patch = image[max(0, y-2):min(h, y+2), max(0, x-2):min(w, x+2)]
|
| 34 |
-
if patch.size > 0:
|
| 35 |
-
pixels.extend(patch.reshape(-1, 3))
|
| 36 |
-
return np.array(pixels)
|
| 37 |
-
|
| 38 |
-
# Main function
|
| 39 |
-
def analyze_face(image):
|
| 40 |
-
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 41 |
-
results = face_mesh.process(image_rgb)
|
| 42 |
-
|
| 43 |
-
if not results.multi_face_landmarks:
|
| 44 |
-
return image, "No face detected"
|
| 45 |
-
|
| 46 |
-
landmarks = results.multi_face_landmarks[0].landmark
|
| 47 |
-
cheek_pixels = get_cheek_pixels(image, landmarks)
|
| 48 |
-
|
| 49 |
-
# Skin tone analysis using KMeans
|
| 50 |
-
kmeans = KMeans(n_clusters=1, random_state=0).fit(cheek_pixels)
|
| 51 |
-
dominant_color = kmeans.cluster_centers_[0].astype(int)
|
| 52 |
-
skin_tone = classify_skin_tone(dominant_color)
|
| 53 |
-
|
| 54 |
-
# Annotate image with skin tone
|
| 55 |
-
annotated = image.copy()
|
| 56 |
-
cv2.putText(annotated, f"Skin Tone: {skin_tone}", (30, 30),
|
| 57 |
-
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
|
| 58 |
-
|
| 59 |
-
return annotated, skin_tone
|
| 60 |
|
| 61 |
# Gradio interface
|
| 62 |
iface = gr.Interface(
|
| 63 |
-
fn=
|
| 64 |
-
inputs=gr.
|
| 65 |
-
outputs=
|
| 66 |
-
|
| 67 |
-
gr.Textbox(label="Detected Skin Tone")
|
| 68 |
-
],
|
| 69 |
-
title="Face Skin Tone Detector"
|
| 70 |
)
|
| 71 |
|
| 72 |
-
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
import cv2
|
| 4 |
import numpy as np
|
| 5 |
+
from sklearn.ensemble import RandomForestClassifier
|
| 6 |
+
from sklearn.preprocessing import LabelEncoder
|
|
|
|
| 7 |
|
| 8 |
+
# Load dataset function
|
| 9 |
+
def load_dataset(file):
|
| 10 |
+
df = pd.read_excel(file)
|
| 11 |
+
return df.head()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# Gradio interface
|
| 14 |
iface = gr.Interface(
|
| 15 |
+
fn=load_dataset,
|
| 16 |
+
inputs=gr.File(label="Upload Excel Dataset"),
|
| 17 |
+
outputs=gr.Dataframe(label="Preview of Dataset"),
|
| 18 |
+
title="Skin Tone Detector"
|
|
|
|
|
|
|
|
|
|
| 19 |
)
|
| 20 |
|
| 21 |
+
iface.launch()
|