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

added interfaces

Browse files
app.py CHANGED
@@ -1,7 +1,138 @@
 
 
 
1
  import gradio as gr
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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=[
119
+ gr.Image(type="numpy", label="Input Image")
120
+ ],
121
+ outputs=[
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 �������� �������� ������� ���������
examples/246416_1_mrvwallpaper_246416_1_org_eder.jpg ADDED
examples/Dzholi.jpg ADDED
examples/Kathrin-Eder.jpg ADDED
examples/OIP (1).jfif ADDED
Binary file (19.8 kB). View file
 
examples/OIP (3).jfif ADDED
Binary file (28.8 kB). View file
 
examples/OIP (6).jfif ADDED
Binary file (23.6 kB). View file
 
examples/R.jfif ADDED
Binary file (127 kB). View file
 
examples/_R___eder_katrin____par3f47be87fe7ac72343cb859f813231c2_dat1592459707.jpeg ADDED
examples/csm_EderT_BER_3333_070319_640_970b31d49d.jpg ADDED
examples/katharina-eder.1024x1024.jpg ADDED
examples/katrin-eder-c-landeshauptstadt-mainz_182-3276025_20210510064436.jpg ADDED
examples/thomas-eder.1024x1024 (1).jpg ADDED
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ 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