-
-
Notifications
You must be signed in to change notification settings - Fork 310
/
autoalbum.py
244 lines (219 loc) · 8.74 KB
/
autoalbum.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
from datetime import datetime, timedelta
import numpy as np
import pytz
from django.db.models import Q
from api.models import (
AlbumAuto,
AlbumDate,
AlbumPlace,
AlbumThing,
AlbumUser,
Face,
File,
LongRunningJob,
Photo,
)
from api.util import logger
def regenerate_event_titles(user, job_id):
if LongRunningJob.objects.filter(job_id=job_id).exists():
lrj = LongRunningJob.objects.get(job_id=job_id)
lrj.started_at = datetime.now().replace(tzinfo=pytz.utc)
lrj.save()
else:
lrj = LongRunningJob.objects.create(
started_by=user,
job_id=job_id,
queued_at=datetime.now().replace(tzinfo=pytz.utc),
started_at=datetime.now().replace(tzinfo=pytz.utc),
job_type=LongRunningJob.JOB_GENERATE_AUTO_ALBUM_TITLES,
)
lrj.save()
try:
aus = AlbumAuto.objects.filter(owner=user).prefetch_related("photos")
target_count = len(aus)
for idx, au in enumerate(aus):
logger.info("job {}: {}".format(job_id, idx))
au._generate_title()
au.save()
lrj.progress_current = idx + 1
lrj.progress_target = target_count
lrj.save()
lrj.finished = True
lrj.finished_at = datetime.now().replace(tzinfo=pytz.utc)
lrj.save()
logger.info("job {}: updated lrj entry to db".format(job_id))
except Exception:
logger.exception("An error occurred")
lrj.failed = True
lrj.finished = True
lrj.finished_at = datetime.now().replace(tzinfo=pytz.utc)
lrj.save()
return 1
def generate_event_albums(user, job_id):
if LongRunningJob.objects.filter(job_id=job_id).exists():
lrj = LongRunningJob.objects.get(job_id=job_id)
lrj.started_at = datetime.now().replace(tzinfo=pytz.utc)
lrj.save()
else:
lrj = LongRunningJob.objects.create(
started_by=user,
job_id=job_id,
queued_at=datetime.now().replace(tzinfo=pytz.utc),
started_at=datetime.now().replace(tzinfo=pytz.utc),
job_type=LongRunningJob.JOB_GENERATE_AUTO_ALBUMS,
)
lrj.save()
try:
photos = (
Photo.objects.filter(Q(owner=user))
.exclude(Q(exif_timestamp=None))
.only("exif_timestamp")
)
def group(photos, dt=timedelta(hours=6)):
photos_with_timestamp = sorted(photos, key=lambda p: p.exif_timestamp)
groups = []
for idx, photo in enumerate(photos_with_timestamp):
if len(groups) == 0:
groups.append([])
groups[-1].append(photo)
else:
# Photos are sorted by timestamp, so we can just check the last photo of the last group
# to see if it is within the time delta
if photo.exif_timestamp - groups[-1][-1].exif_timestamp < dt:
groups[-1].append(photo)
# If the photo is not within the time delta, we create a new group
else:
groups.append([])
groups[-1].append(photo)
return groups
# Group images that are on the same 1 day and 12 hours interval
groups = group(photos, dt=timedelta(days=1, hours=12))
target_count = len(groups)
logger.info(
"job {}: made {} groups out of {} images".format(
job_id, target_count, len(photos)
)
)
album_locations = []
date_format = "%Y:%m:%d %H:%M:%S"
for idx, group in enumerate(groups):
key = group[0].exif_timestamp - timedelta(hours=11, minutes=59)
lastKey = group[-1].exif_timestamp + timedelta(hours=11, minutes=59)
logger.info(str(key.date) + " - " + str(lastKey.date))
logger.info(
"job {}: processing auto album with date: ".format(job_id)
+ key.strftime(date_format)
+ " to "
+ lastKey.strftime(date_format)
)
items = group
if len(group) >= 2:
qs = AlbumAuto.objects.filter(owner=user).filter(
timestamp__range=(key, lastKey)
)
if qs.count() == 0:
album = AlbumAuto(
created_on=datetime.utcnow().replace(tzinfo=pytz.utc),
owner=user,
)
album.timestamp = key
album.save()
logger.info(
"job {}: generate auto album {}".format(job_id, album.id)
)
locs = []
for item in items:
album.photos.add(item)
item.save()
if item.exif_gps_lat and item.exif_gps_lon:
locs.append([item.exif_gps_lat, item.exif_gps_lon])
if len(locs) > 0:
album_location = np.mean(np.array(locs), 0)
album_locations.append(album_location)
album.gps_lat = album_location[0]
album.gps_lon = album_location[1]
else:
album_locations.append([])
album._generate_title()
album.save()
continue
if qs.count() == 1:
album = qs.first()
logger.info("job {}: update auto album {}".format(job_id, album.id))
for item in items:
if item in album.photos.all():
continue
album.photos.add(item)
item.save()
album._generate_title()
album.save()
continue
if qs.count() > 1:
# To-Do: Merge both auto albums
logger.info(
"job {}: found multiple auto albums for date {}".format(
job_id, key.strftime(date_format)
)
)
continue
lrj.progress_current = idx + 1
lrj.progress_target = target_count
lrj.save()
lrj.finished = True
lrj.finished_at = datetime.now().replace(tzinfo=pytz.utc)
lrj.save()
except Exception:
logger.exception("An error occurred")
lrj.failed = True
lrj.finished = True
lrj.finished_at = datetime.now().replace(tzinfo=pytz.utc)
lrj.save()
return 1
# To-Do: This does not belong here
def delete_missing_photos(user, job_id):
if LongRunningJob.objects.filter(job_id=job_id).exists():
lrj = LongRunningJob.objects.get(job_id=job_id)
lrj.started_at = datetime.now().replace(tzinfo=pytz.utc)
lrj.save()
else:
lrj = LongRunningJob.objects.create(
started_by=user,
job_id=job_id,
queued_at=datetime.now().replace(tzinfo=pytz.utc),
started_at=datetime.now().replace(tzinfo=pytz.utc),
job_type=LongRunningJob.JOB_DELETE_MISSING_PHOTOS,
)
lrj.save()
try:
missing_photos = Photo.objects.filter(
Q(owner=user) & Q(files=None) | Q(main_file=None)
)
for missing_photo in missing_photos:
album_dates = AlbumDate.objects.filter(photos=missing_photo)
for album_date in album_dates:
album_date.photos.remove(missing_photo)
album_things = AlbumThing.objects.filter(photos=missing_photo)
for album_thing in album_things:
album_thing.photos.remove(missing_photo)
album_places = AlbumPlace.objects.filter(photos=missing_photo)
for album_place in album_places:
album_place.photos.remove(missing_photo)
album_users = AlbumUser.objects.filter(photos=missing_photo)
for album_user in album_users:
album_user.photos.remove(missing_photo)
faces = Face.objects.filter(photo=missing_photo)
faces.delete()
# To-Do: Remove thumbnails
missing_photos.delete()
missing_files = File.objects.filter(Q(hash__endswith=user) & Q(missing=True))
missing_files.delete()
lrj.finished = True
lrj.finished_at = datetime.now().replace(tzinfo=pytz.utc)
lrj.save()
except Exception:
logger.exception("An error occurred")
lrj.failed = True
lrj.finished = True
lrj.finished_at = datetime.now().replace(tzinfo=pytz.utc)
lrj.save()
return 1