Skip to content

Commit

Permalink
Adding support for blurhash
Browse files Browse the repository at this point in the history
  • Loading branch information
savvasdalkitsis committed Nov 27, 2024
1 parent ea8f612 commit 02a07be
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 0 deletions.
7 changes: 7 additions & 0 deletions api/directory_watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,13 @@ def handle_new_image(user, path, job_id, photo=None):
job_id, path, elapsed
)
)
photo._get_blurhash()
elapsed = (datetime.datetime.now() - start).total_seconds()
util.logger.info(
"job {}: get blurhash: {}, elapsed: {}".format(
job_id, path, elapsed
)
)
photo._recreate_search_captions()
elapsed = (datetime.datetime.now() - start).total_seconds()
util.logger.info(
Expand Down
14 changes: 14 additions & 0 deletions api/migrations/0076_add_blurhash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("api", "0075_alter_face_cluster_person"),
]
operations = [
migrations.AddField(
model_name="Photo",
name="blurhash",
field=models.TextField(blank=True, null=True),
),
]
13 changes: 13 additions & 0 deletions api/models/photo.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import numpy as np
import PIL
import requests
import blurhash
from django.contrib.postgres.fields import ArrayField
from django.core.files.base import ContentFile
from django.db import models
Expand Down Expand Up @@ -73,6 +74,7 @@ class Photo(models.Model):
captions_json = models.JSONField(blank=True, null=True, db_index=True)

dominant_color = models.TextField(blank=True, null=True)
blurhash = models.TextField(blank=True, null=True)

search_captions = models.TextField(blank=True, null=True, db_index=True)
search_location = models.TextField(blank=True, null=True, db_index=True)
Expand Down Expand Up @@ -836,6 +838,17 @@ def _get_dominant_color(self, palette_size=16):
except Exception:
logger.info("Cannot calculate dominant color {} object".format(self))

def _get_blurhash(self, palette_size=16):
# Skip if it's already calculated
if self.blurhash:
return
try:
hash = blurhash.encode(self.square_thumbnail_small.path, x_components=4, y_components=4)
self.blurhash = hash
self.save()
except Exception:
logger.info("Cannot calculate blurhash {} object".format(self))

def manual_delete(self):
for file in self.files.all():
if os.path.isfile(file.path):
Expand Down
8 changes: 8 additions & 0 deletions api/serializers/photos.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
class PhotoSummarySerializer(serializers.ModelSerializer):
id = serializers.SerializerMethodField()
dominantColor = serializers.SerializerMethodField()
blurhash = serializers.SerializerMethodField()
aspectRatio = serializers.SerializerMethodField()
url = serializers.SerializerMethodField()
location = serializers.SerializerMethodField()
Expand All @@ -25,6 +26,7 @@ class Meta:
fields = (
"id",
"dominantColor",
"blurhash",
"url",
"location",
"date",
Expand Down Expand Up @@ -84,6 +86,12 @@ def get_dominantColor(self, obj) -> str:
else:
return ""

def get_blurhash(self, obj) -> str:
if obj.blurhash:
return obj.blurhash
else:
return ""

def get_type(self, obj) -> str:
if obj.video:
return "video"
Expand Down
1 change: 1 addition & 0 deletions api/views/albums.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ def get_queryset(self):
"main_file",
"search_location",
"dominant_color",
"blurhash",
"public",
"rating",
"hidden",
Expand Down
2 changes: 2 additions & 0 deletions api/views/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def list(self, request):
"main_file",
"search_location",
"dominant_color",
"blurhash",
"public",
"rating",
"hidden",
Expand Down Expand Up @@ -82,6 +83,7 @@ def list(self, request):
"main_file",
"search_location",
"dominant_color",
"blurhash",
"public",
"rating",
"hidden",
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,4 @@ timm==1.0.11
transformers==4.46.2
# Dependencies for mistral quantized
llama-cpp-python==0.3.1
blurhash-python==1.2.2

0 comments on commit 02a07be

Please sign in to comment.