summaryrefslogtreecommitdiff
path: root/megapixels/app/server/api/image.py
blob: f2f4a4f934c20fe97678daead87ae4753c9b92b8 (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
from flask import Blueprint, render_template, abort
# from jinja2 import TemplateNotFound

router = Blueprint('image', __name__)

@router.route('/<dataset>/test', methods=['POST'])
def test(name):
  # dataset = 
@router.route('/<dataset>/face', methods=['POST'])
def upload(name):
  file = request.files['query_img']
  fn = file.filename
  if fn.endswith('blob'):
    fn = 'filename.jpg'

  basename, ext = os.path.splitext(fn)
  print("got {}, type {}".format(basename, ext))
  if ext.lower() not in valid_exts:
    return jsonify({ 'error': 'not an image' })

  uploaded_fn = datetime.now().isoformat() + "_" + basename
  uploaded_fn = sanitize_re.sub('', uploaded_fn)
  uploaded_img_path = "static/uploaded/" + uploaded_fn + ext
  uploaded_img_path = uploaded_img_path.lower()
  print('query: {}'.format(uploaded_img_path))

  img = Image.open(file.stream).convert('RGB')
  # img.save(uploaded_img_path)
  # vec = db.load_feature_vector_from_file(uploaded_img_path)
  vec = fe.extract(img)
  # print(vec.shape)

  results = db.search(vec, limit=limit)
  query = {
    'timing': time.time() - start,
  }
  print(results)
  return jsonify({
    'results': results,
  })