Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support msgpack #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ RUN unzip 3.1.0.zip \
&& rm -r /opencv-3.1.0

ADD imagePicker/requirements.txt /opt/requirements.txt
ADD http://www.random.org/strings/?num=10&len=8&digits=on&upperalpha=on&loweralpha=on&unique=on&format=plain&rnd=new uuid
RUN pip install -r /opt/requirements.txt
ADD confs/* /opt/

Expand Down
26 changes: 17 additions & 9 deletions imagePicker/image_api/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import cv2
import base64
import numpy as np
import gc
import msgpack
from image_query import query
from rest_framework import status
from rest_framework.decorators import api_view
Expand Down Expand Up @@ -29,6 +29,12 @@ def search_images(request):
method = request.query_params['method']
else:
method = None

if 'disableBoW' in request.query_params.keys():
disableBoW = True
else:
disableBoW = False

try:
if request.FILES.get("image", None) is not None:
sketch = True if 'sketch' in request.query_params.keys() else False
Expand All @@ -37,7 +43,7 @@ def search_images(request):
img = cv2.imdecode(data, (-1 if sketch else cv2.IMREAD_COLOR))
results = query.get_results(search.search_image(
img, bow_hist=None, color_hist=None,
metric=method, sketch=sketch))[:limit]
metric=method, sketch=sketch), disableBoW)[:limit]
imgs = Image.objects.in_bulk(results)
sorted_imgs = [imgs[img_path] for img_path in results]
serializer = ImageSerializer(sorted_imgs, many=True)
Expand All @@ -46,24 +52,26 @@ def search_images(request):
else:

bow_hist = base64.b64decode(request.data['bow_hist'])
bow_hist = np.fromstring(bow_hist, dtype=np.float32)
bow_hist = np.array(msgpack.unpackb(bow_hist), dtype=np.float32)
color_hist = base64.b64decode(request.data['color_hist'])
color_hist = np.fromstring(color_hist, dtype=np.float32)
color_hist = np.array(msgpack.unpackb(color_hist), dtype=np.float32)
results = query.get_results(search.search_image(
img=None, bow_hist=bow_hist, color_hist=color_hist,
metric=method))[:limit]
metric=method), disableBoW)[:limit]
imgs = Image.objects.in_bulk(results)
sorted_imgs = [imgs[img_path] for img_path in results]
serializer = ImageSerializer(sorted_imgs, many=True)
return Response(serializer.data)
except:
return Response(status=status.HTTP_400_BAD_REQUEST)


@api_view(['GET'])
@renderer_classes((VocRenderer,))
@silk_profile(name='Get Voc')
def get_voc(request):
bow_voc = Vocabulary.objects.get().get_data()
bow_voc = base64.b64encode(bow_voc.tostring())
return Response(bow_voc)
try:
bow_voc = Vocabulary.objects.get().get_data()
bow_voc = base64.b64encode(msgpack.packb(bow_voc.tolist()))
return Response(bow_voc)
except:
return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR)
7 changes: 4 additions & 3 deletions imagePicker/image_query/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ def __init__(self):
self.dumper = Dumper()
self.query = Query()

def search_image(self, img, bow_hist, color_hist, metric, sketch):
def search_image(self, img, bow_hist, color_hist, metric, sketch=False):
return self.query.query_image(img, bow_hist, color_hist, self.dumper.get_img_data(), self.dumper.get_voc_data(), metric, sketch)

def get_results(results):
def get_results(results, disableBoW=False):
imgs = []
results = sorted(results, key=lambda element: (element[2], element[1]))
if not disableBoW:
results = sorted(results, key=lambda element: (element[1]))
for img, _, _ in results:
imgs.append(img)
return imgs
1 change: 1 addition & 0 deletions imagePicker/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ numpy
psycopg2
gunicorn
django-silk
msgpack-python
git+https://github.com/InstaSketch/algolib.git@develop