Repository navigation
Expand file tree
/
Copy pathdetectScenes.py
More file actions
226 lines (130 loc) · 4.58 KB
/
Copy pathdetectScenes.py
File metadata and controls
226 lines (130 loc) · 4.58 KB
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
import os
import numpy as np
import math
from scipy import spatial
#import BhattacharyyaDistance.bhatta_dist as bd
from dictances import bhattacharyya
import matplotlib as mp
import matplotlib.pyplot as plt
from collections import OrderedDict
def buildMatrix(frames):
matrix = [[0 for x in range(len(frames))] for y in range(len(frames))]
for i in range(len(frames)):
for j in range(len(frames)):
matrix[i][j] = distanceMetric(frames[i],frames[j])# similarity = 1 - distance
if(matrix[i][j] == float('nan')):
print(str(i) + '_' + str(j))
return matrix
#Скорее это Hellinger, но не суть https://www.encyclopediaofmath.org/index.php/Bhattacharyya_distance
def bhatta( hist1, hist2):
# calculate mean of hist1
h1_ = np.average(hist1);
# calculate mean of hist2
h2_ = np.average(hist2);
# calculate score
score = 0;
for i in range(len(hist1)):
score += math.sqrt( hist1[i] * hist2[i] );
# print h1_,h2_,score;
#При сравнении плана с самим собой может получиться отрицательное число под корнем
try:
score = math.sqrt( 1 - ( 1 / math.sqrt(h1_*h2_*len(hist1)*len(hist1)) ) * score );
except ValueError as e:
score = 0
return score;
def distanceMetric(vector1, vector2): #попробуй soft cosine measure
distance = np.linalg.norm(np.array(vector1) - np.array(vector2))
#spatial.distance.cosine(vector1,vector2)
#bhatta(vector1, vector2)
return distance
def costFunction(distanceMatrix, scenesVector):#additive cost function
cost = 0
for i in range(1,len(scenesVector)):
for j in range(scenesVector[i - 1] + 1, scenesVector[i]):
for k in range(scenesVector[i - 1] + 1, scenesVector[i]):
cost += distanceMatrix[j][k]
return cost
def distanceRange(D, start, stop):
result = 0
for i in range(start, stop):
result += D[stop][i] + D[i][stop]
return result
def sumsTable(D, N):
E = [[0 for x in range(N)] for y in range(N)]
E[0][0] = 0
for i in range(0, N):
E[i][i] = 0
for j in range(i + 1, N):
E[i][j] = E[i][j-1] + distanceRange(D, i, j)
E[j][i] = E[i][j]
return E
def getCJ(E, n, k, N, C):
result = 0
argmin = N
if k != 1 :
result = C[n][k - 1]
for i in range(n, N - 1):
iterationValue = E[n - 1][i - 1] + C[i + 1][k - 1]
if result > iterationValue :
result = iterationValue
argmin = i
else:
result = E[n - 1][N - 1]
return result, argmin
def costTable(D, K, N):
C = [[0 for x in range(K + 1)] for y in range(N + 1)]
J = [[0 for x in range(K + 1)] for y in range(N + 1)]
E = sumsTable(D, N)
for i in range(1, K + 1):
for j in range(1, N + 1):
CJ = getCJ(E, j, i, N, C)
C[j][i] = CJ[0]
J[j][i] = CJ[1]
return C, J
def getDivision(J, K):
t = []
t.append(0)
for i in range(1,K + 1):
t.append(J[t[i - 1] + 1][K - i + 1])
return t
def basicSceneDetect(features, shots, K, N, fmatrix):
distanceMatrix = buildMatrix(features)
if(fmatrix != "None"):
try:
plt.pcolormesh(distanceMatrix, cmap='magma', snap=True)
plt.savefig(fmatrix + 'matrix.png', bbox_inches='tight', dpi=200)
plt.close()
except Exception as e:
print(e)
tables = costTable(distanceMatrix, K, N)
division = getDivision(tables[1], K)
divisionByFrames = []
for i in range(1, len(division)):
divisionByFrames.append([shots[division[i - 1]][0], shots[division[i] - 1][1]])
try:
plt.pcolormesh(distanceMatrix, cmap='magma', snap=True)
plt.savefig(fmatrix + 'matrix.png', bbox_inches='tight', dpi=200)
plt.close()
except Exception as e:
print(e)
return (divisionByFrames, division, distanceMatrix)
def basicSceneDetectWInputMatrix(features, shots, K, N, fmatrix, distanceMatrix):
if(fmatrix != None):
try:
plt.pcolormesh(distanceMatrix, cmap='magma', snap=True)
plt.savefig(fmatrix + 'matrix.png', bbox_inches='tight', dpi=200)
plt.close()
except Exception as e:
print(e)
tables = costTable(distanceMatrix, K, N)
division = getDivision(tables[1], K)
divisionByFrames = []
for i in range(1, len(division)):
divisionByFrames.append([shots[division[i - 1]][0], shots[division[i] - 1][1]])
try:
plt.pcolormesh(distanceMatrix, cmap='magma', snap=True)
plt.savefig(fmatrix + 'matrix.png', bbox_inches='tight', dpi=200)
plt.close()
except Exception as e:
print(e)
return (divisionByFrames, division)