-
Notifications
You must be signed in to change notification settings - Fork 1
/
auxiliary.py
218 lines (188 loc) · 7.06 KB
/
auxiliary.py
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
# ############################################################################
# Color Fingerprint: aux.py
# Auxiliary functions definitions
# ############################################################################
import os
import cv2
import glob
import shutil
import numpy as np
from itertools import cycle
from itertools import groupby
from operator import itemgetter
from matplotlib import pyplot as plt
from joblib import Parallel, delayed
from joblib import dump, load
from sklearn.cluster import KMeans, MiniBatchKMeans
import matplotlib.font_manager
from IPython.core.display import HTML
def readAndProcessImg(path):
# Read image and convert from BGR to RGB
bgr = cv2.imread(path)
frameBGR = cv2.cvtColor(bgr, cv2.COLOR_BGR2RGB)
shp = frameBGR.shape
return (frameBGR, shp)
def rgb_to_hex(rgb):
return '#'+'%02x%02x%02x' % tuple([int(i*255) for i in rgb])
def hex_to_rgb(value):
value = value.lstrip('#')
lv = len(value)
return tuple(int(value[i:i + lv // 3], 16) for i in range(0, lv, lv // 3))
def dominantImage(
img, domColNum, clustersNum, maxIter=100
):
(frame, shp) = img
flatFrame = frame.reshape([1, shp[0] * shp[1], 3])[0]
kMeansCall = KMeans(n_clusters=clustersNum, max_iter=maxIter)
kmeans = kMeansCall.fit(flatFrame)
# Take the color palette and add it to the clusters container
# if (domColNum==1 and clustersNum==1):
# palette = kmeans.cluster_centers_
# palettePad = [palette]
# else:
frequencies = {
key: len(list(group)) for key, group in groupby(sorted(kmeans.labels_))
}
dominant = dict(
sorted(frequencies.items(), key = itemgetter(1), reverse = True
)[:domColNum])
dominantKeys = list(dominant.keys())
palette = [kmeans.cluster_centers_[j] for j in dominantKeys]
myiter = cycle(palette)
palettePad = [next(myiter) for _ in range(domColNum)]
colors = [rescaleColor(color) for color in palettePad]
return colors
def dominatImageWrapper(
ix, filepaths, clustersArray,
domColNum, clustersNum,
maxIter=100, VERBOSE=True
):
if VERBOSE:
filesNum = len(filepaths)
print('\t* Frame {}/{}'.format(ix+1, filesNum), end='\r')
img = readAndProcessImg(filepaths[ix])
cols = dominantImage(img, domColNum, clustersNum, maxIter=maxIter)
clustersArray[ix] = cols
return clustersArray
def parallelDominantImage(
filepaths, domColNum, clustersNum,
maxIter=100, VERBOSE=True, jobs=4
):
mmap = 'memmap.job'
clustersArray = np.memmap(
mmap, dtype=np.double,
shape=(len(filepaths), domColNum, 3), mode='w+'
)
# clustersArray = np.empty((len(filepaths), domColNum, 3))
Parallel(n_jobs=jobs)(
delayed(dominatImageWrapper)(
ix, filepaths, clustersArray,
domColNum, clustersNum,
maxIter=maxIter, VERBOSE=VERBOSE
) for ix in range(0, len(filepaths))
)
os.remove(mmap)
return clustersArray
def calculateDominantColors(
filepaths, domColNum, clustersNum, maxIter=100, VERBOSE=True
):
# Create an empty array with dimensions: (framesNumber, dominantColors, 3)
clusters = np.empty((len(filepaths), domColNum, 3))
# Initialize a KMean instance for clustering
kMeansCall = MiniBatchKMeans(n_clusters=clustersNum, max_iter=maxIter)
# Iterate through the files
for (i, path) in enumerate(filepaths):
if VERBOSE:
filesNum = len(filepaths)
print('\t* Frame {}/{}'.format(i+1, filesNum), end='\r')
# Read image and reshape to an RGB vector of vectors
(frame, shp) = readAndProcessImg(path)
flatFrame = frame.reshape([1, shp[0] * shp[1], 3])[0]
# Cluster the RGB entries for color dominance detection
kmeans = kMeansCall.fit(flatFrame)
# Take the color palette and add it to the clusters container
if (domColNum==1 and clustersNum==1):
palette = kmeans.cluster_centers_
clusters[i] = [rescaleColor(color) for color in palette]
else:
frequencies = {
key: len(list(group)) for key, group in groupby(sorted(kmeans.labels_))
}
dominant = dict(
sorted(frequencies.items(), key = itemgetter(1), reverse = True
)[:domColNum])
dominantKeys = list(dominant.keys())
palette = [kmeans.cluster_centers_[j] for j in dominantKeys]
myiter = cycle(palette)
pallettePad = [next(myiter) for _ in range(domColNum)]
clusters[i] = [rescaleColor(color) for color in pallettePad]
if VERBOSE:
print("\33[2K", end='\r')
return clusters
def getFilepaths(path, namesHead, ext='.png'):
filepaths = sorted(glob.glob(path + '/' + namesHead + '*' + ext))
return filepaths
def rescaleColor(colorEightBit):
colors = list(colorEightBit)
return [i / 255 for i in colors]
def exportFingerprintPlot(
path, filename, clusters, dims=(10, 10),
dpi=500, aspect=1, movieTitle='', **kwargs
):
fig, ax = plt.subplots(figsize=dims)
ax.axis('off')
ax.axhspan(
kwargs['hspan'][0], kwargs['hspan'][1],
facecolor=kwargs['facecolor'], transform=ax.transAxes
)
# ax.axvspan(
# kwargs['vspan'][0], kwargs['vspan'][1],
# facecolor=kwargs['facecolor'], transform=ax.transAxes
# )
plt.imshow(list(map(list, zip(*clusters))), aspect=aspect)
plt.text(
kwargs['textpos'][0], kwargs['textpos'][1], movieTitle,
horizontalalignment=kwargs['halign'],
verticalalignment=kwargs['valign'],
fontfamily=kwargs['fontfamily'],
fontsize=kwargs['fontsize'],
color=kwargs['color'],
transform=ax.transAxes
)
plt.savefig(
path + '/' + filename, dpi=dpi,
bbox_inches='tight', pad_inches=0,
facecolor=kwargs['facecolor']
)
plt.close()
def fontFromOS(systemName):
# Select font according to OS
if systemName == 'Darwin':
FONT = 'Avenir'
elif systemName == 'Linux':
FONT = 'Liberation Sans Narrow'
else:
FONT = 'Arial'
return FONT
def defineFont(fontName, color='black', size=100, alpha=.06):
fontDict = {
'fontname': fontName,
'color': color, 'weight': 'light',
'size': size, 'alpha': alpha
}
return fontDict
def isNotebook():
try:
shell = get_ipython().__class__.__name__
if shell == 'ZMQInteractiveShell':
return True # Jupyter notebook or qtconsole
elif shell == 'TerminalInteractiveShell':
return False # Terminal running IPython
else:
return False # Other type (?)
except NameError:
return False # Probably standard Python interpreter
# def make_html(fontname):
# return "<p>{font}: <span style='font-family:{font}; font-size: 24px;'>{font}</p>".format(font=fontname)
# code = "\n".join([make_html(font) for font in sorted(set([f.name for f in matplotlib.font_manager.fontManager.ttflist]))])
# HTML("<div style='column-count: 2;'>{}</div>".format(code))