-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
205 lines (165 loc) · 9.05 KB
/
main.py
File metadata and controls
205 lines (165 loc) · 9.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import face_recognition
import cv2
import streamlit as st
import numpy as np
from PIL import Image
st.title("AI FACE RECOGNIZER")
st.write("**Обученная нейросеть на основе библиотеки dlib, способная распознавать лица людей в медицинской маске и без**")
st.sidebar.write("")
st.sidebar.write("")
st.sidebar.write("")
st.sidebar.write("Выберите метод распознавания лица:")
instruction = "Инструкция"
photoDetection = "С фотографии"
liveCamDetection = "В режиме реального времени"
activities = [instruction, photoDetection, liveCamDetection]
choice = st.sidebar.selectbox("", activities)
if choice == instruction:
st.write("На этой странице вы можете увидеть инструкцию по использованию нашего веб-приложения:")
st.write("В левой части окна вы можете выбрать один из двух методов распознавания лица: с фотографии и в режиме реального времени (в этом случае вам придется включить фронтальную камеру).")
st.write("В случае выбора распознавания по фотографии вам откроется панель, куда нужно загрузить файл в формате jpg, jpeg, png или webp. После чего просто дождаться, пока алгоритм закончит работу и выведет результат.")
st.write("В случае выбора распознавания в режиме реального времени появится окно, в которое будет выведено изображение с веб-камеры. Алгоритм определяет лица в зоне видимости камеры мгновенно.")
st.write("Поскольку данный алгоритм используется для контроля доступа (к устройству, системе, для входа куда-либо), то при удачной попытке распознавания лица вам будет разрешен доступ к абсрактной системе. В противном случае в доступе будет отказано.")
st.write("")
st.write("")
st.write("")
st.caption("**Сделано командой AI CREATORS**")
obabkov_M1 = face_recognition.load_image_file(
"DataSet Masks/9. Ilia Nikolaevich Obabkov 1.jpg")
obabkov_M1_encoding = face_recognition.face_encodings(obabkov_M1)[0]
obabkov_F1 = face_recognition.load_image_file(
"DataSet Faces/10. Ilia Nikolaevich Obabkov 2.png")
obabkov_F1_encoding = face_recognition.face_encodings(obabkov_F1)[0]
shadrinD_M1 = face_recognition.load_image_file(
"DataSet Masks/12. Denis Borisovich Shadrin 1.jpg")
shadrinD_M1_encoding = face_recognition.face_encodings(shadrinD_M1)[0]
malevaniyA_F1 = face_recognition.load_image_file(
"DataSet Faces/16. Malevaniy Artem 2.jpeg")
malevaniyA_F1_encoding = face_recognition.face_encodings(malevaniyA_F1)[0]
malevaniyA_M1 = face_recognition.load_image_file(
"DataSet Masks/16. Malevaniy Artem 2.jpg")
malevaniyA_M1_encoding = face_recognition.face_encodings(malevaniyA_M1)[0]
akulovD_F1 = face_recognition.load_image_file(
"DataSet Faces/14. Akulov Danila 2.jpg")
akulovD_F1_encoding = face_recognition.face_encodings(akulovD_F1)[0]
akulovD_M1 = face_recognition.load_image_file(
"DataSet Masks/14. Akulov Danila 2.jpg")
akulovD_M1_encoding = face_recognition.face_encodings(akulovD_M1)[0]
akulovD_M2 = face_recognition.load_image_file(
"DataSet Masks/13. Akulov Danila 1.jpg")
akulovD_M2_encoding = face_recognition.face_encodings(akulovD_M2)[0]
samarinM_F1 = face_recognition.load_image_file(
"DataSet Faces/20. Samarin M. P. 1.png")
samarinM_F1_encoding = face_recognition.face_encodings(samarinM_F1)[0]
samarinM_M1 = face_recognition.load_image_file(
"DataSet Masks/21. Samarin M. P. 2.jpg")
samarinM_M1_encoding = face_recognition.face_encodings(samarinM_M1)[0]
known_face_encodings = \
[
obabkov_M1_encoding,
obabkov_F1_encoding,
shadrinD_M1_encoding,
malevaniyA_F1_encoding,
malevaniyA_M1_encoding,
akulovD_F1_encoding,
akulovD_M1_encoding,
akulovD_M2_encoding,
samarinM_F1_encoding,
samarinM_M1_encoding
]
known_face_names = \
[
"Obabkov I. N.",
"Obabkov I. N.",
"Shadrin D. B.",
"Malevaniy A. K.",
"Malevaniy A. K.",
"Akulov D. A.",
"Akulov D. A.",
"Akulov D. A.",
"Samarin M. P.",
"Samarin M. P.",
]
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True
FRAME_WINDOW = st.image([])
if choice == liveCamDetection:
video_capture = cv2.VideoCapture(0)
successCheck = True
errorCheck = True
while True:
ret, frame = video_capture.read()
if not ret:
continue
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
rgb_small_frame = small_frame[:, :, ::-1]
if process_this_frame:
face_locations = face_recognition.face_locations(rgb_small_frame)
face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
face_names = []
for face_encoding in face_encodings:
matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
name = "Unknown"
face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
best_match_index = np.argmin(face_distances)
if matches[best_match_index]:
name = known_face_names[best_match_index]
face_names.append(name)
if name != "Unknown" and successCheck:
st.success("Доступ к системе разрешен")
successCheck = False
elif name == "Unknown" and errorCheck:
st.error("В доступе отказано")
errorCheck = False
process_this_frame = not process_this_frame
for (top, right, bottom, left), name in zip(face_locations, face_names):
top *= 4
right *= 4
bottom *= 4
left *= 4
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
FRAME_WINDOW.image(frame)
video_capture.release()
if choice == photoDetection:
image_file = st.file_uploader(
"Загрузите фотографию", type=['jpeg', 'png', 'jpg', 'webp'])
if image_file:
image = Image.open(image_file)
successCheck = True
errorCheck = True
while True:
frame = np.array(image.convert('RGB'))
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
rgb_small_frame = small_frame[:, :, ::-1]
face_locations = face_recognition.face_locations(rgb_small_frame)
face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
face_names = []
for face_encoding in face_encodings:
matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
name = "Unknown"
face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
best_match_index = np.argmin(face_distances)
if matches[best_match_index]:
name = known_face_names[best_match_index]
face_names.append(name)
if name != "Unknown" and successCheck:
st.success("Доступ к системе разрешен")
successCheck = False
elif name == "Unknown" and errorCheck:
st.error("В доступе отказано")
errorCheck = False
for (top, right, bottom, left), name in zip(face_locations, face_names):
top *= 4
right *= 4
bottom *= 4
left *= 4
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
FRAME_WINDOW.image(frame)