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

from app.models.sql_factory import search_by_phash, add_phash
from app.utils.im_utils import compute_phash_int
from app.utils.file_utils import sha256_stream

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

LIMIT = 9

api = Blueprint('api', __name__)

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

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

  file = request.files['q']
  fn = file.filename
  if fn.endswith('blob'): # FIX PNG IMAGES?
    logging.debug('received a blob, assuming JPEG')
    fn = 'filename.jpg'

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

  ext = ext[1:].lower()

  im = Image.open(file.stream).convert('RGB')
  phash = compute_phash_int(im)

  try:
    threshold = int(request.args.get('threshold') or 6)
    limit = int(request.args.get('limit') or 1)
    add = str(request.args.get('add') or 'true') == 'true'
  except:
    return jsonify({
      'success': False,
      'match': False,
      'error': 'param_error'
    })

  results = search_by_phash(phash=phash, threshold=threshold, limit=limit)

  if len(results) == 0:
    if add:
      hash = sha256_stream(file)
      add_phash(sha256=hash, phash=phash, ext=ext)
    match = False
  else:
    match = True

  logging.debug('query took {0:.2g} s.'.format(time.time() - start))

  if limit > 1:
    return jsonify({
      'success': True,
      'match': match,
      'results': results,
      'timing': time.time() - start,
    })

  return jsonify({
    'success': True,
    'match': match,
    'closest_match': results[0] if len(results) else None,
    'timing': time.time() - start,
  })