File size: 2,454 Bytes
2eb6f71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
UI Controller Module for EmotionMirror application.

This module contains the UIController class that manages the user interface components
and interactions throughout the application.
"""
import streamlit as st
import logging
from typing import Dict, Any, Optional
import numpy as np
from PIL import Image
import io
import os
from datetime import datetime

from services.image_service import ImageService
from services.database_service import DatabaseService

class UIController:
    """
    Controller for managing the user interface components of the application.
    Handles the setup and display of UI elements throughout the app.
    """
    
    def __init__(
        self, 
        image_service: Optional[ImageService] = None,
        database_service: Optional[DatabaseService] = None,
        config: Optional[Dict[str, Any]] = None
    ):
        """
        Initialize the UI controller with optional service dependencies.
        
        Args:
            image_service: Optional ImageService instance for image processing
            database_service: Optional service for database operations
            config: Optional configuration dictionary with app settings
        """
        self.image_service = image_service or ImageService()
        self.database_service = database_service or DatabaseService()
        self.config = config or {}
        
    def setup_image_upload_interface(self):
        """
        Set up and display the image upload interface with validation feedback.
        
        Returns:
            The uploaded file object if an image was successfully uploaded and validated
        """
        # Create file uploader
        uploaded_file = st.file_uploader(
            "Choose an image...", 
            type=["jpg", "jpeg", "png"], 
            help="Upload a clear image of a face for analysis."
        )
        
        if uploaded_file is not None:
            # Validate image file
            validation_result = self.image_service.validate_image_file(
                uploaded_file,
                check_content=True,
                check_dimensions=True
            )
            
            # If validation failed, show issues
            if not validation_result["valid"]:
                st.error("Image validation failed:")
                for issue in validation_result["issues"]:
                    st.warning(issue)
                return None
        
        return uploaded_file