File size: 6,471 Bytes
f7e620e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Export Utilities for EmotionMirror Application

This module provides functions for exporting analysis data to various formats,
including CSV, JSON, and visual reports.
"""

import os
import csv
import json
import logging
from typing import Dict, List, Any, Optional
from datetime import datetime
import pandas as pd
import base64
from io import BytesIO, StringIO

# Configure logging
logger = logging.getLogger(__name__)

def export_to_json(data: Dict[str, Any], filepath: Optional[str] = None) -> str:
    """
    Export data to JSON format.
    
    Args:
        data: Data to export
        filepath: Optional path to save the JSON file
        
    Returns:
        JSON string if filepath is None, else filepath where saved
    """
    try:
        json_str = json.dumps(data, indent=4)
        
        if filepath:
            with open(filepath, 'w', encoding='utf-8') as f:
                f.write(json_str)
            logger.info(f"Data exported to JSON file: {filepath}")
            return filepath
        
        return json_str
    
    except Exception as e:
        logger.error(f"Error exporting data to JSON: {e}")
        raise

def export_to_csv(data: Dict[str, Any], filepath: Optional[str] = None) -> str:
    """
    Export analysis data to CSV format.
    
    Args:
        data: Analysis data to export
        filepath: Optional path to save the CSV file
        
    Returns:
        CSV string if filepath is None, else filepath where saved
    """
    try:
        # Flatten the data structure for CSV format
        rows = []
        
        for analysis in data.get('analyses', []):
            base_row = {
                'analysis_id': analysis.get('id'),
                'session_id': analysis.get('session_id'),
                'timestamp': analysis.get('timestamp'),
                'image_path': analysis.get('image_path'),
                'face_count': analysis.get('face_count')
            }
            
            # Add tags if present
            if analysis.get('tags'):
                base_row['tags'] = ','.join(analysis['tags'])
            
            # Add face-specific data
            for i, face in enumerate(analysis.get('faces', [])):
                row = base_row.copy()
                row['face_index'] = i
                row['emotion'] = face.get('emotion')
                row['confidence'] = face.get('confidence')
                
                # Add feature values
                for feature, value in face.get('features', {}).items():
                    row[f'feature_{feature}'] = value
                
                # Add emotion values
                for emotion, value in face.get('emotions', {}).items():
                    row[f'emotion_{emotion}'] = value
                
                rows.append(row)
        
        if not rows:
            logger.warning("No data to export to CSV")
            return "" if filepath is None else filepath
        
        # Create a DataFrame and export to CSV
        df = pd.DataFrame(rows)
        
        if filepath:
            df.to_csv(filepath, index=False)
            logger.info(f"Data exported to CSV file: {filepath}")
            return filepath
        
        # Return CSV string if no filepath provided
        csv_buffer = StringIO()
        df.to_csv(csv_buffer, index=False)
        return csv_buffer.getvalue()
    
    except Exception as e:
        logger.error(f"Error exporting data to CSV: {e}")
        raise

def get_download_link(content: str, filename: str, mimetype: str) -> str:
    """
    Generate a download link for the content.
    
    Args:
        content: String content to download
        filename: Name for the downloaded file
        mimetype: MIME type of the file
        
    Returns:
        HTML download link
    """
    try:
        b64 = base64.b64encode(content.encode()).decode()
        href = f'data:{mimetype};base64,{b64}'
        return f'<a href="{href}" download="{filename}" class="download-button">Download {filename}</a>'
    
    except Exception as e:
        logger.error(f"Error creating download link: {e}")
        return ""

def generate_emotion_summary(data: Dict[str, Any]) -> Dict[str, Any]:
    """
    Generate a summary of emotion data across multiple analyses.
    
    Args:
        data: Dictionary of analysis data
        
    Returns:
        Dictionary with summarized emotion data
    """
    try:
        # Initialize counters
        emotion_counts = {}
        total_faces = 0
        emotion_confidence = {}
        
        # Process each analysis
        for analysis in data.get('analyses', []):
            for face in analysis.get('faces', []):
                # Count primary emotions
                emotion = face.get('emotion')
                if emotion:
                    emotion_counts[emotion] = emotion_counts.get(emotion, 0) + 1
                    
                    # Track confidence values
                    if emotion not in emotion_confidence:
                        emotion_confidence[emotion] = []
                    emotion_confidence[emotion].append(face.get('confidence', 0))
                    
                    total_faces += 1
        
        # Calculate statistics
        if total_faces > 0:
            # Calculate percentages
            emotion_percentages = {
                emotion: (count / total_faces) * 100 
                for emotion, count in emotion_counts.items()
            }
            
            # Calculate average confidence per emotion
            avg_confidence = {
                emotion: sum(values) / len(values) 
                for emotion, values in emotion_confidence.items() if values
            }
            
            return {
                'total_analyses': len(data.get('analyses', [])),
                'total_faces': total_faces,
                'emotion_counts': emotion_counts,
                'emotion_percentages': emotion_percentages,
                'avg_confidence': avg_confidence
            }
        
        return {
            'total_analyses': len(data.get('analyses', [])),
            'total_faces': 0,
            'emotion_counts': {},
            'emotion_percentages': {},
            'avg_confidence': {}
        }
    
    except Exception as e:
        logger.error(f"Error generating emotion summary: {e}")
        return {
            'error': str(e),
            'total_analyses': 0,
            'total_faces': 0
        }