Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import os
|
|
| 2 |
import csv
|
| 3 |
import zipfile
|
| 4 |
import shutil
|
|
|
|
| 5 |
from datetime import datetime
|
| 6 |
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
|
| 7 |
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
|
|
@@ -11,7 +12,6 @@ import gradio as gr
|
|
| 11 |
from deepface import DeepFace
|
| 12 |
import numpy as np
|
| 13 |
from PIL import Image
|
| 14 |
-
import time
|
| 15 |
from pathlib import Path
|
| 16 |
import pandas as pd
|
| 17 |
|
|
@@ -55,7 +55,7 @@ def validate_batch_number(batch_no):
|
|
| 55 |
return True, ""
|
| 56 |
|
| 57 |
def toggle_webcam_flow(batch_no):
|
| 58 |
-
"""Control visibility of components based on flow"""
|
| 59 |
if batch_no.strip():
|
| 60 |
# Validate batch number
|
| 61 |
is_valid, validation_msg = validate_batch_number(batch_no)
|
|
@@ -64,15 +64,17 @@ def toggle_webcam_flow(batch_no):
|
|
| 64 |
batch_no_input: gr.Textbox(visible=True, value=""),
|
| 65 |
webcam: gr.Image(visible=False),
|
| 66 |
message: gr.Textbox(value=validation_msg, visible=True),
|
| 67 |
-
batch_no_state: ""
|
|
|
|
| 68 |
}
|
| 69 |
|
| 70 |
-
#
|
| 71 |
return {
|
| 72 |
batch_no_input: gr.Textbox(visible=False),
|
| 73 |
-
webcam: gr.Image(visible=
|
| 74 |
-
message: gr.Textbox(visible=
|
| 75 |
-
batch_no_state: batch_no
|
|
|
|
| 76 |
}
|
| 77 |
else:
|
| 78 |
# No batch number - show input, hide webcam
|
|
@@ -80,9 +82,19 @@ def toggle_webcam_flow(batch_no):
|
|
| 80 |
batch_no_input: gr.Textbox(visible=True),
|
| 81 |
webcam: gr.Image(visible=False),
|
| 82 |
message: gr.Textbox(visible=False),
|
| 83 |
-
batch_no_state: ""
|
|
|
|
| 84 |
}
|
| 85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
def predict_emotion(batch_no: str, image):
|
| 87 |
if not batch_no.strip():
|
| 88 |
return {
|
|
@@ -92,7 +104,9 @@ def predict_emotion(batch_no: str, image):
|
|
| 92 |
result_img_comp: gr.Image(visible=False),
|
| 93 |
result_text_comp: gr.Textbox(visible=False),
|
| 94 |
batch_no_input: gr.Textbox(visible=True),
|
| 95 |
-
webcam: gr.Image(visible=False)
|
|
|
|
|
|
|
| 96 |
}
|
| 97 |
|
| 98 |
# Validate batch number format
|
|
@@ -105,7 +119,9 @@ def predict_emotion(batch_no: str, image):
|
|
| 105 |
result_img_comp: gr.Image(visible=False),
|
| 106 |
result_text_comp: gr.Textbox(visible=False),
|
| 107 |
batch_no_input: gr.Textbox(visible=True),
|
| 108 |
-
webcam: gr.Image(visible=False)
|
|
|
|
|
|
|
| 109 |
}
|
| 110 |
|
| 111 |
if image is None:
|
|
@@ -116,7 +132,9 @@ def predict_emotion(batch_no: str, image):
|
|
| 116 |
result_img_comp: gr.Image(visible=False),
|
| 117 |
result_text_comp: gr.Textbox(visible=False),
|
| 118 |
batch_no_input: gr.Textbox(visible=False),
|
| 119 |
-
webcam: gr.Image(visible=True)
|
|
|
|
|
|
|
| 120 |
}
|
| 121 |
|
| 122 |
try:
|
|
@@ -148,7 +166,9 @@ def predict_emotion(batch_no: str, image):
|
|
| 148 |
result_img_comp: gr.Image(visible=False),
|
| 149 |
result_text_comp: gr.Textbox(visible=False),
|
| 150 |
batch_no_input: gr.Textbox(visible=True),
|
| 151 |
-
webcam: gr.Image(visible=False)
|
|
|
|
|
|
|
| 152 |
}
|
| 153 |
|
| 154 |
# Process the first face found
|
|
@@ -190,7 +210,9 @@ def predict_emotion(batch_no: str, image):
|
|
| 190 |
result_img_comp: gr.Image(visible=True),
|
| 191 |
result_text_comp: gr.Textbox(visible=True),
|
| 192 |
batch_no_input: gr.Textbox(visible=True, value=""),
|
| 193 |
-
webcam: gr.Image(visible=False)
|
|
|
|
|
|
|
| 194 |
}
|
| 195 |
|
| 196 |
except Exception as e:
|
|
@@ -201,9 +223,23 @@ def predict_emotion(batch_no: str, image):
|
|
| 201 |
result_img_comp: gr.Image(visible=False),
|
| 202 |
result_text_comp: gr.Textbox(visible=False),
|
| 203 |
batch_no_input: gr.Textbox(visible=True, value=""),
|
| 204 |
-
webcam: gr.Image(visible=False)
|
|
|
|
|
|
|
| 205 |
}
|
| 206 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
def get_image_gallery(emotion, image_type):
|
| 208 |
"""Get image gallery for selected emotion and type"""
|
| 209 |
if emotion == "All Emotions":
|
|
@@ -444,14 +480,22 @@ with gr.Blocks(title="Emotion Capture", css="""
|
|
| 444 |
.gradio-container { max-width: 800px !important }
|
| 445 |
.message { color: red; font-weight: bold; }
|
| 446 |
.gallery { grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 447 |
""") as capture_interface:
|
| 448 |
|
| 449 |
gr.Markdown("""
|
| 450 |
# Emotion Capture Interface
|
| 451 |
-
1. Enter your batch number (numbers only)
|
| 452 |
-
2. Capture your face (webcam will appear)
|
| 453 |
3. View your emotion analysis results
|
| 454 |
-
4.
|
| 455 |
""")
|
| 456 |
|
| 457 |
# Add a state to store batch number between callbacks
|
|
@@ -465,6 +509,14 @@ with gr.Blocks(title="Emotion Capture", css="""
|
|
| 465 |
)
|
| 466 |
message = gr.Textbox(label="", visible=False, interactive=False, elem_classes="message")
|
| 467 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 468 |
# Webcam component starts hidden
|
| 469 |
webcam = gr.Image(
|
| 470 |
sources=["webcam"],
|
|
@@ -485,11 +537,23 @@ with gr.Blocks(title="Emotion Capture", css="""
|
|
| 485 |
result_img_comp = gr.Image(visible=False)
|
| 486 |
result_text_comp = gr.Textbox(visible=False)
|
| 487 |
|
| 488 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 489 |
batch_no_input.change(
|
| 490 |
toggle_webcam_flow,
|
| 491 |
inputs=batch_no_input,
|
| 492 |
-
outputs=[batch_no_input, webcam, message, batch_no_state]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 493 |
)
|
| 494 |
|
| 495 |
# Process when webcam captures an image
|
|
@@ -503,7 +567,23 @@ with gr.Blocks(title="Emotion Capture", css="""
|
|
| 503 |
result_img_comp,
|
| 504 |
result_text_comp,
|
| 505 |
batch_no_input,
|
| 506 |
-
webcam
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 507 |
]
|
| 508 |
)
|
| 509 |
|
|
@@ -703,7 +783,8 @@ with gr.Blocks(title="Data Management") as data_interface:
|
|
| 703 |
# Combine interfaces
|
| 704 |
demo = gr.TabbedInterface(
|
| 705 |
[capture_interface, data_interface],
|
| 706 |
-
["Emotion Capture", "Data Management"]
|
|
|
|
| 707 |
)
|
| 708 |
|
| 709 |
if __name__ == "__main__":
|
|
|
|
| 2 |
import csv
|
| 3 |
import zipfile
|
| 4 |
import shutil
|
| 5 |
+
import time
|
| 6 |
from datetime import datetime
|
| 7 |
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
|
| 8 |
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
|
|
|
|
| 12 |
from deepface import DeepFace
|
| 13 |
import numpy as np
|
| 14 |
from PIL import Image
|
|
|
|
| 15 |
from pathlib import Path
|
| 16 |
import pandas as pd
|
| 17 |
|
|
|
|
| 55 |
return True, ""
|
| 56 |
|
| 57 |
def toggle_webcam_flow(batch_no):
|
| 58 |
+
"""Control visibility of components based on flow with added delay"""
|
| 59 |
if batch_no.strip():
|
| 60 |
# Validate batch number
|
| 61 |
is_valid, validation_msg = validate_batch_number(batch_no)
|
|
|
|
| 64 |
batch_no_input: gr.Textbox(visible=True, value=""),
|
| 65 |
webcam: gr.Image(visible=False),
|
| 66 |
message: gr.Textbox(value=validation_msg, visible=True),
|
| 67 |
+
batch_no_state: "",
|
| 68 |
+
processing: gr.Textbox(visible=False)
|
| 69 |
}
|
| 70 |
|
| 71 |
+
# Show processing message before showing webcam
|
| 72 |
return {
|
| 73 |
batch_no_input: gr.Textbox(visible=False),
|
| 74 |
+
webcam: gr.Image(visible=False),
|
| 75 |
+
message: gr.Textbox(value=f"Processing batch {batch_no}...", visible=True),
|
| 76 |
+
batch_no_state: batch_no,
|
| 77 |
+
processing: gr.Textbox(value="Please wait...", visible=True)
|
| 78 |
}
|
| 79 |
else:
|
| 80 |
# No batch number - show input, hide webcam
|
|
|
|
| 82 |
batch_no_input: gr.Textbox(visible=True),
|
| 83 |
webcam: gr.Image(visible=False),
|
| 84 |
message: gr.Textbox(visible=False),
|
| 85 |
+
batch_no_state: "",
|
| 86 |
+
processing: gr.Textbox(visible=False)
|
| 87 |
}
|
| 88 |
|
| 89 |
+
def show_webcam_after_delay(batch_no):
|
| 90 |
+
"""Show webcam after a short delay"""
|
| 91 |
+
time.sleep(3) # 3 second delay
|
| 92 |
+
return {
|
| 93 |
+
webcam: gr.Image(visible=True),
|
| 94 |
+
message: gr.Textbox(visible=False),
|
| 95 |
+
processing: gr.Textbox(visible=False)
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
def predict_emotion(batch_no: str, image):
|
| 99 |
if not batch_no.strip():
|
| 100 |
return {
|
|
|
|
| 104 |
result_img_comp: gr.Image(visible=False),
|
| 105 |
result_text_comp: gr.Textbox(visible=False),
|
| 106 |
batch_no_input: gr.Textbox(visible=True),
|
| 107 |
+
webcam: gr.Image(visible=False),
|
| 108 |
+
done_btn: gr.Button(visible=False),
|
| 109 |
+
processing: gr.Textbox(visible=False)
|
| 110 |
}
|
| 111 |
|
| 112 |
# Validate batch number format
|
|
|
|
| 119 |
result_img_comp: gr.Image(visible=False),
|
| 120 |
result_text_comp: gr.Textbox(visible=False),
|
| 121 |
batch_no_input: gr.Textbox(visible=True),
|
| 122 |
+
webcam: gr.Image(visible=False),
|
| 123 |
+
done_btn: gr.Button(visible=False),
|
| 124 |
+
processing: gr.Textbox(visible=False)
|
| 125 |
}
|
| 126 |
|
| 127 |
if image is None:
|
|
|
|
| 132 |
result_img_comp: gr.Image(visible=False),
|
| 133 |
result_text_comp: gr.Textbox(visible=False),
|
| 134 |
batch_no_input: gr.Textbox(visible=False),
|
| 135 |
+
webcam: gr.Image(visible=True),
|
| 136 |
+
done_btn: gr.Button(visible=False),
|
| 137 |
+
processing: gr.Textbox(visible=False)
|
| 138 |
}
|
| 139 |
|
| 140 |
try:
|
|
|
|
| 166 |
result_img_comp: gr.Image(visible=False),
|
| 167 |
result_text_comp: gr.Textbox(visible=False),
|
| 168 |
batch_no_input: gr.Textbox(visible=True),
|
| 169 |
+
webcam: gr.Image(visible=False),
|
| 170 |
+
done_btn: gr.Button(visible=False),
|
| 171 |
+
processing: gr.Textbox(visible=False)
|
| 172 |
}
|
| 173 |
|
| 174 |
# Process the first face found
|
|
|
|
| 210 |
result_img_comp: gr.Image(visible=True),
|
| 211 |
result_text_comp: gr.Textbox(visible=True),
|
| 212 |
batch_no_input: gr.Textbox(visible=True, value=""),
|
| 213 |
+
webcam: gr.Image(visible=False),
|
| 214 |
+
done_btn: gr.Button(visible=True),
|
| 215 |
+
processing: gr.Textbox(visible=False)
|
| 216 |
}
|
| 217 |
|
| 218 |
except Exception as e:
|
|
|
|
| 223 |
result_img_comp: gr.Image(visible=False),
|
| 224 |
result_text_comp: gr.Textbox(visible=False),
|
| 225 |
batch_no_input: gr.Textbox(visible=True, value=""),
|
| 226 |
+
webcam: gr.Image(visible=False),
|
| 227 |
+
done_btn: gr.Button(visible=False),
|
| 228 |
+
processing: gr.Textbox(visible=False)
|
| 229 |
}
|
| 230 |
|
| 231 |
+
def complete_session():
|
| 232 |
+
"""Reset interface after user is done viewing results"""
|
| 233 |
+
return {
|
| 234 |
+
batch_no_input: gr.Textbox(visible=True, value=""),
|
| 235 |
+
webcam: gr.Image(visible=False),
|
| 236 |
+
result_img_comp: gr.Image(visible=False),
|
| 237 |
+
result_text_comp: gr.Textbox(visible=False),
|
| 238 |
+
done_btn: gr.Button(visible=False),
|
| 239 |
+
message: gr.Textbox(value="Session completed. Next user may enter batch number.", visible=True),
|
| 240 |
+
processing: gr.Textbox(visible=False)
|
| 241 |
+
}
|
| 242 |
+
|
| 243 |
def get_image_gallery(emotion, image_type):
|
| 244 |
"""Get image gallery for selected emotion and type"""
|
| 245 |
if emotion == "All Emotions":
|
|
|
|
| 480 |
.gradio-container { max-width: 800px !important }
|
| 481 |
.message { color: red; font-weight: bold; }
|
| 482 |
.gallery { grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); }
|
| 483 |
+
.done-btn { margin-top: 20px; }
|
| 484 |
+
.processing {
|
| 485 |
+
font-size: 1.2em;
|
| 486 |
+
color: #0066cc;
|
| 487 |
+
font-weight: bold;
|
| 488 |
+
text-align: center;
|
| 489 |
+
margin: 20px 0;
|
| 490 |
+
}
|
| 491 |
""") as capture_interface:
|
| 492 |
|
| 493 |
gr.Markdown("""
|
| 494 |
# Emotion Capture Interface
|
| 495 |
+
1. Enter your batch number (numbers only) and wait for confirmation
|
| 496 |
+
2. Capture your face (webcam will appear after validation)
|
| 497 |
3. View your emotion analysis results
|
| 498 |
+
4. Click "Done" when finished to reset for next user
|
| 499 |
""")
|
| 500 |
|
| 501 |
# Add a state to store batch number between callbacks
|
|
|
|
| 509 |
)
|
| 510 |
message = gr.Textbox(label="", visible=False, interactive=False, elem_classes="message")
|
| 511 |
|
| 512 |
+
# Processing indicator (hidden by default)
|
| 513 |
+
processing = gr.Textbox(
|
| 514 |
+
label="",
|
| 515 |
+
visible=False,
|
| 516 |
+
interactive=False,
|
| 517 |
+
elem_classes="processing"
|
| 518 |
+
)
|
| 519 |
+
|
| 520 |
# Webcam component starts hidden
|
| 521 |
webcam = gr.Image(
|
| 522 |
sources=["webcam"],
|
|
|
|
| 537 |
result_img_comp = gr.Image(visible=False)
|
| 538 |
result_text_comp = gr.Textbox(visible=False)
|
| 539 |
|
| 540 |
+
# Done button (starts hidden)
|
| 541 |
+
done_btn = gr.Button(
|
| 542 |
+
"Done",
|
| 543 |
+
visible=False,
|
| 544 |
+
elem_classes="done-btn",
|
| 545 |
+
variant="primary"
|
| 546 |
+
)
|
| 547 |
+
|
| 548 |
+
# Show webcam when valid batch number is entered (with delay)
|
| 549 |
batch_no_input.change(
|
| 550 |
toggle_webcam_flow,
|
| 551 |
inputs=batch_no_input,
|
| 552 |
+
outputs=[batch_no_input, webcam, message, batch_no_state, processing]
|
| 553 |
+
).then(
|
| 554 |
+
show_webcam_after_delay,
|
| 555 |
+
inputs=batch_no_state,
|
| 556 |
+
outputs=[webcam, message, processing]
|
| 557 |
)
|
| 558 |
|
| 559 |
# Process when webcam captures an image
|
|
|
|
| 567 |
result_img_comp,
|
| 568 |
result_text_comp,
|
| 569 |
batch_no_input,
|
| 570 |
+
webcam,
|
| 571 |
+
done_btn,
|
| 572 |
+
processing
|
| 573 |
+
]
|
| 574 |
+
)
|
| 575 |
+
|
| 576 |
+
# Handle session completion
|
| 577 |
+
done_btn.click(
|
| 578 |
+
complete_session,
|
| 579 |
+
outputs=[
|
| 580 |
+
batch_no_input,
|
| 581 |
+
webcam,
|
| 582 |
+
result_img_comp,
|
| 583 |
+
result_text_comp,
|
| 584 |
+
done_btn,
|
| 585 |
+
message,
|
| 586 |
+
processing
|
| 587 |
]
|
| 588 |
)
|
| 589 |
|
|
|
|
| 783 |
# Combine interfaces
|
| 784 |
demo = gr.TabbedInterface(
|
| 785 |
[capture_interface, data_interface],
|
| 786 |
+
["Emotion Capture", "Data Management"],
|
| 787 |
+
title="Emotion Analysis System"
|
| 788 |
)
|
| 789 |
|
| 790 |
if __name__ == "__main__":
|