valenynl commited on
Commit
c5ac1e3
·
1 Parent(s): 938c065

Added logic

Browse files
Files changed (2) hide show
  1. app.py +87 -58
  2. requirements.txt +1 -1
app.py CHANGED
@@ -1,118 +1,127 @@
1
  import cv2
2
- import mediapipe as mp
3
  import numpy as np
4
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
5
 
6
- example_list = [["examples/" + example] for example in os.listdir("examples")]
7
 
8
  def process_face_image(input_image):
9
  """
10
- ������� �������� ����������, ��������� ��������� �������,
11
- ������� ��� ����������: ����������� �� ������������
12
  """
13
- # ���������� ���������� gradio ������ numpy
14
  if input_image is None:
15
  return None, None
16
-
17
  # Face mesh
18
  mp_face_mesh = mp.solutions.face_mesh
19
  face_mesh = mp_face_mesh.FaceMesh(static_image_mode=True, max_num_faces=1, min_detection_confidence=0.5)
20
-
21
- # �������� ������ ����������
22
  image = input_image.copy()
23
  height, width, _ = image.shape
24
  rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
25
-
26
- # ��������� ��ﳿ ���������� ��� ��������� � ����
27
  image_all_landmarks = image.copy()
28
  image_with_lines = image.copy()
29
-
30
- # ��������� ���������
31
  result = face_mesh.process(rgb_image)
32
-
33
- # ����������, �� �������� �������
34
  if not result.multi_face_landmarks:
35
- return image, image, "������� �� ��������"
36
-
37
- # ���������� �������� ���������
38
  for facial_landmarks in result.multi_face_landmarks:
39
- # ������� �� ��������� ���������� �������
40
  for i in range(0, 468):
41
  pt1 = facial_landmarks.landmark[i]
42
  x = int(pt1.x * width)
43
  y = int(pt1.y * height)
44
  cv2.circle(image_all_landmarks, (x, y), 1, (100, 100, 0), -1)
45
-
46
- # ������ ������ ��������� ��� �������� �����
47
  if i in [10, 152, 234, 454, 35, 265, 129, 358]:
48
- cv2.putText(image_all_landmarks, str(i), (x+2, y+2),
49
- cv2.FONT_HERSHEY_SIMPLEX, 0.3, (0, 0, 255), 1)
50
-
51
- # ������ ������� (����� 234 454)
52
  right_face = facial_landmarks.landmark[234]
53
  left_face = facial_landmarks.landmark[454]
54
  right_x = int(right_face.x * width)
55
  right_y = int(right_face.y * height)
56
  left_x = int(left_face.x * width)
57
  left_y = int(left_face.y * height)
58
-
59
- # ������� ���� ������ �������
60
  cv2.line(image_with_lines, (right_x, right_y), (left_x, left_y), (0, 255, 0), 3)
61
  face_width = ((left_x - right_x) ** 2 + (left_y - right_y) ** 2) ** 0.5
62
- cv2.putText(image_with_lines, f"Face width: {face_width:.2f}px",
63
  (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2)
64
-
65
- # ³������ �� ����� (����� 35 265)
66
  right_eye = facial_landmarks.landmark[35]
67
  left_eye = facial_landmarks.landmark[265]
68
  right_eye_x = int(right_eye.x * width)
69
  right_eye_y = int(right_eye.y * height)
70
  left_eye_x = int(left_eye.x * width)
71
  left_eye_y = int(left_eye.y * height)
72
-
73
- # ������� ���� ������� �� �����
74
  cv2.line(image_with_lines, (right_eye_x, right_eye_y), (left_eye_x, left_eye_y), (255, 0, 0), 3)
75
  eye_distance = ((left_eye_x - right_eye_x) ** 2 + (left_eye_y - right_eye_y) ** 2) ** 0.5
76
- cv2.putText(image_with_lines, f"Eye distance: {eye_distance:.2f}px",
77
  (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 0), 2)
78
-
79
- # ������ ���� (����� 129 358)
80
  right_nose = facial_landmarks.landmark[129]
81
  left_nose = facial_landmarks.landmark[358]
82
  right_nose_x = int(right_nose.x * width)
83
  right_nose_y = int(right_nose.y * height)
84
  left_nose_x = int(left_nose.x * width)
85
  left_nose_y = int(left_nose.y * height)
86
-
87
- # ������� ���� ������ ����
88
  cv2.line(image_with_lines, (right_nose_x, right_nose_y), (left_nose_x, left_nose_y), (255, 165, 0), 3)
89
  nose_width = ((left_nose_x - right_nose_x) ** 2 + (left_nose_y - right_nose_y) ** 2) ** 0.5
90
- cv2.putText(image_with_lines, f"Nose width: {nose_width:.2f}px",
91
  (10, 120), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 165, 0), 2)
92
-
93
- # ������ ������� (����� 10 152)
94
- forehead = facial_landmarks.landmark[10] # ����� �� ���
95
- chin = facial_landmarks.landmark[152] # ����� �� �������
96
  forehead_x = int(forehead.x * width)
97
  forehead_y = int(forehead.y * height)
98
  chin_x = int(chin.x * width)
99
  chin_y = int(chin.y * height)
100
-
101
- # ������� ���� ������ �������
102
  cv2.line(image_with_lines, (forehead_x, forehead_y), (chin_x, chin_y), (0, 0, 255), 3)
103
  face_height = ((chin_x - forehead_x) ** 2 + (chin_y - forehead_y) ** 2) ** 0.5
104
- cv2.putText(image_with_lines, f"Face height: {face_height:.2f}px",
105
  (10, 90), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
106
-
107
- # ��������� ��������� �������
108
  face_ratio = face_width / face_height if face_height > 0 else 0
109
- cv2.putText(image_with_lines, f"Face ratio: {face_ratio:.2f}",
110
  (10, 150), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 255), 2)
111
-
112
- # ��������� ������ ����������
113
  return image_all_landmarks, image_with_lines
114
 
115
- # ��������� ��������� Gradio
 
116
  demo = gr.Interface(
117
  fn=process_face_image,
118
  inputs=[
@@ -122,17 +131,37 @@ demo = gr.Interface(
122
  gr.Image(type="numpy", label="Face Landmarks"),
123
  gr.Image(type="numpy", label="Face Measurements")
124
  ],
125
- title="Sytoss: Face Analysis with Measurements",
126
  description="""
127
  Upload a face image to get:
128
- 1. An image with all landmark points
129
- 2. An image with measurements (face width, eye distance, nose width, face height)
130
  """,
131
- examples=[
132
- examples=example_list
133
- ]
134
  )
135
 
136
- # ��������� ���������
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
137
  if __name__ == "__main__":
138
- demo.launch(share=True) # share=True �������� �������� ������� ���������
 
1
  import cv2
 
2
  import numpy as np
3
  import gradio as gr
4
+ import os
5
+
6
+ # Install mediapipe if not available
7
+ try:
8
+ import mediapipe as mp
9
+ except ImportError:
10
+ import pip
11
+
12
+ pip.main(['install', 'mediapipe'])
13
+ import mediapipe as mp
14
 
 
15
 
16
  def process_face_image(input_image):
17
  """
18
+ Function processes the image, finds facial landmarks,
19
+ and returns two images: one with landmarks and one with measurements
20
  """
21
+ # Convert image from gradio to numpy format
22
  if input_image is None:
23
  return None, None
24
+
25
  # Face mesh
26
  mp_face_mesh = mp.solutions.face_mesh
27
  face_mesh = mp_face_mesh.FaceMesh(static_image_mode=True, max_num_faces=1, min_detection_confidence=0.5)
28
+
29
+ # Get image dimensions
30
  image = input_image.copy()
31
  height, width, _ = image.shape
32
  rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
33
+
34
+ # Create copies for landmarks and measurements
35
  image_all_landmarks = image.copy()
36
  image_with_lines = image.copy()
37
+
38
+ # Find landmarks
39
  result = face_mesh.process(rgb_image)
40
+
41
+ # Check if face was detected
42
  if not result.multi_face_landmarks:
43
+ return image, image, "No face detected"
44
+
45
+ # Process the found landmarks
46
  for facial_landmarks in result.multi_face_landmarks:
47
+ # Draw all landmarks as thin points
48
  for i in range(0, 468):
49
  pt1 = facial_landmarks.landmark[i]
50
  x = int(pt1.x * width)
51
  y = int(pt1.y * height)
52
  cv2.circle(image_all_landmarks, (x, y), 1, (100, 100, 0), -1)
53
+
54
+ # Add landmark numbers for important points
55
  if i in [10, 152, 234, 454, 35, 265, 129, 358]:
56
+ cv2.putText(image_all_landmarks, str(i), (x + 2, y + 2),
57
+ cv2.FONT_HERSHEY_SIMPLEX, 0.3, (0, 0, 255), 1)
58
+
59
+ # Face width (points 234 and 454)
60
  right_face = facial_landmarks.landmark[234]
61
  left_face = facial_landmarks.landmark[454]
62
  right_x = int(right_face.x * width)
63
  right_y = int(right_face.y * height)
64
  left_x = int(left_face.x * width)
65
  left_y = int(left_face.y * height)
66
+
67
+ # Draw face width line
68
  cv2.line(image_with_lines, (right_x, right_y), (left_x, left_y), (0, 255, 0), 3)
69
  face_width = ((left_x - right_x) ** 2 + (left_y - right_y) ** 2) ** 0.5
70
+ cv2.putText(image_with_lines, f"Face width: {face_width:.2f}px",
71
  (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2)
72
+
73
+ # Eye distance (points 35 and 265)
74
  right_eye = facial_landmarks.landmark[35]
75
  left_eye = facial_landmarks.landmark[265]
76
  right_eye_x = int(right_eye.x * width)
77
  right_eye_y = int(right_eye.y * height)
78
  left_eye_x = int(left_eye.x * width)
79
  left_eye_y = int(left_eye.y * height)
80
+
81
+ # Draw eye distance line
82
  cv2.line(image_with_lines, (right_eye_x, right_eye_y), (left_eye_x, left_eye_y), (255, 0, 0), 3)
83
  eye_distance = ((left_eye_x - right_eye_x) ** 2 + (left_eye_y - right_eye_y) ** 2) ** 0.5
84
+ cv2.putText(image_with_lines, f"Eye distance: {eye_distance:.2f}px",
85
  (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 0), 2)
86
+
87
+ # Nose width (points 129 and 358)
88
  right_nose = facial_landmarks.landmark[129]
89
  left_nose = facial_landmarks.landmark[358]
90
  right_nose_x = int(right_nose.x * width)
91
  right_nose_y = int(right_nose.y * height)
92
  left_nose_x = int(left_nose.x * width)
93
  left_nose_y = int(left_nose.y * height)
94
+
95
+ # Draw nose width line
96
  cv2.line(image_with_lines, (right_nose_x, right_nose_y), (left_nose_x, left_nose_y), (255, 165, 0), 3)
97
  nose_width = ((left_nose_x - right_nose_x) ** 2 + (left_nose_y - right_nose_y) ** 2) ** 0.5
98
+ cv2.putText(image_with_lines, f"Nose width: {nose_width:.2f}px",
99
  (10, 120), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 165, 0), 2)
100
+
101
+ # Face height (points 10 and 152)
102
+ forehead = facial_landmarks.landmark[10] # Forehead point
103
+ chin = facial_landmarks.landmark[152] # Chin point
104
  forehead_x = int(forehead.x * width)
105
  forehead_y = int(forehead.y * height)
106
  chin_x = int(chin.x * width)
107
  chin_y = int(chin.y * height)
108
+
109
+ # Draw face height line
110
  cv2.line(image_with_lines, (forehead_x, forehead_y), (chin_x, chin_y), (0, 0, 255), 3)
111
  face_height = ((chin_x - forehead_x) ** 2 + (chin_y - forehead_y) ** 2) ** 0.5
112
+ cv2.putText(image_with_lines, f"Face height: {face_height:.2f}px",
113
  (10, 90), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
114
+
115
+ # Return face ratio
116
  face_ratio = face_width / face_height if face_height > 0 else 0
117
+ cv2.putText(image_with_lines, f"Face ratio: {face_ratio:.2f}",
118
  (10, 150), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 255), 2)
119
+
120
+ # Return both images
121
  return image_all_landmarks, image_with_lines
122
 
123
+
124
+ # Create Gradio interface
125
  demo = gr.Interface(
126
  fn=process_face_image,
127
  inputs=[
 
131
  gr.Image(type="numpy", label="Face Landmarks"),
132
  gr.Image(type="numpy", label="Face Measurements")
133
  ],
134
+ title="Face Analysis with Measurements",
135
  description="""
136
  Upload a face image to get:
137
+ 1. Image with all landmark points
138
+ 2. Image with measurements (face width, eye distance, nose width, face height)
139
  """,
 
 
 
140
  )
141
 
142
+ # Add examples from the 'examples' directory if it exists
143
+ if os.path.exists("examples"):
144
+ example_list = [["examples/" + example] for example in os.listdir("examples") if
145
+ example.endswith(('.jpg', '.jpeg', '.png'))]
146
+ if example_list:
147
+ demo = gr.Interface(
148
+ fn=process_face_image,
149
+ inputs=[
150
+ gr.Image(type="numpy", label="Input Image")
151
+ ],
152
+ outputs=[
153
+ gr.Image(type="numpy", label="Face Landmarks"),
154
+ gr.Image(type="numpy", label="Face Measurements")
155
+ ],
156
+ title="Face Analysis with Measurements",
157
+ description="""
158
+ Upload a face image to get:
159
+ 1. Image with all landmark points
160
+ 2. Image with measurements (face width, eye distance, nose width, face height)
161
+ """,
162
+ examples=example_list
163
+ )
164
+
165
+ # Launch the interface
166
  if __name__ == "__main__":
167
+ demo.launch(share=True) # share=True allows you to get a public link
requirements.txt CHANGED
@@ -2,4 +2,4 @@ gradio==3.49.0
2
  numpy==1.26.0
3
  opencv-python==4.9.0.80
4
  ultralytics==8.1.10
5
- mediapipe==0.10.7
 
2
  numpy==1.26.0
3
  opencv-python==4.9.0.80
4
  ultralytics==8.1.10
5
+ mediapipe==0.10.13