RICHERGIRL commited on
Commit
1d93970
·
verified ·
1 Parent(s): 3ede931

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -16
app.py CHANGED
@@ -1,31 +1,76 @@
1
  import gradio as gr
2
  import cv2
3
  import numpy as np
 
4
  import uuid
5
  import os
 
6
 
7
- def capture_image(image):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  if image is None:
9
- return None, "No image captured"
10
 
11
- # Create temp directory if not exists
12
- os.makedirs("temp_images", exist_ok=True)
13
 
14
- # Generate unique filename
15
- filename = f"temp_images/{uuid.uuid4().hex}.png"
16
- cv2.imwrite(filename, cv2.cvtColor(image, cv2.COLOR_RGB2BGR))
17
-
18
- return image, f"Image saved as {filename}"
 
 
 
 
 
19
 
20
- # Gradio Interface
21
  demo = gr.Interface(
22
- fn=capture_image,
23
- inputs=gr.Image(label="Capture from Webcam", type="numpy", image_mode="RGB"),
24
- outputs=[gr.Image(label="Captured Image"), gr.Textbox(label="Status")],
25
- live=True,
 
 
 
 
26
  allow_flagging="never",
27
- title="Step 1: Capture Webcam Image"
28
  )
29
 
30
  if __name__ == "__main__":
31
- demo.launch()
 
1
  import gradio as gr
2
  import cv2
3
  import numpy as np
4
+ import mediapipe as mp
5
  import uuid
6
  import os
7
+ from sklearn.cluster import KMeans
8
 
9
+ # Create reusable face mesh
10
+ mp_face_mesh = mp.solutions.face_mesh
11
+ face_mesh = mp_face_mesh.FaceMesh(static_image_mode=True)
12
+
13
+ # Face Shape Detection Logic (Simplified based on face landmarks)
14
+ def detect_face_shape(landmarks, image_shape):
15
+ # Grab required landmark points
16
+ left_cheek = landmarks[234]
17
+ right_cheek = landmarks[454]
18
+ chin = landmarks[152]
19
+ forehead = landmarks[10]
20
+
21
+ # Calculate distances
22
+ width = np.linalg.norm(np.array([left_cheek.x, left_cheek.y]) - np.array([right_cheek.x, right_cheek.y]))
23
+ height = np.linalg.norm(np.array([chin.x, chin.y]) - np.array([forehead.x, forehead.y]))
24
+
25
+ ratio = width / height
26
+ if ratio > 1.3:
27
+ return "Round"
28
+ elif ratio > 1.1:
29
+ return "Oval"
30
+ else:
31
+ return "Long"
32
+
33
+ # Skin tone using KMeans
34
+ def get_skin_tone(image):
35
+ h, w, _ = image.shape
36
+ face_crop = image[h//4:3*h//4, w//3:2*w//3] # middle region
37
+
38
+ pixels = face_crop.reshape(-1, 3)
39
+ kmeans = KMeans(n_clusters=3, random_state=0).fit(pixels)
40
+ dominant = kmeans.cluster_centers_.astype(int)[0]
41
+ return tuple(dominant)
42
+
43
+ def analyze_face(image):
44
  if image is None:
45
+ return None, "No image", "No skin tone"
46
 
47
+ image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
48
+ results = face_mesh.process(image_rgb)
49
 
50
+ if not results.multi_face_landmarks:
51
+ return image, "No face detected", "N/A"
52
+
53
+ landmarks = results.multi_face_landmarks[0].landmark
54
+ shape = detect_face_shape(landmarks, image.shape)
55
+
56
+ tone = get_skin_tone(image)
57
+ tone_str = f"RGB: {tone}"
58
+
59
+ return image, shape, tone_str
60
 
61
+ # Gradio interface
62
  demo = gr.Interface(
63
+ fn=analyze_face,
64
+ inputs=gr.Image(type="numpy", image_mode="BGR", label="Upload or Capture Face"),
65
+ outputs=[
66
+ gr.Image(label="Original Image"),
67
+ gr.Textbox(label="Detected Face Shape"),
68
+ gr.Textbox(label="Dominant Skin Tone")
69
+ ],
70
+ live=False,
71
  allow_flagging="never",
72
+ title="Step 2: Face Shape and Skin Tone Analyzer"
73
  )
74
 
75
  if __name__ == "__main__":
76
+ demo.launch()