-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
359 lines (313 loc) · 13.8 KB
/
app.py
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import streamlit as st
import cv2
import mediapipe as mp
import numpy as np
import time
from utils import record_response,save_video,record_video
from detector import detect_face_movement
import keyboard
import os
import psutil
questions = [
"Can technology solve all of humanity’s problems?",
"Is climate change a significant threat to the planet?",
"Should genetically modified organisms (GMOs) be banned?",
"Is social media harmful to society?",
"Should recreational marijuana be legalized?",
"Is online learning as effective as in-person learning?",
"Should the minimum wage be raised?",
"Is space exploration worth the cost?",
"Should voting be made mandatory?",
"Is artificial intelligence dangerous?"
]
def set_default_dark_theme():
theme = """
<style>
[data-testid="stHorizontalBlock"] .stDataFrame {
color: black;
}
</style>
"""
st.markdown(theme, unsafe_allow_html=True)
set_default_dark_theme()
def prompt_question(questionInd,container):
question = questions[questionInd]
container.markdown(f"<h3>{question}</h3>", unsafe_allow_html=True)
#Function for creating a countdown when user presses answer question, which opens camera after 5 seconds
def countdown_and_answer(cap, stframe, question_index, question):
print(question_index+1)
#Print Question in H2
question_text = f"<h2 style='color: white;font-weight: bold;'>Question {question_index+1}: {questions[question_index]}</h2>"
#Container in streamlit is easy to manage elements instead of st.write()
question_text_container = st.empty() # Container for question text
question_text_container.write(question_text, unsafe_allow_html=True)
countdown=st.empty()
success_text = st.empty()
info_text = st.empty()
#countdown for 5 seconds
for i in range(5, 0, -1):
countdown.markdown(f"<h1 style='text-align: center; color: yellow;'>{i}</h1>",
unsafe_allow_html=True)
time.sleep(1)
#Clear once done
countdown.empty()
success_text.success('You can start answering')
time.sleep(0.5)
#Clear
success_text.empty()
info_text.info("Recording 3-second video clip...")
#Record and detect logic : We record First
frames = record_video(cap, stframe, duration=3)
info_text.info("Video clip recorded successfully.")
info_text.empty()
response = detect_face_movement(frames, question, stframe)
question_text_container.empty()
save_video(frames, f"question_{question_index + 1}_response.avi")
while response=="Undetermined":
#Need to restart loop
warn_text = st.empty()
warn_text.warning("Response is undetermined. Please try again.")
time.sleep(0.5)
warn_text.empty()
info_text.info("Recording 3-second video clip...")
frames = record_video(cap, stframe, duration=3)
info_text.info("Video clip recorded successfully.")
info_text.empty() # Clear the info message
response = detect_face_movement(frames, question, stframe)
save_video(frames, f"question_{question_index + 1}_response.avi")
while response=="Restart":
warn_text = st.empty()
warn_text.warning("Face is not visible clearly! Please come close to the camera")
time.sleep(1)
warn_text.empty()
info_text.info("Recording 3-second video clip...")
frames = record_video(cap, stframe, duration=3)
info_text.info("Video clip recorded successfully.")
info_text.empty() # Clear the info message
response = detect_face_movement(frames, question, stframe)
save_video(frames, f"question_{question_index + 1}_response.avi")
record_response(questions[question_index], response)
if response == "Yes":
st.sidebar.write(f"<h3 style='text-align: center; color: white;'>Your response: </h3>"
f"<h3 style='text-align: center; color: green;'>{response}</h3>",
unsafe_allow_html=True)
else:
st.sidebar.write(f"<h3 style='text-align: center; color: white;'>Your response: </h3>"
f"<h3 style='text-align: center; color: red;'>{response}</h3>",
unsafe_allow_html=True)
##Function to terminate program
def terminate():
st.empty()
st.title("Please close the browser window to quit the app.")
time.sleep(4)
# keyboard.press_and_release('ctrl+w')
os.kill(os.getpid(),2)
# Terminate streamlit python process
pid = os.getpid()
p = psutil.Process(pid)
p.terminate()
st.stop()
#Handle end of list : To prompt user to quit app or use again
def handle_eol(nextQueInd):
if nextQueInd == len(questions):
print("This loop")
st.markdown("<h2 style='color: white;> All Questions Have been Answered </h2> ",unsafe_allow_html=True)
st.markdown("<h2 style='color: white;>Do you want to Quit or Restart? </h2> ",unsafe_allow_html=True)
col3,col4=st.columns(2)
with col3:
if st.button("Quit"):
terminate()
with col4:
if st.button("Restart"):
nextQueInd=0
st.session_state.current_question_index = 0
print("lauda", st.session_state.current_question_index)
st.session_state.show_next_step_button = False
st.empty()
# Re-run the main function to restart the application
main()
return st.session_state.current_question_index
#To hide texts and turn reanswe state true
def show_hide():
st.empty()
st.session_state.reanswer = True
st.session_state.show_next_step_button = False
def contd_handle():
nextQueInd = st.session_state.current_question_index+1
if nextQueInd >= len(questions):
st.markdown("<h2 style='color: white;> All Questions Have been Answered </h2> ",unsafe_allow_html=True)
st.markdown("<h2 style='color: white;>Do you want to Quit or Restart? </h2> ",unsafe_allow_html=True)
col3,col4=st.columns(2)
with col3:
if st.button("Quit"):
terminate()
with col4:
if st.button("Restart"):
nextQueInd=0
nextQueInd = st.session_state.current_question_index+1
st.session_state.current_question_index = nextQueInd
st.session_state.show_next_step_button = False
def main():
st.title('VSTBalance - Daily Checkup')
st.markdown(f"<h5 style='text-align: left;'>Empower your nods, unlock answers. 🤖💡</h5>",unsafe_allow_html=True)
text_body=st.empty()
reanswer_info_text=st.empty()
instructions = """
### Instructions
- Click start to start the application.
- When the question pops up, press "Answer" to answer the question or "Continue" to skip.
- When the screen pops up, follow the below GIFs to understand how to nod.
"""
nod_yes_text = "## To nod yes, do this"
nod_yes_gif = "https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExbjI0ZDA3emc5eWNjczluMm8wNHB6ODZnYmwwc3RmOHZkcjQ1eXFvcSZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/WoaeuzCFZ1TRsmwf1t/giphy.gif"
nod_no_text = "## To nod no, do this"
nod_no_gif = "https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExdDk5MXBjeTI1N2FtdDZyZThzb2phMm9majRlcmtmb3E5aHhybzhkZiZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/QuhgjzQ3PaWE8NiiMJ/giphy-downsized-large.gif"
reanswer_info = f"<h4 style='color : White;'>If you want to reanswer, press 'Reanswer'. Else, press continue to the next question.</h4>"
text_body.markdown(instructions)
reanswer_info_text.markdown(reanswer_info,unsafe_allow_html=True)
#Column for the Gifs
stframe = st.empty()
#creating sidebar to start the app using container and creating a session to track user input
####BUtton Containers ######
StartBtnContainer = st.sidebar.empty()
AnswerBtnContainer = st.sidebar.empty() # To press answer ques
changeBtnContainer = st.sidebar.empty() #To press change ques
ContinueBtnContainer = st.empty() #To press continue
EndBtnContainer = st.sidebar.empty() # To press End
ReanswerBtnContainer = st.empty() #To press reanswer
#####################################
#Setting deefault sidebar size
st.markdown(
"""
<style>
section[data-testid="stSidebar"] {
width: 500px !important;
}
</style>
""",
unsafe_allow_html=True,
)
##### Im creating text containers since there's no animation in streaamlit
TitleContainer = st.sidebar.empty()
ImgContainer=st.sidebar.empty()
LicenseContainer=st.sidebar.empty()
textContainer=st.sidebar.empty()
qtnbtncontainer=st.sidebar.empty()
##########################################################################
#Create Logo in Sidebar
st.session_state.title="VSTBalance - Daily Checkup"
st.session_state.img=f'<img src = "https://www.virtusense.ai/hubfs/Site%20content/Imported_Blog_Media/Logo-bar-black-768x127.png" style="position: absolute; top: 55px; left: 10px; width: 400px;">'
st.session_state.license="""
<div style="position: fixed; bottom: 10px; left: 15%; align: center; transform: translateX(-50%); background-color: #333; padding: 10px; border-top: 1px solid #555; text-align: center;">
<p style="margin: 0; color: #fff;">© 2024 AchuthanKrishna. All rights reserved.</p>
</div>
"""
######SESSION STATES #########
if 'start' not in st.session_state:
st.session_state.start = False
if 'quit' not in st.session_state: #For quit
st.session_state.quit = False
if 'current_question' not in st.session_state: #current ques is first
st.session_state.current_question = questions[0]
if 'current_question_index' not in st.session_state: #start from 0
st.session_state.current_question_index = 0
if "change_ques"not in st.session_state:
st.session_state.change_que = False
if 'reanswer' not in st.session_state:
st.session_state.reanswer = False
if 'show_next_step_button' not in st.session_state:
st.session_state.show_next_step_button = False
########################################
reans_question, continue_question = st.columns([1,1])
col1, col2, col3 = st.sidebar.columns([1,2,1])
with col1:
if StartBtnContainer.button("Start",type="primary",use_container_width=True):
st.session_state.start = True
#Quit to exit app
with col2:
if qtnbtncontainer.button("Quit App",type="primary",use_container_width=True):
st.session_state.quit=True
if st.session_state.quit:
StartBtnContainer.empty()
qtnbtncontainer.empty()
text_body.empty()
reanswer_info_text.empty()
st.empty()
nod_no_gif=" "
nod_yes_gif=" "
nod_yes_text=" "
nod_no_text=" "
st.title("Please close the browser window to quit the app.")
time.sleep(4)
# keyboard.press_and_release('ctrl+c')
os.kill(os.getpid(),2)
# Terminate streamlit python process
pid = os.getpid()
p = psutil.Process(pid)
p.terminate()
st.stop()
#If pressed Start
if st.session_state.start:
text_body.empty()
StartBtnContainer.empty()
st.session_state.img = " "
st.session_state.title="Questions"
cap = cv2.VideoCapture(0)
st.sidebar.empty()
container = st.sidebar.container() #Check status of sidebar
ans_que = st.sidebar.button(f"Answer Question",key="ans_que") #answer Question button
change_que = st.sidebar.button(f"Change Question") #Change Ques
if change_que :
nextQueInd = (st.session_state.current_question_index+1)
print(len(questions))
print(nextQueInd)
st.session_state.current_question_index = nextQueInd
if nextQueInd==len(questions):
reanswer_info_text.empty()
nextQueInda=handle_eol(nextQueInd)
st.session_state.current_question_index=0
print(st.session_state.current_question_index)
container.empty()
else :
container.empty()
if ans_que:
text_body.empty()
countdown_and_answer(cap, stframe, st.session_state.current_question_index, st.session_state.current_question)
st.session_state.show_next_step_button = True
#If user wants to reanswer
if st.session_state.reanswer :
text_body.empty()
countdown_and_answer(cap, stframe, st.session_state.current_question_index, st.session_state.current_question)
st.session_state.reanswer = False
st.session_state.show_next_step_button = True
with reans_question:
if st.session_state.show_next_step_button:
ReanswerBtnContainer.button("Reanswer", on_click=show_hide,use_container_width=True)
with continue_question:
if st.session_state.show_next_step_button:
ContinueBtnContainer.button("Continue", on_click=contd_handle,use_container_width=True)
prompt_question(st.session_state.current_question_index,container)
TitleContainer.title(st.session_state.title)
ImgContainer.markdown(st.session_state.img, unsafe_allow_html=True,)
LicenseContainer.markdown(st.session_state.license, unsafe_allow_html=True,)
img_b1,img_b2 = st.columns(2)
with img_b1:
st.markdown(nod_yes_text)
st.image(nod_yes_gif,width=200)
with img_b2:
st.markdown(nod_no_text)
st.image(nod_no_gif,width=200)
##Primary Button Styling
st.markdown("""
<style>
button[kind="primary"] {
background: orange;
position: absolute;
top: 300px; left: 0px;
text-decoration: none;
cursor: pointer;
border: none !important;
}
</style>""", unsafe_allow_html=True)
if __name__ == '__main__':
main()