-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfacedetection.py
More file actions
241 lines (195 loc) · 6.2 KB
/
facedetection.py
File metadata and controls
241 lines (195 loc) · 6.2 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import cv2
import os
import io
import streamlit as st
import mediapipe as mp
import cv2 as cv
import numpy as np
import tempfile
import time
from PIL import Image
st.set_page_config(layout="wide")
st.session_state.setdefault("rows", {})
import analysis
DEMO_IMAGE = 'demo/demo.jpg'
DEMO_VIDEO = 'demo/demo.mp4'
left_placeholder, empty_placeholder, right_placeholder = st.columns([10, 1, 4])
video_frame_placeholder = st.empty()
video_text_placeholder = st.empty()
with left_placeholder:
st.title('Face Detection with Mediapipe')
## Add Sidebar and Main Window style
st.markdown(
"""
<style>
[data-testid="stSidebar"][aria-expanded="true"] > div:first-child{
width: 350px
}
[data-testid="stSidebar"][aria-expanded="false"] > div:first-child{
width: 350px
margin-left: -350px
}
</style>
""",
unsafe_allow_html=True,
)
## Create Sidebar
st.sidebar.title('Selection')
# Add Debug Mode selection
debug_mode = st.sidebar.radio(
"Select Debug Mode",
('Off', 'On')
)
st.sidebar.subheader('Parameter')
def main():
## Define available pages in selection box
app_mode = st.sidebar.selectbox(
'App Mode',
['Image','Video','About']
)
# About Page
if app_mode == 'About':
image = Image.open('about.png')
col1, col2 = st.columns([1, 2])
with col1:
# Display the image in the first column
st.markdown(
"""
<style>
.image-container {
margin-top: 50px;
}
</style>
<div class="image-container">
<img src="about.png" alt="">
</div>
""", unsafe_allow_html=True
)
st.image(image, caption='')
with col2:
# Display the About message in the second column
st.write("""
## About VisualAI
The purpose of this application is to interprete the states of faces in a scenario with persons
to feed this to loacal LLM AI interfaced by Langchain.\n
**Google Mediapipe** is used to perform this. **Streamlit** is used for the media interface (GUI).\n
- **Martin Hummel**,\n
Senior Software Engineer,\n
Germany/Bavaria/Regensburg\n
- Email: jupp@linuxmail.org\n
- [Github](https://github.com/MartinsRepo/VisualAI) \n
**Dec. 2023**
""")
# Image Page
elif app_mode == 'Image':
# cleanup
st.cache_data.clear()
st.cache_resource.clear()
left_placeholder, empty_placeholder, right_placeholder = st.columns([2, 1, 4])
st.sidebar.markdown('---')
## Add Sidebar and Window style
st.markdown(
"""
<style>
[data-testid="stSidebar"][aria-expanded="true"] > div:first-child{
width: 350px
}
[data-testid="stSidebar"][aria-expanded="false"] > div:first-child{
width: 350px
margin-left: -350px
}
</style>
""",
unsafe_allow_html=True,
)
max_faces = st.sidebar.number_input('Maximum Number of Faces', value=3, min_value=1, max_value=5)
st.sidebar.markdown('---')
detection_confidence = st.sidebar.slider('Min Detection Confidence', min_value=0.0,max_value=1.0,value=0.5)
st.sidebar.markdown('---')
## Output
with left_placeholder:
st.markdown('## Output Image')
img_file_buffer = st.sidebar.file_uploader("Upload an Image", type=["jpg","jpeg","png"])
if img_file_buffer is not None:
image = np.array(Image.open(img_file_buffer))
uploaded_file = io.TextIOWrapper(img_file_buffer) # retrieve uploaded filename
else:
demo_image = DEMO_IMAGE
image = np.array(Image.open(demo_image))
st.sidebar.text('Original Image')
st.sidebar.image(image)
face_count=0
## Dashboard
with mp.solutions.face_mesh.FaceMesh(
static_image_mode=True, #Set of unrelated images
max_num_faces=max_faces,
min_detection_confidence=detection_confidence,
refine_landmarks=False,
min_tracking_confidence=0.01
) as face_mesh:
results = face_mesh.process(image)
out_image=image.copy()
if img_file_buffer is None:
imgfilename = "demo.jpg"
else:
imgfilename = uploaded_file.name
analysis.decode_image_mediapipe( out_image, imgfilename, results, face_count, left_placeholder, right_placeholder, debug_mode)
# Video Page
elif app_mode == 'Video':
# Selector for video interface number
video_interface_number = st.sidebar.number_input('Select Video Interface Number', min_value=0, value=0, step=1)
use_webcam = st.sidebar.button('Use Webcam')
record = st.sidebar.checkbox("Record Video")
if record:
st.checkbox('Recording', True)
st.sidebar.markdown('---')
## Add Sidebar and Window style
st.markdown(
"""
<style>
[data-testid="stSidebar"][aria-expanded="true"] > div:first-child{
width: 350px
}
[data-testid="stSidebar"][aria-expanded="false"] > div:first-child{
width: 350px
margin-left: -350px
}
</style>
""",
unsafe_allow_html=True,
)
max_faces = st.sidebar.number_input('Maximum Number of Faces', value=2, min_value=1, max_value=5)
st.sidebar.markdown('---')
detection_confidence = st.sidebar.slider('Min Detection Confidence', min_value=0.0,max_value=1.0,value=0.5)
tracking_confidence = st.sidebar.slider('Min Tracking Confidence', min_value=0.0,max_value=1.0,value=0.5)
st.sidebar.markdown('---')
## Get Video
video_file_buffer = st.sidebar.file_uploader("Upload a Video", type=['mp4', 'mov', 'avi'])
temp_file = tempfile.NamedTemporaryFile(delete=False)
if not video_file_buffer:
if use_webcam:
video = cv.VideoCapture(video_interface_number)
else:
video = cv.VideoCapture(DEMO_VIDEO)
temp_file.name = DEMO_VIDEO
else:
temp_file.write(video_file_buffer.read())
video = cv.VideoCapture(temp_file.name)
width = int(video.get(cv.CAP_PROP_FRAME_WIDTH))
height = int(video.get(cv.CAP_PROP_FRAME_HEIGHT))
fps_input = int(video.get(cv.CAP_PROP_FPS))
## Recording
codec = cv.VideoWriter_fourcc(*"mp4v")
out = cv.VideoWriter('output.mp4', codec, fps_input, (width,height))
st.sidebar.text('Input Video')
st.sidebar.video(temp_file.name)
fps = 0
i = 0
analysis.decode_video_mediapipe(video, max_faces, detection_confidence, tracking_confidence, debug_mode)
try:
os.remove('output.mp4')
except OSError as e:
# If it fails, inform the user.
print("Error: %s - %s." % (e.filename, e.strerror))
if __name__ == "__main__":
main()