Spaces:
Configuration error
Configuration error
Update app.py
Browse files
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
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
face_mesh = mp_face_mesh.FaceMesh(static_image_mode=True)
|
| 12 |
mp_drawing = mp.solutions.drawing_utils
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 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 |
-
|
| 29 |
-
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 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 |
-
#
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
dominant_color = kmeans.cluster_centers_[0].astype(int)
|
| 45 |
|
| 46 |
-
return
|
| 47 |
|
| 48 |
# Gradio Interface
|
| 49 |
-
|
| 50 |
-
fn=
|
| 51 |
-
inputs=gr.Image(
|
| 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 |
-
|
| 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()
|
|
|