import os import re import time import numpy as np 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 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') phash = compute_phash_int(im) threshold = request.args.get('threshold') || 6 res = search_by_phash(phash, threshold) return jsonify({ 'res': res })