-
Notifications
You must be signed in to change notification settings - Fork 0
/
similarity.go
159 lines (133 loc) · 3.24 KB
/
similarity.go
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
package skiptro
import (
"fmt"
"time"
"github.com/corona10/goimagehash"
)
type (
Similarity struct {
Matrix [][]bool
StartIndex int
EndIndex int
}
Scene struct {
Start time.Duration
End time.Duration
Similarity Similarity
}
)
// FindLongestScene returns the longest matching scene between two
// TODO Return hash frames of the intro in the scene
func FindLongestScene(source, target []*goimagehash.ImageHash, tolerance int, duration time.Duration) (Scene, error) {
similarityMatrix := make([][]bool, len(source))
for i, h1 := range source {
similarityMatrix[i] = make([]bool, len(target))
for j, h2 := range target {
dist, err := h1.Distance(h2)
if err != nil {
return Scene{}, fmt.Errorf("could not calculate distance: %w", err)
}
if dist < tolerance {
similarityMatrix[i][j] = true
}
}
}
// TODO Maybe have a threshold for seconds of dissimilarity for intros with different scenes (i.e. The Office)
rows := len(similarityMatrix)
cols := len(similarityMatrix[0])
// TODO Configurable
skipTolerance := 3
curSkip := 0
inFlow := false
// Right/top side of diagonal
targetFrame, max := 0, 0
for i := 0; i < rows; i++ {
j := 0
diagSimilar := 0
k := 0
for i+k < rows && j+k < cols {
similar := similarityMatrix[i+k][j+k] ||
clamp(i+k-1, j+k+1, similarityMatrix) ||
clamp(i+k+1, j+k-1, similarityMatrix) ||
clamp(i+k, j+k-1, similarityMatrix) ||
clamp(i+k-1, j+k, similarityMatrix)
if similar {
diagSimilar++
inFlow = true
} else if inFlow {
curSkip++
if curSkip == skipTolerance {
curSkip = 0
inFlow = false
}
if inFlow {
diagSimilar++
}
}
if diagSimilar > max {
max = diagSimilar
targetFrame = j + k
}
k++
}
}
// Left/bottom side of diagonal
for j := 1; j < cols; j++ {
i := 0
diagSimilar := 0
k := 0
for i+k < rows && j+k < cols {
similar := similarityMatrix[i+k][j+k] ||
clamp(i+k-1, j+k+1, similarityMatrix) ||
clamp(i+k+1, j+k-1, similarityMatrix) ||
clamp(i+k, j+k-1, similarityMatrix) ||
clamp(i+k-1, j+k, similarityMatrix)
if similar {
diagSimilar++
inFlow = true
} else if inFlow {
curSkip++
if curSkip == skipTolerance {
curSkip = 0
inFlow = false
}
if inFlow {
diagSimilar++
}
}
if diagSimilar > max {
max = diagSimilar
targetFrame = j + k
}
k++
}
}
targetBegin := targetFrame - max
frameDuration := float64(duration.Milliseconds()) / float64(len(similarityMatrix))
start, err := time.ParseDuration(fmt.Sprintf("%ds", int((frameDuration*float64(targetBegin))/1000)))
if err != nil {
return Scene{}, fmt.Errorf("could not parse starting time: %w", err)
}
end, err := time.ParseDuration(fmt.Sprintf("%ds", int((frameDuration*float64(max))/1000)))
if err != nil {
return Scene{}, fmt.Errorf("could not parse starting time: %w", err)
}
return Scene{
Start: start,
End: start + end,
Similarity: Similarity{
Matrix: similarityMatrix,
StartIndex: targetBegin,
EndIndex: targetBegin + max,
},
}, nil
}
func clamp(i, j int, matrix [][]bool) bool {
if i < 0 || i >= len(matrix) {
return false
}
if j < 0 || j >= len(matrix[0]) {
return false
}
return matrix[i][j]
}