|
|
import cv2 |
|
|
import numpy as np |
|
|
import gradio as gr |
|
|
import os |
|
|
|
|
|
|
|
|
try: |
|
|
import mediapipe as mp |
|
|
except ImportError: |
|
|
import pip |
|
|
|
|
|
pip.main(['install', 'mediapipe']) |
|
|
import mediapipe as mp |
|
|
|
|
|
|
|
|
def process_face_image(input_image): |
|
|
""" |
|
|
Function processes the image, finds facial landmarks, |
|
|
and returns two images: one with landmarks and one with measurements |
|
|
""" |
|
|
|
|
|
if input_image is None: |
|
|
return None, None |
|
|
|
|
|
|
|
|
mp_face_mesh = mp.solutions.face_mesh |
|
|
face_mesh = mp_face_mesh.FaceMesh(static_image_mode=True, max_num_faces=1, min_detection_confidence=0.5) |
|
|
|
|
|
|
|
|
image = input_image.copy() |
|
|
height, width, _ = image.shape |
|
|
rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) |
|
|
|
|
|
|
|
|
image_all_landmarks = image.copy() |
|
|
image_with_lines = image.copy() |
|
|
|
|
|
|
|
|
result = face_mesh.process(rgb_image) |
|
|
|
|
|
|
|
|
if not result.multi_face_landmarks: |
|
|
return image, image, "No face detected" |
|
|
|
|
|
|
|
|
for facial_landmarks in result.multi_face_landmarks: |
|
|
|
|
|
for i in range(0, 468): |
|
|
pt1 = facial_landmarks.landmark[i] |
|
|
x = int(pt1.x * width) |
|
|
y = int(pt1.y * height) |
|
|
cv2.circle(image_all_landmarks, (x, y), 1, (100, 100, 0), -1) |
|
|
|
|
|
|
|
|
if i in [10, 152, 234, 454, 35, 265, 129, 358]: |
|
|
cv2.putText(image_all_landmarks, str(i), (x + 2, y + 2), |
|
|
cv2.FONT_HERSHEY_SIMPLEX, 0.3, (0, 0, 255), 1) |
|
|
|
|
|
|
|
|
right_face = facial_landmarks.landmark[234] |
|
|
left_face = facial_landmarks.landmark[454] |
|
|
right_x = int(right_face.x * width) |
|
|
right_y = int(right_face.y * height) |
|
|
left_x = int(left_face.x * width) |
|
|
left_y = int(left_face.y * height) |
|
|
|
|
|
|
|
|
cv2.line(image_with_lines, (right_x, right_y), (left_x, left_y), (0, 255, 0), 3) |
|
|
face_width = ((left_x - right_x) ** 2 + (left_y - right_y) ** 2) ** 0.5 |
|
|
cv2.putText(image_with_lines, f"Face width: {face_width:.2f}px", |
|
|
(10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2) |
|
|
|
|
|
|
|
|
right_eye = facial_landmarks.landmark[35] |
|
|
left_eye = facial_landmarks.landmark[265] |
|
|
right_eye_x = int(right_eye.x * width) |
|
|
right_eye_y = int(right_eye.y * height) |
|
|
left_eye_x = int(left_eye.x * width) |
|
|
left_eye_y = int(left_eye.y * height) |
|
|
|
|
|
|
|
|
cv2.line(image_with_lines, (right_eye_x, right_eye_y), (left_eye_x, left_eye_y), (255, 0, 0), 3) |
|
|
eye_distance = ((left_eye_x - right_eye_x) ** 2 + (left_eye_y - right_eye_y) ** 2) ** 0.5 |
|
|
cv2.putText(image_with_lines, f"Eye distance: {eye_distance:.2f}px", |
|
|
(10, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 0), 2) |
|
|
|
|
|
|
|
|
right_nose = facial_landmarks.landmark[129] |
|
|
left_nose = facial_landmarks.landmark[358] |
|
|
|
|
|
right_nose_x = int(right_nose.x * width) |
|
|
right_nose_y = int(right_nose.y * height) |
|
|
left_nose_x = int(left_nose.x * width) |
|
|
left_nose_y = int(left_nose.y * height) |
|
|
|
|
|
|
|
|
cv2.line(image_with_lines, (right_nose_x, right_nose_y), (left_nose_x, left_nose_y), (255, 165, 0), 3) |
|
|
nose_width = ((left_nose_x - right_nose_x) ** 2 + (left_nose_y - right_nose_y) ** 2) ** 0.5 |
|
|
cv2.putText(image_with_lines, f"Nose width: {nose_width:.2f}px", |
|
|
(10, 120), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 165, 0), 2) |
|
|
|
|
|
|
|
|
forehead = facial_landmarks.landmark[10] |
|
|
chin = facial_landmarks.landmark[152] |
|
|
forehead_x = int(forehead.x * width) |
|
|
forehead_y = int(forehead.y * height) |
|
|
chin_x = int(chin.x * width) |
|
|
chin_y = int(chin.y * height) |
|
|
|
|
|
|
|
|
cv2.line(image_with_lines, (forehead_x, forehead_y), (chin_x, chin_y), (0, 0, 255), 3) |
|
|
face_height = ((chin_x - forehead_x) ** 2 + (chin_y - forehead_y) ** 2) ** 0.5 |
|
|
cv2.putText(image_with_lines, f"Face height: {face_height:.2f}px", |
|
|
(10, 90), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2) |
|
|
|
|
|
|
|
|
face_ratio = face_width / face_height if face_height > 0 else 0 |
|
|
cv2.putText(image_with_lines, f"Face ratio: {face_ratio:.2f}", |
|
|
(10, 150), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 255), 2) |
|
|
|
|
|
|
|
|
return image_all_landmarks, image_with_lines |
|
|
|
|
|
|
|
|
|
|
|
demo = gr.Interface( |
|
|
fn=process_face_image, |
|
|
inputs=[ |
|
|
gr.Image(type="numpy", label="Input Image") |
|
|
], |
|
|
outputs=[ |
|
|
gr.Image(type="numpy", label="Face Landmarks"), |
|
|
gr.Image(type="numpy", label="Face Measurements") |
|
|
], |
|
|
title="Face Analysis with Measurements", |
|
|
description=""" |
|
|
Upload a face image to get: |
|
|
1. Image with all landmark points |
|
|
2. Image with measurements (face width, eye distance, nose width, face height) |
|
|
""", |
|
|
) |
|
|
|
|
|
|
|
|
if os.path.exists("examples"): |
|
|
example_list = [["examples/" + example] for example in os.listdir("examples") if |
|
|
example.endswith(('.jpg', '.jpeg', '.png'))] |
|
|
if example_list: |
|
|
demo = gr.Interface( |
|
|
fn=process_face_image, |
|
|
inputs=[ |
|
|
gr.Image(type="numpy", label="Input Image") |
|
|
], |
|
|
outputs=[ |
|
|
gr.Image(type="numpy", label="Face Landmarks"), |
|
|
gr.Image(type="numpy", label="Face Measurements") |
|
|
], |
|
|
title="Face Analysis with Measurements", |
|
|
description=""" |
|
|
Upload a face image to get: |
|
|
1. Image with all landmark points |
|
|
2. Image with measurements (face width, eye distance, nose width, face height) |
|
|
""", |
|
|
examples=example_list |
|
|
) |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo.launch(share=True) |
|
|
|