Update app.py
Browse files
app.py
CHANGED
|
@@ -212,11 +212,11 @@ def process_single_image(image: Image.Image) -> Tuple[Image.Image, str]:
|
|
| 212 |
logger.error(f"Error processing image: {e}")
|
| 213 |
return image, f"Error processing image: {str(e)}"
|
| 214 |
|
| 215 |
-
def process_webcam_frame(image: Image.Image) -> Image.Image:
|
| 216 |
-
"""Process webcam frame for
|
| 217 |
try:
|
| 218 |
if image is None:
|
| 219 |
-
return None
|
| 220 |
|
| 221 |
# Convert PIL to numpy array
|
| 222 |
image_np = np.array(image)
|
|
@@ -225,7 +225,7 @@ def process_webcam_frame(image: Image.Image) -> Image.Image:
|
|
| 225 |
faces = detect_faces(image_np)
|
| 226 |
|
| 227 |
if not faces:
|
| 228 |
-
return image
|
| 229 |
|
| 230 |
# Process each face
|
| 231 |
emotions_list = []
|
|
@@ -240,46 +240,58 @@ def process_webcam_frame(image: Image.Image) -> Image.Image:
|
|
| 240 |
# Draw results
|
| 241 |
result_image = draw_emotion_results(image.copy(), faces, emotions_list)
|
| 242 |
|
| 243 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
|
| 245 |
except Exception as e:
|
| 246 |
logger.error(f"Error processing webcam frame: {e}")
|
| 247 |
-
return image
|
| 248 |
|
| 249 |
-
def analyze_emotions_batch(
|
| 250 |
-
"""Analyze emotions in multiple
|
| 251 |
try:
|
| 252 |
-
if not
|
| 253 |
-
return "No
|
| 254 |
|
| 255 |
all_results = []
|
| 256 |
|
| 257 |
-
for idx,
|
| 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 |
return "\n".join(all_results)
|
| 285 |
|
|
@@ -408,18 +420,33 @@ def create_interface():
|
|
| 408 |
gr.Markdown(
|
| 409 |
"""
|
| 410 |
### Real-time Emotion Detection
|
| 411 |
-
|
| 412 |
"""
|
| 413 |
)
|
| 414 |
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 423 |
|
| 424 |
with gr.Tab("📊 Detailed Statistics"):
|
| 425 |
with gr.Row():
|
|
@@ -516,6 +543,18 @@ def create_interface():
|
|
| 516 |
api_name="analyze_single_image"
|
| 517 |
)
|
| 518 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 519 |
analyze_stats_btn.click(
|
| 520 |
fn=get_emotion_statistics,
|
| 521 |
inputs=stats_image_input,
|
|
@@ -545,6 +584,16 @@ if __name__ == "__main__":
|
|
| 545 |
if load_models():
|
| 546 |
logger.info("Models loaded successfully!")
|
| 547 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 548 |
# Create interface
|
| 549 |
iface = create_interface()
|
| 550 |
|
|
@@ -558,4 +607,26 @@ if __name__ == "__main__":
|
|
| 558 |
show_api=True
|
| 559 |
)
|
| 560 |
else:
|
| 561 |
-
logger.error("Failed to load models. Please check your model configuration.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
logger.error(f"Error processing image: {e}")
|
| 213 |
return image, f"Error processing image: {str(e)}"
|
| 214 |
|
| 215 |
+
def process_webcam_frame(image: Image.Image) -> Tuple[Image.Image, str]:
|
| 216 |
+
"""Process webcam frame for emotion detection"""
|
| 217 |
try:
|
| 218 |
if image is None:
|
| 219 |
+
return None, "No image provided"
|
| 220 |
|
| 221 |
# Convert PIL to numpy array
|
| 222 |
image_np = np.array(image)
|
|
|
|
| 225 |
faces = detect_faces(image_np)
|
| 226 |
|
| 227 |
if not faces:
|
| 228 |
+
return image, "No faces detected in the frame"
|
| 229 |
|
| 230 |
# Process each face
|
| 231 |
emotions_list = []
|
|
|
|
| 240 |
# Draw results
|
| 241 |
result_image = draw_emotion_results(image.copy(), faces, emotions_list)
|
| 242 |
|
| 243 |
+
# Create summary text
|
| 244 |
+
summary_lines = [f"Detected {len(faces)} face(s):"]
|
| 245 |
+
for i, emotions in enumerate(emotions_list):
|
| 246 |
+
top_emotion = max(emotions, key=lambda x: x['score'])
|
| 247 |
+
summary_lines.append(f"Face {i+1}: {top_emotion['label']} ({top_emotion['score']:.2f})")
|
| 248 |
+
|
| 249 |
+
summary = "\n".join(summary_lines)
|
| 250 |
+
|
| 251 |
+
return result_image, summary
|
| 252 |
|
| 253 |
except Exception as e:
|
| 254 |
logger.error(f"Error processing webcam frame: {e}")
|
| 255 |
+
return image, f"Error processing frame: {str(e)}"
|
| 256 |
|
| 257 |
+
def analyze_emotions_batch(files) -> str:
|
| 258 |
+
"""Analyze emotions in multiple uploaded files"""
|
| 259 |
try:
|
| 260 |
+
if not files:
|
| 261 |
+
return "No files provided"
|
| 262 |
|
| 263 |
all_results = []
|
| 264 |
|
| 265 |
+
for idx, file in enumerate(files):
|
| 266 |
+
try:
|
| 267 |
+
# Open the image file
|
| 268 |
+
image = Image.open(file.name)
|
| 269 |
|
| 270 |
+
# Convert PIL to numpy array
|
| 271 |
+
image_np = np.array(image)
|
| 272 |
+
|
| 273 |
+
# Detect faces
|
| 274 |
+
faces = detect_faces(image_np)
|
| 275 |
+
|
| 276 |
+
if not faces:
|
| 277 |
+
all_results.append(f"File {idx+1} ({file.name}): No faces detected")
|
| 278 |
+
continue
|
| 279 |
+
|
| 280 |
+
# Process each face
|
| 281 |
+
image_emotions = []
|
| 282 |
+
for (x, y, w, h) in faces:
|
| 283 |
+
# Extract face region
|
| 284 |
+
face_region = image.crop((x, y, x + w, y + h))
|
| 285 |
+
|
| 286 |
+
# Predict emotion
|
| 287 |
+
emotions = predict_emotion(face_region)
|
| 288 |
+
top_emotion = max(emotions, key=lambda x: x['score'])
|
| 289 |
+
image_emotions.append(f"{top_emotion['label']} ({top_emotion['score']:.2f})")
|
| 290 |
+
|
| 291 |
+
all_results.append(f"File {idx+1} ({file.name}): {', '.join(image_emotions)}")
|
| 292 |
+
|
| 293 |
+
except Exception as e:
|
| 294 |
+
all_results.append(f"File {idx+1}: Error processing - {str(e)}")
|
| 295 |
|
| 296 |
return "\n".join(all_results)
|
| 297 |
|
|
|
|
| 420 |
gr.Markdown(
|
| 421 |
"""
|
| 422 |
### Real-time Emotion Detection
|
| 423 |
+
Upload images from your camera or device to see emotion detection results!
|
| 424 |
"""
|
| 425 |
)
|
| 426 |
|
| 427 |
+
with gr.Row():
|
| 428 |
+
with gr.Column(scale=1):
|
| 429 |
+
webcam_input = gr.Image(
|
| 430 |
+
label="Camera Input",
|
| 431 |
+
type="pil",
|
| 432 |
+
height=400,
|
| 433 |
+
sources=["webcam", "upload"]
|
| 434 |
+
)
|
| 435 |
+
|
| 436 |
+
with gr.Row():
|
| 437 |
+
process_webcam_btn = gr.Button("Analyze Frame", variant="primary", size="lg")
|
| 438 |
+
clear_webcam_btn = gr.Button("Clear", variant="secondary")
|
| 439 |
+
|
| 440 |
+
with gr.Column(scale=1):
|
| 441 |
+
webcam_output = gr.Image(
|
| 442 |
+
label="Emotion Detection Results",
|
| 443 |
+
height=400
|
| 444 |
+
)
|
| 445 |
+
webcam_result_text = gr.Textbox(
|
| 446 |
+
label="Detection Results",
|
| 447 |
+
lines=5,
|
| 448 |
+
show_copy_button=True
|
| 449 |
+
)
|
| 450 |
|
| 451 |
with gr.Tab("📊 Detailed Statistics"):
|
| 452 |
with gr.Row():
|
|
|
|
| 543 |
api_name="analyze_single_image"
|
| 544 |
)
|
| 545 |
|
| 546 |
+
process_webcam_btn.click(
|
| 547 |
+
fn=process_webcam_frame,
|
| 548 |
+
inputs=webcam_input,
|
| 549 |
+
outputs=[webcam_output, webcam_result_text],
|
| 550 |
+
api_name="process_webcam"
|
| 551 |
+
)
|
| 552 |
+
|
| 553 |
+
clear_webcam_btn.click(
|
| 554 |
+
fn=lambda: (None, None, ""),
|
| 555 |
+
outputs=[webcam_input, webcam_output, webcam_result_text]
|
| 556 |
+
)
|
| 557 |
+
|
| 558 |
analyze_stats_btn.click(
|
| 559 |
fn=get_emotion_statistics,
|
| 560 |
inputs=stats_image_input,
|
|
|
|
| 584 |
if load_models():
|
| 585 |
logger.info("Models loaded successfully!")
|
| 586 |
|
| 587 |
+
# Test the model with a simple image
|
| 588 |
+
try:
|
| 589 |
+
# Create a test image
|
| 590 |
+
test_image = Image.new('RGB', (224, 224), color='white')
|
| 591 |
+
test_result = predict_emotion(test_image)
|
| 592 |
+
logger.info(f"Model test successful. Result format: {type(test_result)}")
|
| 593 |
+
logger.info(f"Test result: {test_result}")
|
| 594 |
+
except Exception as e:
|
| 595 |
+
logger.error(f"Model test failed: {e}")
|
| 596 |
+
|
| 597 |
# Create interface
|
| 598 |
iface = create_interface()
|
| 599 |
|
|
|
|
| 607 |
show_api=True
|
| 608 |
)
|
| 609 |
else:
|
| 610 |
+
logger.error("Failed to load models. Please check your model configuration.")
|
| 611 |
+
# Create a simple interface to show the error
|
| 612 |
+
with gr.Blocks() as error_iface:
|
| 613 |
+
gr.Markdown(
|
| 614 |
+
"""
|
| 615 |
+
# ⚠️ Model Loading Error
|
| 616 |
+
|
| 617 |
+
The emotion detection model failed to load. This could be due to:
|
| 618 |
+
|
| 619 |
+
1. Network connectivity issues
|
| 620 |
+
2. Model compatibility problems
|
| 621 |
+
3. Missing dependencies
|
| 622 |
+
|
| 623 |
+
Please check the logs for more details.
|
| 624 |
+
"""
|
| 625 |
+
)
|
| 626 |
+
|
| 627 |
+
error_iface.launch(
|
| 628 |
+
share=False,
|
| 629 |
+
show_error=True,
|
| 630 |
+
server_name="0.0.0.0",
|
| 631 |
+
server_port=7860
|
| 632 |
+
)
|