summaryrefslogtreecommitdiff
path: root/check/app/server/api.py
blob: 6b97b59260f8e7614d2dc943b3a01b979adbd83c (plain)
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
import os
import re
import time
import numpy as np
from flask import Blueprint, request, jsonify
from PIL import Image

# from app.utils.im_utils import pil2np
from app.models.sql_factory import search_by_phash, add_phash
from app.utils.im_utils import pil2np

sanitize_re = re.compile('[\W]+')
valid_exts = ['.gif', '.jpg', '.jpeg', '.png']

LIMIT = 9

api = Blueprint('api', __name__)

@api.route('/')
def index():
  """API status noop"""
  return jsonify({ 'status': 'ok' })

@api.route('/v1/match/', methods=['POST'])
def upload():
  """Search by image"""
  start = time.time()

  file = request.files['query_img']
  fn = file.filename
  if fn.endswith('blob'): # FIX PNG IMAGES?
    fn = 'filename.jpg'

  basename, ext = os.path.splitext(fn)
  if ext.lower() not in valid_exts:
    return jsonify({
      'error': 'not_an_image'
    })

  im = Image.open(file.stream).convert('RGB')
  im_np = pil2np(im)

  res = search_by_phash(im_np)

  # get threshold
  return jsonify({ 'res': res })