File size: 3,516 Bytes
b0a48de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import faiss
from sklearn.metrics import pairwise_distances_argmin_min
import random
import numpy as np
from utils import *

def kmeans(number_of_clusters, features):
    # Cluster the frames using K-Means

    # K-means from sklearn
    #kmeans = KMeans(n_clusters=number_of_clusters, random_state=0).fit(features)

    # K-means from faiss
    ncentroids = number_of_clusters
    niter = 10
    verbose = True
    x = features
    
    # Take the first dimension of the first element of the list
    dimension = x[0].shape[0]

    kmeans = faiss.Kmeans(dimension, ncentroids, niter=niter, verbose=verbose)
    kmeans.train(x)

    #closest, _ = pairwise_distances_argmin_min(kmeans.cluster_centers_, features)
    closest, _ = pairwise_distances_argmin_min(kmeans.centroids, x)
    
    closest_clips_frames = []

    for i in sorted(closest):
        for idx in range(i*8, (i+1)*8):
            closest_clips_frames.append(idx)

    return closest_clips_frames

def tt01(features, threshold):

    i = 0
    clips = []

    # compare the sum of squared difference between clips i and j
    for j in range(1, len(features)):
        if sum_of_squared_difference(features[i], features[j]) > threshold:
            clip = []

            # add frames from clip i to j-1 to the clip list
            for b in range(i*8, j*8):
                clip.append(b)

            # randomly select 15% of the frames from the clip list
            random_num = round(len(clip)*0.15)

            # sort the frames in the clip list to ensure the order of the frames
            random_Frames = sorted(random.sample(clip, random_num))
            i = j
            clips.extend(random_Frames)

    # add the last clip to the clip list
    clip = []
    if i==j:
        for c in range(j*8, j*8+8):
            clip.append(c)
            random_num = round(len(clip)*0.15)
            random_Frames = sorted(random.sample(clip, random_num))
        #print("i == j")

    else: # (i<j)
        for c in range(i*8, (j+1)*8):
            clip.append(c)
            random_num = round(len(clip)*0.15)
            random_Frames = sorted(random.sample(clip, random_num))
        #print(f"{i} with {j}")

    clips.extend(random_Frames)
            
    return clips

def tt02(features, threshold):

    i = 0
    previous = i
    clips = []

    #compare the sum of squared difference between clips j and previous
    for j in range(1, len(features)):
        if sum_of_squared_difference(features[previous], features[j]) > threshold:
            clip = []

            # add frames from clip i to j-1 to the clip list
            for b in range(i*8, j*8):
                clip.append(b)

            # randomly select 15% of the frames from the clip list
            random_num = round(len(clip)*0.15)
            # sort the frames in the clip list to ensure the order of the frames
            random_Frames = sorted(random.sample(clip, random_num))
            i = j
            clips.extend(random_Frames)

        previous = j

    # add the last clip to the clip list
    clip = []
    if i==j:
        for c in range(j*8, j*8+8):
            clip.append(c)
            random_num = round(len(clip)*0.15)
            random_Frames = sorted(random.sample(clip, random_num))

    else: # (i<j)
        for c in range(i*8, (j+1)*8):
            clip.append(c)
            random_num = round(len(clip)*0.15)
            random_Frames = sorted(random.sample(clip, random_num))

    clips.extend(random_Frames)
        
    return clips