File size: 15,964 Bytes
11e16fe
859cec7
f037e1e
88ae781
caf2618
11e16fe
 
 
2ab7633
2128c11
2e27102
ab7a94a
11e16fe
 
 
71c6d95
b6d52dc
0b7634e
11e16fe
 
bd57c84
11e16fe
 
 
 
 
 
 
 
79bd6b6
11e16fe
 
79bd6b6
 
 
 
11e16fe
79bd6b6
 
 
 
 
 
 
 
 
 
 
 
 
11e16fe
79bd6b6
 
11e16fe
79bd6b6
11e16fe
79bd6b6
 
 
 
 
 
 
 
 
 
 
 
11e16fe
79bd6b6
11e16fe
 
 
 
c78126c
 
11e16fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86a04fb
d76a2e5
 
 
 
 
 
 
86a04fb
 
 
 
 
 
 
 
 
 
 
 
 
 
77142e3
 
 
 
 
 
 
 
 
 
86a04fb
77142e3
86a04fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a337f54
86a04fb
 
 
a337f54
86a04fb
77142e3
 
 
 
86a04fb
 
 
 
d76a2e5
86a04fb
 
d76a2e5
 
77142e3
d76a2e5
 
 
 
 
 
 
 
11e16fe
bd57c84
11e16fe
 
 
 
c78126c
11e16fe
 
 
 
 
 
 
 
 
 
 
 
caf2618
11e16fe
bd57c84
11e16fe
 
 
 
 
 
 
 
 
5fefb86
11e16fe
5fefb86
 
 
 
 
11e16fe
 
 
 
 
 
 
 
 
 
 
 
 
 
bd57c84
11e16fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
abc304c
4d4ae71
9130830
b68e4d5
392b31b
f037e1e
 
 
 
 
 
 
 
 
2128c11
3fab580
26223d9
f037e1e
ed36df7
f037e1e
2128c11
ed36df7
f037e1e
26a4c32
92bbbb6
f037e1e
2128c11
f037e1e
 
2128c11
f037e1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9130830
 
 
b68e4d5
f037e1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
859cec7
 
 
 
99614c2
 
 
859cec7
3ad6751
 
99614c2
 
 
859cec7
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
import matplotlib.colors as mcolors
from matplotlib.colors import LinearSegmentedColormap
import seaborn as sns
import numpy as np
import pandas as pd
import cv2
from moviepy.editor import VideoFileClip, AudioFileClip, CompositeVideoClip, ImageClip, VideoClip, concatenate_videoclips
from moviepy.video.fx.all import resize
from PIL import Image, ImageDraw, ImageFont
from matplotlib.patches import Rectangle
from utils import seconds_to_timecode
from anomaly_detection import determine_anomalies
from scipy import interpolate
import gradio as gr
import os

def plot_mse(df, mse_values, title, color='navy', time_threshold=3, anomaly_threshold=4):
    plt.figure(figsize=(16, 8), dpi=300)
    fig, ax = plt.subplots(figsize=(16, 8))

    if 'Seconds' not in df.columns:
        df['Seconds'] = df['Timecode'].apply(
            lambda x: sum(float(t) * 60 ** i for i, t in enumerate(reversed(x.split(':')))))

    # Ensure df and mse_values have the same length and remove NaN values
    min_length = min(len(df), len(mse_values))
    df = df.iloc[:min_length].copy()
    mse_values = mse_values[:min_length]

    # Remove NaN values and create a mask for valid data
    valid_mask = ~np.isnan(mse_values)
    df = df[valid_mask]
    mse_values = mse_values[valid_mask]

    # Function to identify continuous segments
    def get_continuous_segments(seconds, values, max_gap=1):
        segments = []
        current_segment = []
        for i, (sec, val) in enumerate(zip(seconds, values)):
            if not current_segment or (sec - current_segment[-1][0] <= max_gap):
                current_segment.append((sec, val))
            else:
                segments.append(current_segment)
                current_segment = [(sec, val)]
        if current_segment:
            segments.append(current_segment)
        return segments

    # Get continuous segments
    segments = get_continuous_segments(df['Seconds'], mse_values)

    # Plot each segment separately
    for segment in segments:
        segment_seconds, segment_mse = zip(*segment)
        ax.scatter(segment_seconds, segment_mse, color=color, alpha=0.3, s=5)
        
        # Calculate and plot rolling mean and std for this segment
        if len(segment) > 1:  # Only if there's more than one point in the segment
            segment_df = pd.DataFrame({'Seconds': segment_seconds, 'MSE': segment_mse})
            segment_df = segment_df.sort_values('Seconds')
            mean = segment_df['MSE'].rolling(window=min(10, len(segment)), min_periods=1, center=True).mean()
            std = segment_df['MSE'].rolling(window=min(10, len(segment)), min_periods=1, center=True).std()
            
            ax.plot(segment_df['Seconds'], mean, color=color, linewidth=0.5)
            ax.fill_between(segment_df['Seconds'], mean - std, mean + std, color=color, alpha=0.1)

    # Rest of the function remains the same
    median = np.median(mse_values)
    ax.axhline(y=median, color='black', linestyle='--', label='Median Baseline')

    threshold = np.mean(mse_values) + anomaly_threshold * np.std(mse_values)
    ax.axhline(y=threshold, color='red', linestyle='--', label=f'Anomaly Threshold')
    ax.text(ax.get_xlim()[1], threshold, f'Anomaly Threshold', verticalalignment='center', horizontalalignment='left', color='red')

    anomalies = determine_anomalies(mse_values, anomaly_threshold)
    anomaly_frames = df['Frame'].iloc[anomalies].tolist()

    ax.scatter(df['Seconds'].iloc[anomalies], mse_values[anomalies], color='red', s=20, zorder=5)

    anomaly_data = list(zip(df['Timecode'].iloc[anomalies],
                            df['Seconds'].iloc[anomalies],
                            mse_values[anomalies]))
    anomaly_data.sort(key=lambda x: x[1])

    grouped_anomalies = []
    current_group = []
    for timecode, sec, mse in anomaly_data:
        if not current_group or sec - current_group[-1][1] <= time_threshold:
            current_group.append((timecode, sec, mse))
        else:
            grouped_anomalies.append(current_group)
            current_group = [(timecode, sec, mse)]
    if current_group:
        grouped_anomalies.append(current_group)

    for group in grouped_anomalies:
        start_sec = group[0][1]
        end_sec = group[-1][1]
        rect = Rectangle((start_sec, ax.get_ylim()[0]), end_sec - start_sec, ax.get_ylim()[1] - ax.get_ylim()[0],
                         facecolor='red', alpha=0.2, zorder=1)
        ax.add_patch(rect)

    for group in grouped_anomalies:
        highest_mse_anomaly = max(group, key=lambda x: x[2])
        timecode, sec, mse = highest_mse_anomaly
        ax.annotate(timecode, (sec, mse), textcoords="offset points", xytext=(0, 10),
                    ha='center', fontsize=6, color='red')

    max_seconds = df['Seconds'].max()
    num_ticks = 100
    tick_locations = np.linspace(0, max_seconds, num_ticks)
    tick_labels = [seconds_to_timecode(int(s)) for s in tick_locations]

    ax.set_xticks(tick_locations)
    ax.set_xticklabels(tick_labels, rotation=90, ha='center', fontsize=6)

    ax.set_xlabel('Timecode')
    ax.set_ylabel('Mean Squared Error')
    ax.set_title(title)

    ax.grid(True, linestyle='--', alpha=0.7)
    ax.legend()
    plt.tight_layout()
    plt.close()
    return fig, anomaly_frames

def plot_combined_mse(df, mse_embeddings, mse_posture, mse_voice, title, anomaly_threshold=4, time_threshold=3):
    plt.figure(figsize=(16, 8), dpi=300)
    fig, ax = plt.subplots(figsize=(16, 8))

    if 'Seconds' not in df.columns:
        df['Seconds'] = df['Timecode'].apply(
            lambda x: sum(float(t) * 60 ** i for i, t in enumerate(reversed(x.split(':')))))

    # Function to identify continuous segments
    def get_continuous_segments(seconds, values, max_gap=1):
        segments = []
        current_segment = []
        for i, (sec, val) in enumerate(zip(seconds, values)):
            if not current_segment or (sec - current_segment[-1][0] <= max_gap):
                current_segment.append((sec, val))
            else:
                segments.append(current_segment)
                current_segment = [(sec, val)]
        if current_segment:
            segments.append(current_segment)
        return segments

    # Scale all MSE values to the same range (0 to 1)
    def scale_mse(mse_values):
        min_val = np.min(mse_values)
        max_val = np.max(mse_values)
        return (mse_values - min_val) / (max_val - min_val)

    mse_embeddings_scaled = scale_mse(mse_embeddings)
    mse_posture_scaled = scale_mse(mse_posture)
    mse_voice_scaled = scale_mse(mse_voice)

    # Plot each data series
    for mse_values, color, label in zip([mse_embeddings_scaled, mse_posture_scaled, mse_voice_scaled], 
                                        ['navy', 'purple', 'green'], 
                                        ['Facial Features', 'Body Posture', 'Voice']):
        segments = get_continuous_segments(df['Seconds'], mse_values)
        
        for segment in segments:
            segment_seconds, segment_mse = zip(*segment)
            ax.scatter(segment_seconds, segment_mse, color=color, alpha=0.3, s=5, label=label if segment == segments[0] else "")
            
            if len(segment) > 1:
                segment_df = pd.DataFrame({'Seconds': segment_seconds, 'MSE': segment_mse})
                segment_df = segment_df.sort_values('Seconds')
                mean = segment_df['MSE'].rolling(window=min(10, len(segment)), min_periods=1, center=True).mean()
                std = segment_df['MSE'].rolling(window=min(10, len(segment)), min_periods=1, center=True).std()
                
                ax.plot(segment_df['Seconds'], mean, color=color, linewidth=0.5)
                ax.fill_between(segment_df['Seconds'], mean - std, mean + std, color=color, alpha=0.1)

        # Plot median baseline for each series
        median = np.median(mse_values)
        ax.axhline(y=median, color=color, linestyle=':', alpha=0.5, label=f'{label} Baseline Median')

        # Plot threshold for each series
        threshold = np.mean(mse_values) + anomaly_threshold * np.std(mse_values)
        ax.axhline(y=threshold, color=color, linestyle='--', alpha=1, label=f'{label} Anomaly Threshold')

        # Plot anomalies in red
        anomalies = mse_values > threshold
        ax.scatter(df['Seconds'][anomalies], mse_values[anomalies], color='red', s=20, zorder=5)

    max_seconds = df['Seconds'].max()
    num_ticks = 100
    tick_locations = np.linspace(0, max_seconds, num_ticks)
    tick_labels = [seconds_to_timecode(int(s)) for s in tick_locations]

    ax.set_xticks(tick_locations)
    ax.set_xticklabels(tick_labels, rotation=90, ha='center', fontsize=6)

    ax.set_xlabel('Timecode')
    ax.set_ylabel('Scaled Mean Squared Error')
    ax.set_title(title)

    ax.grid(True, linestyle='--', alpha=0.7)
    ax.legend()
    plt.tight_layout()
    plt.close()
    return fig

def plot_mse_histogram(mse_values, title, anomaly_threshold, color='blue'):
    plt.figure(figsize=(16, 3), dpi=300)
    fig, ax = plt.subplots(figsize=(16, 3))

    ax.hist(mse_values, bins=100, edgecolor='black', color=color, alpha=0.7)
    ax.set_xlabel('Mean Squared Error')
    ax.set_ylabel('Number of Frames')
    ax.set_title(title)

    mean = np.mean(mse_values)
    std = np.std(mse_values)
    threshold = mean + anomaly_threshold * std

    ax.axvline(x=threshold, color='red', linestyle='--', linewidth=2)

    plt.tight_layout()
    plt.close()
    return fig


def plot_mse_heatmap(mse_values, title, df):
    plt.figure(figsize=(20, 3), dpi=300)
    fig, ax = plt.subplots(figsize=(20, 3))

    # Reshape MSE values to 2D array for heatmap
    mse_2d = mse_values.reshape(1, -1)

    # Create heatmap
    sns.heatmap(mse_2d, cmap='YlOrRd', cbar=False, ax=ax)

    # Set x-axis ticks to timecodes
    num_ticks = min(60, len(mse_values))
    tick_locations = np.linspace(0, len(mse_values) - 1, num_ticks).astype(int)
    
    # Ensure tick_locations are within bounds
    tick_locations = tick_locations[tick_locations < len(df)]
    
    tick_labels = [df['Timecode'].iloc[i] if i < len(df) else '' for i in tick_locations]

    ax.set_xticks(tick_locations)
    ax.set_xticklabels(tick_labels, rotation=90, ha='center', va='top')

    ax.set_title(title)

    # Remove y-axis labels
    ax.set_yticks([])

    plt.tight_layout()
    plt.close()
    return fig

def plot_posture(df, posture_scores, color='blue', anomaly_threshold=3):
    plt.figure(figsize=(16, 8), dpi=300)
    fig, ax = plt.subplots(figsize=(16, 8))

    df['Seconds'] = df['Timecode'].apply(
        lambda x: sum(float(t) * 60 ** i for i, t in enumerate(reversed(x.split(':')))))

    posture_data = [(frame, score) for frame, score in posture_scores.items() if score is not None]
    posture_frames, posture_scores = zip(*posture_data)

    # Create a new dataframe for posture data
    posture_df = pd.DataFrame({'Frame': posture_frames, 'Score': posture_scores})


    posture_df = posture_df.merge(df[['Frame', 'Seconds']], on='Frame', how='inner')

    ax.scatter(posture_df['Seconds'], posture_df['Score'], color=color, alpha=0.3, s=5)
    mean = posture_df['Score'].rolling(window=10).mean()
    ax.plot(posture_df['Seconds'], mean, color=color, linewidth=0.5)

    ax.set_xlabel('Timecode')
    ax.set_ylabel('Posture Score')
    ax.set_title("Body Posture Over Time")

    ax.grid(True, linestyle='--', alpha=0.7)

    max_seconds = df['Seconds'].max()
    num_ticks = 80
    tick_locations = np.linspace(0, max_seconds, num_ticks)
    tick_labels = [seconds_to_timecode(int(s)) for s in tick_locations]

    ax.set_xticks(tick_locations)
    ax.set_xticklabels(tick_labels, rotation=90, ha='center', fontsize=6)

    plt.tight_layout()
    plt.close()
    return fig

def create_heatmap(t, mse_embeddings, mse_posture, mse_voice, video_fps, total_frames, video_width):
    frame_count = int(t * video_fps)
    
    # Normalize MSE values
    mse_embeddings_norm = (mse_embeddings - np.min(mse_embeddings)) / (np.max(mse_embeddings) - np.min(mse_embeddings))
    mse_posture_norm = (mse_posture - np.min(mse_posture)) / (np.max(mse_posture) - np.min(mse_posture))
    mse_voice_norm = (mse_voice - np.min(mse_voice)) / (np.max(mse_voice) - np.min(mse_voice))
    
    combined_mse = np.zeros((3, total_frames))
    combined_mse[0] = mse_embeddings_norm
    combined_mse[1] = mse_posture_norm
    combined_mse[2] = mse_voice_norm

    fig, ax = plt.subplots(figsize=(video_width / 250, 0.6))
    ax.imshow(combined_mse, aspect='auto', cmap='Reds', vmin=0, vmax=1, extent=[0, total_frames, 0, 3])
    ax.set_yticks([0.5, 1.5, 2.5])
    ax.set_yticklabels(['Voice', 'Posture', 'Face'], fontsize=7) 
    ax.set_xticks([])

    ax.axvline(x=frame_count, color='black', linewidth=3)

    plt.tight_layout(pad=0.5)
    
    canvas = FigureCanvas(fig)
    canvas.draw()
    heatmap_img = np.frombuffer(canvas.tostring_rgb(), dtype='uint8')
    heatmap_img = heatmap_img.reshape(canvas.get_width_height()[::-1] + (3,))
    plt.close(fig)
    return heatmap_img

def create_video_with_heatmap(video_path, df, mse_embeddings, mse_posture, mse_voice, output_folder, desired_fps, largest_cluster):
    print(f"Creating heatmap video. Output folder: {output_folder}")
    
    os.makedirs(output_folder, exist_ok=True)
    
    output_filename = os.path.basename(video_path).rsplit('.', 1)[0] + '_heatmap.mp4'
    heatmap_video_path = os.path.join(output_folder, output_filename)
    
    print(f"Heatmap video will be saved at: {heatmap_video_path}")

    # Load the original video
    video = VideoFileClip(video_path)
    
    # Get video properties
    width, height = video.w, video.h
    total_frames = int(video.duration * video.fps)
    
    # Ensure all MSE arrays have the same length as total_frames
    mse_embeddings = np.interp(np.linspace(0, len(mse_embeddings) - 1, total_frames), 
                               np.arange(len(mse_embeddings)), mse_embeddings)
    mse_posture = np.interp(np.linspace(0, len(mse_posture) - 1, total_frames), 
                            np.arange(len(mse_posture)), mse_posture)
    mse_voice = np.interp(np.linspace(0, len(mse_voice) - 1, total_frames), 
                          np.arange(len(mse_voice)), mse_voice)
    
    def combine_video_and_heatmap(t):
        video_frame = video.get_frame(t)
        heatmap_frame = create_heatmap(t, mse_embeddings, mse_posture, mse_voice, video.fps, total_frames, width)
        heatmap_frame_resized = cv2.resize(heatmap_frame, (width, heatmap_frame.shape[0]))
        combined_frame = np.vstack((video_frame, heatmap_frame_resized))
        return combined_frame

    final_clip = VideoClip(combine_video_and_heatmap, duration=video.duration)
    final_clip = final_clip.set_audio(video.audio)

    # Write the final video
    final_clip.write_videofile(heatmap_video_path, codec='libx264', audio_codec='aac', fps=video.fps)
    
    # Close the video clips
    video.close()
    final_clip.close()
    
    if os.path.exists(heatmap_video_path):
        print(f"Heatmap video created at: {heatmap_video_path}")
        print(f"Heatmap video size: {os.path.getsize(heatmap_video_path)} bytes")
        return heatmap_video_path
    else:
        print(f"Failed to create heatmap video at: {heatmap_video_path}")
        return None


# Function to create the correlation heatmap
def plot_correlation_heatmap(mse_embeddings, mse_posture, mse_voice):
    data = np.vstack((mse_embeddings, mse_posture, mse_voice)).T
    df = pd.DataFrame(data, columns=["Facial Features", "Body Posture", "Voice"])
    corr = df.corr()

    plt.figure(figsize=(10, 8), dpi=300)

    heatmap = sns.heatmap(corr, annot=True, cmap='coolwarm', vmin=-1, vmax=1)
    plt.title('Correlation Heatmap of MSEs')
    plt.tight_layout()
    return plt.gcf()