File size: 2,670 Bytes
24615d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
import matplotlib.pyplot as plt

def highlight_score_plot(basename: str, model_scores: dict, num_frames: int, directory=None):
    fig = plt.gcf()
    fig.set_size_inches(20, 10)
    n_clips = len(list(model_scores.values())[0])
    x = np.arange(n_clips)
    x_labels = [f'{frame_id}~{frame_id + num_frames - 1}' for frame_id in range(n_clips)]

    for model, scores in model_scores.items():
        plt.plot(x, scores, 'o-', linewidth=4.0, markersize=10.0, label = model)
    plt.legend()
    plt.title(basename)
    plt.xticks(x, x_labels, rotation=90)
    plt.xlabel('Duration')
    plt.ylabel('Score')
    if directory!= None:
        plt.savefig(f'{directory}/highlight_score.jpg', dpi=100)
    else:
        plt.show()
    plt.close()

def plot_loss(train_loss, val_loss, exp_name: str, directory=None):
    fig = plt.gcf()
    fig.set_size_inches(20, 10)

    x = np.arange(len(train_loss))
    x_labels = x
    
    plt.plot(x, train_loss, 'o-', linewidth=4.0, markersize=10.0, label = f'{exp_name}_train')
    plt.plot(x, val_loss, 'o-', linewidth=4.0, markersize=10.0, label = f'{exp_name}_val')

    plt.legend()
    plt.title(f'{exp_name} training/validation loss')
    plt.xticks(x, x_labels)
    plt.xlabel('Epochs')
    plt.ylabel('Loss')
    if directory!= None:
        plt.savefig(f'{directory}/loss.jpg', dpi=100)
    else:
        plt.show()
    plt.close()

def plot_scores(scores, title=None, directory=None):
    import matplotlib.pyplot as plt
    import numpy as np
    fig = plt.gcf()
    fig.set_size_inches(20, 10)

    x = np.arange(len(scores))
    x_labels = [f'{x[i]}~{x[i]+2}' for i in range(len(x))]
    
    plt.plot(x, scores, 'o-', linewidth=4.0, markersize=10.0)

    plt.grid()
    if not title:
        plt.title(f'scores')
    else:
        plt.title(title)
    plt.xticks(x, x_labels, rotation=90)
    plt.xlabel('time')
    plt.ylabel('scores')
    if directory!= None:
        if not title:
            plt.savefig(f'{directory}/scores.jpg', dpi=100)
        else:
            plt.savefig(f'{directory}/{title}.jpg', dpi=100)
    else:
        plt.show()
    plt.close()

def extract_clips(video_path, intervals, path='../test.mp4'):
    from moviepy.editor import VideoFileClip, concatenate_videoclips
    video = VideoFileClip(video_path)
    clips = [video.subclip(start_sec, end_sec) if end_sec < video.duration else video.subclip(start_sec, video.duration) for start_sec, end_sec in intervals]
    highlight = concatenate_videoclips(clips)
    highlight = highlight.set_audio(highlight.audio)
    highlight.write_videofile(path,audio=True, audio_codec='aac', fps=30, logger=None)
    return highlight