Skip to content

Commit

Permalink
resolves #78, #74, #65, #51, addresses #42, #48, adds basic photo
Browse files Browse the repository at this point in the history
similarity backend
  • Loading branch information
Hooram Nam committed Feb 26, 2019
1 parent e2d1d84 commit 8a13cea
Show file tree
Hide file tree
Showing 17 changed files with 614 additions and 411 deletions.
74 changes: 42 additions & 32 deletions api/api_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ def jump_by_month(start_date, end_date, month_step=1):
yield current_date


def get_location_timeline():
def get_location_timeline(user):
qs_photos = Photo.objects.exclude(geolocation_json={}).exclude(
exif_timestamp=None).order_by('exif_timestamp')
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'])
Expand Down Expand Up @@ -131,7 +131,7 @@ def get_search_term_examples(user):
except ValueError:
return [
'for people', 'for places', 'for things', 'for time',
'for file path'
'for file path or file name'
]

search_data = []
Expand Down Expand Up @@ -288,8 +288,8 @@ def get_location_clusters(user):
# return cluster_centers.tolist()


def get_photo_country_counts():
photos_with_gps = Photo.objects.exclude(geolocation_json=None)
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 = []
Expand All @@ -300,13 +300,17 @@ def get_photo_country_counts():
countries.append(feature['place_name'])

counts = Counter(countries)
print(counts)
# print(counts)
return counts


def get_location_sunburst():
def get_location_sunburst(user):
photos_with_gps = Photo.objects.exclude(geolocation_json={}).exclude(
geolocation_json=None)
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 = []
Expand Down Expand Up @@ -363,8 +367,9 @@ def get_location_sunburst():
return dataStructure


def get_photo_month_counts():
def get_photo_month_counts(user):
counts = Photo.objects \
.filter(owner=user) \
.exclude(exif_timestamp=None) \
.annotate(month=TruncMonth('exif_timestamp')) \
.values('month') \
Expand All @@ -375,28 +380,33 @@ def get_photo_month_counts():
c['month'] for c in counts
if c['month'].year >= 2000 and c['month'].year <= datetime.now().year
]
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})

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
return out
else:
return []


captions_sw = [
Expand All @@ -415,8 +425,8 @@ def get_photo_month_counts():
]


def get_searchterms_wordcloud():
photos = Photo.objects.all().prefetch_related('faces__person')
def get_searchterms_wordcloud(user):
photos = Photo.objects.filter(owner=user).prefetch_related('faces__person')
captions = []
locations = []
people = []
Expand Down
46 changes: 16 additions & 30 deletions api/autoalbum.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@

import ipdb

from api.flags import \
is_auto_albums_being_processed, \
is_photos_being_added, \
set_auto_album_processing_flag_on, \
set_auto_album_processing_flag_off
# from api.flags import \
# is_auto_albums_being_processed, \
# is_photos_being_added, \
# set_auto_album_processing_flag_on, \
# set_auto_album_processing_flag_off
from django_rq import job

from tqdm import tqdm
import rq
from api.util import logger
import pytz


# def is_auto_albums_being_processed():
# global FLAG_IS_AUTO_ALBUMS_BEING_PROCESSED
Expand Down Expand Up @@ -83,34 +86,18 @@ def regenerate_event_titles(user):

@job
def generate_event_albums(user):
job_id = rq.get_current_job().id
lrj = LongRunningJob(
started_by=user,
job_id=rq.get_current_job().id,
job_id=job_id,
started_at=datetime.now(),
job_type=LongRunningJob.JOB_GENERATE_AUTO_ALBUMS)
lrj.save()

if is_auto_albums_being_processed()['status']:
status = False
message = "There are even albums being created at the moment. Please try again later."
return {'status': status, 'message': message}

set_auto_album_processing_flag_on()
photo_count = Photo.objects.filter(owner=user).count()
if photo_count == 0:
status = False
message = "Please add some more photos!"
set_auto_album_processing_flag_off()
return {'status': status, 'message': message}
else:
if is_photos_being_added()['status']:
status = False
message = "There are photos being added to the library. Please try again later."
set_auto_album_processing_flag_off()
return {'status': status, 'message': message}

try:
photos = Photo.objects.filter(owner=user)
photos = Photo.objects.filter(owner=user).only('exif_timestamp')

photos_with_timestamp = [(photo.exif_timestamp, photo)
for photo in photos if photo.exif_timestamp]
Expand Down Expand Up @@ -138,14 +125,15 @@ def group(photos_with_timestamp, dt=timedelta(hours=6)):

album_locations = []

date_format = "%Y:%m:%d %H:%M:%S"
for group in groups:
key = group[0].exif_timestamp
print(key)
logger.info('job {}: processing auto album with date: '.format(job_id) + key.strftime(date_format))
items = group
if len(group) >= 2:
qs = AlbumAuto.objects.filter(timestamp=key).filter(owner=user)
if qs.count() == 0:
album = AlbumAuto(created_on=datetime.utcnow(), owner=user)
album = AlbumAuto(created_on=datetime.utcnow().replace(tzinfo=pytz.utc), owner=user)
album.timestamp = key
album.save()

Expand All @@ -155,8 +143,6 @@ def group(photos_with_timestamp, dt=timedelta(hours=6)):
item.save()
if item.exif_gps_lat and item.exif_gps_lon:
locs.append([item.exif_gps_lat, item.exif_gps_lon])
print('-', item.image_hash, 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)
Expand All @@ -166,6 +152,7 @@ def group(photos_with_timestamp, dt=timedelta(hours=6)):
album_locations.append([])
album._autotitle()
album.save()
logger.info('job {}: generated auto album {}'.format(job_id,album.id))
status = True
message = 'success'
res = {'status': status, 'message': message}
Expand All @@ -186,5 +173,4 @@ def group(photos_with_timestamp, dt=timedelta(hours=6)):
lrj.finished_at = datetime.now()
lrj.save()

set_auto_album_processing_flag_off()
return 1
return 1
Loading

0 comments on commit 8a13cea

Please sign in to comment.