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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -12
app.py CHANGED
@@ -7,24 +7,37 @@ 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
 
@@ -32,9 +45,9 @@ def capture_and_analyze(image):
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()
 
7
 
8
  # Mediapipe face detection setup
9
  mp_face_detection = mp.solutions.face_detection
10
+ mp_face_mesh = mp.solutions.face_mesh
11
  mp_drawing = mp.solutions.drawing_utils
12
+ mp_drawing_styles = mp.solutions.drawing_styles
13
 
14
+ def analyze_face_features(image):
15
  # Convert to BGR for OpenCV compatibility
16
  image_np = np.array(image)
17
  image_np = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)
18
 
19
+ # Face Mesh Detection
20
+ with mp_face_mesh.FaceMesh(static_image_mode=True, max_num_faces=1, refine_landmarks=True, min_detection_confidence=0.5) as face_mesh:
21
+ results = face_mesh.process(cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB))
22
+
23
+ if results.multi_face_landmarks:
24
+ for face_landmarks in results.multi_face_landmarks:
25
+ mp_drawing.draw_landmarks(
26
+ image=image_np,
27
+ landmark_list=face_landmarks,
28
+ connections=mp_face_mesh.FACEMESH_TESSELATION,
29
+ landmark_drawing_spec=None,
30
+ connection_drawing_spec=mp_drawing_styles.get_default_face_mesh_tesselation_style())
31
+ mp_drawing.draw_landmarks(
32
+ image=image_np,
33
+ landmark_list=face_landmarks,
34
+ connections=mp_face_mesh.FACEMESH_CONTOURS,
35
+ landmark_drawing_spec=None,
36
+ connection_drawing_spec=mp_drawing_styles.get_default_face_mesh_contours_style())
37
  else:
38
+ print("No facial landmarks detected")
39
 
40
+ # Save processed image temporarily
41
  temp_file = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
42
  cv2.imwrite(temp_file.name, image_np)
43
 
 
45
 
46
  # Gradio Interface
47
  iface = gr.Interface(
48
+ fn=analyze_face_features,
49
  inputs=gr.Image(label="Upload or Capture a Face Image"),
50
+ outputs=gr.Image(label="Face Mesh + Features")
51
  )
52
 
53
  iface.launch()