forked from hooram/ownphotos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_util.py
487 lines (392 loc) · 15.7 KB
/
api_util.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
from api.models import Photo, Face, Person, AlbumAuto, AlbumDate, AlbumUser, LongRunningJob
import numpy as np
import os
import json
from collections import Counter
from scipy import linalg
from sklearn.decomposition import PCA
import numpy as np
from sklearn import cluster
from sklearn import mixture
from scipy.spatial import distance
from sklearn.preprocessing import StandardScaler
from api.util import compute_bic
from sklearn.cluster import MeanShift, estimate_bandwidth
from django.db.models.functions import TruncMonth
from django.db.models import Sum, Count, Q, Prefetch
from nltk.corpus import stopwords
import random
from datetime import date, timedelta, datetime
from itertools import groupby
from tqdm import tqdm
import seaborn as sns
import pandas as pd
from api.util import logger
def get_current_job():
job_detail = None
running_job = LongRunningJob.objects.filter(
finished=False).order_by('-started_at').first()
if running_job:
job_detail = LongRunningJobSerializer(running_job).data
return job_detail
def shuffle(l):
random.shuffle(l)
return l
def path_to_dict(path):
d = {'title': os.path.basename(path), 'absolute_path': path}
d['children'] = [
path_to_dict(os.path.join(path, x)) for x in os.listdir(path)
if os.path.isdir(os.path.join(path, x))
]
return d
def jump_by_month(start_date, end_date, month_step=1):
current_date = start_date
yield current_date
while current_date < end_date:
carry, new_month = divmod(current_date.month - 1 + month_step, 12)
new_month += 1
current_date = current_date.replace(
year=current_date.year + carry, month=new_month)
yield current_date
def get_location_timeline(user):
qs_photos = Photo.objects.exclude(geolocation_json={}).exclude(
exif_timestamp=None).filter(owner=user).order_by('exif_timestamp')
photos = qs_photos.all()
timestamp_loc = [(p.exif_timestamp,
p.geolocation_json['features'][-1]['text'])
for p in photos]
groups = []
uniquekeys = []
for k, g in groupby(timestamp_loc, lambda x: x[1]):
groups.append(list(g)) # Store group iterator as a list
uniquekeys.append(k)
city_start_end_duration = []
for idx, group in enumerate(groups):
city = group[0][1]
start = group[0][0]
if idx < len(groups) - 1:
end = groups[idx + 1][0][0]
else:
end = group[-1][0]
# end = group[-1][0]
time_in_city = (end - start).total_seconds()
if time_in_city > 0:
city_start_end_duration.append([city, start, end, time_in_city])
locs = list(set([e[0] for e in city_start_end_duration]))
colors = sns.color_palette('Paired', len(locs)).as_hex()
loc2color = dict(zip(locs, colors))
intervals_in_seconds = []
for idx, sted in enumerate(city_start_end_duration):
intervals_in_seconds.append({
'loc':
sted[0],
'start':
sted[1].timestamp(),
'end':
sted[2].timestamp(),
'dur':
sted[2].timestamp() - sted[1].timestamp()
})
data = [{
"data": [d['dur']],
"color": loc2color[d['loc']],
"loc": d['loc'],
'start': d['start'],
'end': d['end']
} for d in intervals_in_seconds]
return data
def get_search_term_examples(user):
pp = Photo.objects.filter(owner=user).exclude(geolocation_json={}).exclude(
exif_timestamp=None).exclude(
captions_json={}).prefetch_related('faces__person')
try:
samples = random.sample(list(pp.all()), 100)
except ValueError:
return [
'for people', 'for places', 'for things', 'for time',
'for file path or file name'
]
search_data = []
for p in samples:
faces = p.faces.all()
terms_loc = [
f['text'] for f in p.geolocation_json['features'][-5:]
if not f['text'].isdigit()
]
terms_time = [str(p.exif_timestamp.year)]
terms_people = [f.person.name.split(' ')[0] for f in faces]
terms_things = p.captions_json['places365'][
'categories'] # + p.captions_json['places365']['attributes']
terms = {
"loc": terms_loc,
"time": terms_time,
"people": terms_people,
"things": terms_things
}
search_data.append(terms)
search_terms = []
for datum in search_data:
term_loc = random.choice(datum['loc'])
search_terms.append(term_loc)
term_time = random.choice(datum['time'])
search_terms.append(term_time)
term_thing = random.choice(datum['things'])
if len(datum['people']) > 0:
term_people = random.choice(datum['people'])
search_terms.append(term_people)
search_term_loc_people = ' '.join(
shuffle([term_loc, term_people]))
if random.random() > 0.3:
search_terms.append(search_term_loc_people)
search_term_time_people = ' '.join(
shuffle([term_time, term_people]))
if random.random() > 0.3:
search_terms.append(search_term_time_people)
search_term_people_thing = ' '.join(
shuffle([term_people, term_thing]))
if random.random() > 0.9:
search_terms.append(search_term_people_thing)
search_term_all = ' '.join(
shuffle([term_loc, term_people, term_time, term_thing]))
if random.random() > 0.95:
search_terms.append(search_term_all)
else:
term_people = ''
search_term_loc_time = ' '.join(shuffle([term_loc, term_time]))
if random.random() > 0.3:
search_terms.append(search_term_loc_time)
search_term_loc_thing = ' '.join(shuffle([term_loc, term_thing]))
if random.random() > 0.9:
search_terms.append(search_term_loc_thing)
search_term_time_thing = ' '.join(shuffle([term_time, term_thing]))
if random.random() > 0.9:
search_terms.append(search_term_time_thing)
return list(set(search_terms))
def get_count_stats(user):
num_photos = Photo.objects.filter(owner=user).count()
num_faces = Face.objects.filter(photo__owner=user).count()
num_unknown_faces = Face.objects.filter(
Q(person__name__exact='unknown') & Q(photo__owner=user)).count()
num_labeled_faces = Face.objects.filter(
Q(person_label_is_inferred=False) & ~Q(person__name__exact='unknown') &
Q(photo__owner=user)).count()
num_inferred_faces = Face.objects.filter(
Q(person_label_is_inferred=True) & Q(photo__owner=user)).count()
num_people = Person.objects.count()
num_albumauto = AlbumAuto.objects.filter(owner=user).count()
num_albumdate = AlbumDate.objects.filter(owner=user).count()
num_albumuser = AlbumUser.objects.filter(owner=user).count()
res = {
"num_photos": num_photos,
"num_faces": num_faces,
"num_people": num_people,
"num_unknown_faces": num_unknown_faces,
"num_labeled_faces": num_labeled_faces,
"num_inferred_faces": num_inferred_faces,
"num_albumauto": num_albumauto,
"num_albumdate": num_albumdate,
"num_albumuser": num_albumuser,
}
return res
def get_location_clusters(user):
start = datetime.now()
photos = Photo.objects.filter(owner=user).exclude(geolocation_json={})
level = -3
coord_names = []
names = []
for p in photos:
for feature in p.geolocation_json['features']:
names.append(feature['text'])
if not feature['text'].isdigit():
coord_names.append([feature['text'], feature['center']])
# try:
# names.append(p.geolocation_json['features'][level]['text'])
# coord_names.append([
# p.geolocation_json['features'][level]['text'],
# p.geolocation_json['features'][level]['center']
# ])
# except:
# pass
groups = []
uniquekeys = []
coord_names.sort(key=lambda x: x[0])
for k, g in groupby(coord_names, lambda x: x[0]):
groups.append(list(g)) # Store group iterator as a list
uniquekeys.append(k)
res = [[g[0][1][1], g[0][1][0], g[0][0]] for g in groups]
elapsed = (datetime.now() - start).total_seconds()
logger.info('location clustering took %.2f seconds' % elapsed)
return res
# photos_with_gps = Photo.objects.exclude(exif_gps_lat=None)
# vecs_all = np.array([[p.exif_gps_lat,p.exif_gps_lon] for p in photos_with_gps])
# # bandwidth = estimate_bandwidth(vecs_all, quantile=0.005)
# bandwidth = 0.1
# ms = MeanShift(bandwidth=bandwidth, bin_seeding=True)
# ms.fit(vecs_all)
# labels = ms.labels_
# cluster_centers = ms.cluster_centers_
# labels_unique = np.unique(labels)
# n_clusters_ = len(labels_unique)
# return cluster_centers.tolist()
def get_photo_country_counts(user):
photos_with_gps = Photo.objects.exclude(geolocation_json=None).filter(owner=user)
geolocations = [p.geolocation_json for p in photos_with_gps]
# countries = [gl['features'][0]['properties']['country'] for gl in geolocations if 'features' in gl.keys() and len(gl['features']) > 0]
countries = []
for gl in geolocations:
if 'features' in gl.keys():
for feature in gl['features']:
if feature['place_type'][0] == 'country':
countries.append(feature['place_name'])
counts = Counter(countries)
# print(counts)
return counts
def get_location_sunburst(user):
photos_with_gps = Photo.objects.exclude(geolocation_json={}).exclude(
geolocation_json=None).filter(owner=user)
if photos_with_gps.count() == 0:
return {'children':[]}
geolocations = [p.geolocation_json for p in photos_with_gps]
four_levels = []
for gl in geolocations:
out_dict = {}
if 'features' in gl.keys():
if len(gl['features']) >= 1:
out_dict[1] = gl['features'][-1]['text']
if len(gl['features']) >= 2:
out_dict[2] = gl['features'][-2]['text']
if len(gl['features']) >= 3:
out_dict[3] = gl['features'][-3]['text']
# if len(gl['features']) >= 4:
# out_dict[4] = gl['features'][-4]['text']
# if len(gl['features']) >= 5:
# out_dict[5] = gl['features'][-5]['text']
four_levels.append(out_dict)
df = pd.DataFrame(four_levels)
df = df.groupby(
df.columns.tolist()).size().reset_index().rename(columns={4: 'count'})
dataStructure = {'name': 'Places I\'ve visited', 'children': []}
palette = sns.color_palette('hls', 10).as_hex()
for data in df.iterrows():
current = dataStructure
depthCursor = current['children']
for i, item in enumerate(data[1][:-2]):
idx = None
j = None
for j, c in enumerate(depthCursor):
if item in c.values():
idx = j
if idx == None:
depthCursor.append({
'name': item,
'children': [],
'hex': random.choice(palette)
})
idx = len(depthCursor) - 1
depthCursor = depthCursor[idx]['children']
if i == len(data[1]) - 3:
depthCursor.append({
'name': '{}'.format(list(data[1])[-2]),
'value': list(data[1])[-1],
'hex': random.choice(palette)
})
current = depthCursor
return dataStructure
def get_photo_month_counts(user):
counts = Photo.objects \
.filter(owner=user) \
.exclude(exif_timestamp=None) \
.annotate(month=TruncMonth('exif_timestamp')) \
.values('month') \
.annotate(c=Count('image_hash')) \
.values('month', 'c')
all_months = [
c['month'] for c in counts
if c['month'].year >= 2000 and c['month'].year <= datetime.now().year
]
if len(all_months) > 0:
first_month = min(all_months)
last_month = max(all_months)
month_span = jump_by_month(first_month, last_month)
counts = sorted(counts, key=lambda k: k['month'])
res = []
for count in counts:
key = '-'.join([str(count['month'].year), str(count['month'].month)])
count = count['c']
res.append([key, count])
res = dict(res)
out = []
for month in month_span:
m = '-'.join([str(month.year), str(month.month)])
if m in res.keys():
out.append({'month': m, 'count': res[m]})
else:
out.append({'month': m, 'count': 0})
return out
else:
return []
captions_sw = [
'a', 'of', 'the', 'on', 'in', 'at', 'has', 'holding', 'wearing', 'with',
'this', 'there', 'man', 'woman', '<unk>', 'along', 'no', 'is', 'big',
'small', 'large', 'and', 'backtround', 'looking', 'for', 'it', 'area',
'distance', 'was', 'white', 'black', 'brown', 'blue', 'background',
'ground', 'lot', 'red', 'wall', 'green', 'two', 'one', 'top', 'bottom',
'behind', 'front', 'building', 'shirt', 'hair', 'are', 'scene', 'tree',
'trees', 'sky', 'window', 'windows', 'standing', 'glasses', 'building',
'buildings'
]
captions_sw = [
'a', 'of', 'the', 'on', 'in', 'at', 'has', 'with', 'this', 'there',
'along', 'no', 'is', 'it', 'was', 'are', 'background'
]
def get_searchterms_wordcloud(user):
photos = Photo.objects.filter(owner=user).prefetch_related('faces__person')
captions = []
locations = []
people = []
location_entities = []
for photo in photos:
faces = photo.faces.all()
for face in faces:
people.append(face.person.name)
if photo.search_captions:
captions.append(photo.search_captions)
if photo.search_location:
locations.append(photo.search_location)
if photo.geolocation_json and 'features' in photo.geolocation_json.keys(
):
for feature in photo.geolocation_json['features']:
if not feature['text'].isdigit(
) and 'poi' not in feature['place_type']:
location_entities.append(feature['text'].replace(
'(', '').replace(')', ''))
caption_tokens = ' '.join(captions).replace(',', ' ').split()
location_tokens = ' '.join(locations).replace(',', ' ').replace(
'(', ' ').replace(')', ' ').split()
caption_tokens = [
t for t in caption_tokens
if not t.isdigit() and t.lower() not in captions_sw
]
location_tokens = [t for t in location_tokens if not t.isdigit()]
caption_token_counts = Counter(caption_tokens)
location_token_counts = Counter(location_tokens)
location_token_counts = Counter(location_entities)
people_counts = Counter(people)
caption_token_counts = [{
'label': key,
'y': np.log(value)
} for key, value in caption_token_counts.most_common(50)]
location_token_counts = [{
'label': key,
'y': np.log(value)
} for key, value in location_token_counts.most_common(50)]
people_counts = [{
'label': key,
'y': np.log(value)
} for key, value in people_counts.most_common(50)]
out = {
'captions': caption_token_counts,
'locations': location_token_counts,
'people': people_counts
}
return out