RICHERGIRL commited on
Commit
c1e3864
·
verified ·
1 Parent(s): 1ef2901

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -45
app.py CHANGED
@@ -2,60 +2,39 @@ import gradio as gr
2
  import cv2
3
  import numpy as np
4
  import tempfile
5
- from PIL import Image
6
  import mediapipe as mp
7
- from sklearn.cluster import KMeans
8
 
9
- # MediaPipe setup
10
- mp_face_mesh = mp.solutions.face_mesh
11
- face_mesh = mp_face_mesh.FaceMesh(static_image_mode=True)
12
  mp_drawing = mp.solutions.drawing_utils
13
 
14
- # Step 1 & 2 combined: Capture, analyze, and return image with annotations
15
- def analyze_face(image):
16
- if image is None:
17
- return "No image provided"
18
-
19
- # Save image temporarily
20
- temp_file = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
21
- image_pil = Image.fromarray(image)
22
- image_pil.save(temp_file.name)
23
-
24
- # Convert for MediaPipe
25
- img_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
26
- result = face_mesh.process(img_rgb)
27
 
28
- if not result.multi_face_landmarks:
29
- return "No face detected"
 
30
 
31
- # Draw landmarks
32
- for face_landmarks in result.multi_face_landmarks:
33
- mp_drawing.draw_landmarks(
34
- image=image,
35
- landmark_list=face_landmarks,
36
- connections=mp_face_mesh.FACEMESH_TESSELATION,
37
- landmark_drawing_spec=None,
38
- connection_drawing_spec=mp_drawing.DrawingSpec(color=(0,255,0), thickness=1, circle_radius=1),
39
- )
40
 
41
- # Skin tone detection with KMeans
42
- pixels = img_rgb.reshape((-1, 3))
43
- kmeans = KMeans(n_clusters=1, random_state=42).fit(pixels)
44
- dominant_color = kmeans.cluster_centers_[0].astype(int)
45
 
46
- return image, f"Dominant skin tone RGB: {tuple(dominant_color)}"
47
 
48
  # Gradio Interface
49
- demo = gr.Interface(
50
- fn=analyze_face,
51
- inputs=gr.Image(type="numpy", image_mode="BGR", label="Capture or Upload Your Face"),
52
- outputs=[
53
- gr.Image(type="numpy", label="Face Analysis Output"),
54
- gr.Textbox(label="Detected Skin Tone (RGB)")
55
- ],
56
- title="Face Scanner for Mask Recommendation",
57
- description="Upload or capture a photo to analyze face landmarks and detect skin tone."
58
  )
59
 
60
- if __name__ == "__main__":
61
- demo.launch()
 
2
  import cv2
3
  import numpy as np
4
  import tempfile
 
5
  import mediapipe as mp
6
+ from PIL import Image
7
 
8
+ # Mediapipe face detection setup
9
+ mp_face_detection = mp.solutions.face_detection
 
10
  mp_drawing = mp.solutions.drawing_utils
11
 
12
+ def capture_and_analyze(image):
13
+ # Convert to BGR for OpenCV compatibility
14
+ image_np = np.array(image)
15
+ image_np = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)
 
 
 
 
 
 
 
 
 
16
 
17
+ # Run face detection
18
+ with mp_face_detection.FaceDetection(model_selection=1, min_detection_confidence=0.5) as face_detection:
19
+ results = face_detection.process(cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB))
20
 
21
+ if results.detections:
22
+ for detection in results.detections:
23
+ mp_drawing.draw_detection(image_np, detection)
24
+ else:
25
+ print("No face detected")
 
 
 
 
26
 
27
+ # Save image temporarily
28
+ temp_file = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
29
+ cv2.imwrite(temp_file.name, image_np)
 
30
 
31
+ return Image.fromarray(cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB))
32
 
33
  # Gradio Interface
34
+ iface = gr.Interface(
35
+ fn=capture_and_analyze,
36
+ inputs=gr.Image(label="Upload or Capture a Face Image"),
37
+ outputs=gr.Image(label="Processed Image with Face Detection")
 
 
 
 
 
38
  )
39
 
40
+ iface.launch()