blob: 1b17a1e9203e69d98b32cea2c3b70efc77d6a81e (
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
|
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 test endpoint
"""
return jsonify({ 'status': 'ok' })
@api.route('/v1/match/', methods=['POST'])
def upload():
"""
Search by uploading an 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 })
|