import os import re import time import dlib from flask import Blueprint, request, jsonify from PIL import Image # todo: try to remove PIL dependency from app.processors import face_recognition from app.processors import face_detector from app.models.sql_factory import list_datasets, get_dataset, get_table sanitize_re = re.compile('[\W]+') valid_exts = ['.gif', '.jpg', '.jpeg', '.png'] api = Blueprint('api', __name__) @api.route('/') def index(): return jsonify({ 'datasets': list_datasets() }) @api.route('/dataset/') def show(name): dataset = get_dataset(name) if dataset: return jsonify(dataset.describe()) else: return jsonify({ 'status': 404 }) @api.route('/dataset//face', methods=['POST']) def upload(name): start = time.time() dataset = get_dataset(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' }) img = Image.open(file.stream).convert('RGB') # Face detection detector = face_detector.DetectorDLIBHOG() # get detection as BBox object bboxes = detector.detect(im, largest=True) bbox = bboxes[0] dim = im.shape[:2][::-1] bbox = bbox.to_dim(dim) # convert back to real dimensions # face recognition/vector recognition = face_recognition.RecognitionDLIB(gpu=-1) # print(vec.shape) # results = db.search(vec, limit=limit) # with the result we have an ID # query the sql dataset for the UUID etc here query = { 'timing': time.time() - start, } results = [] print(results) return jsonify({ 'query': query, 'results': results, }) @api.route('/dataset//name', methods=['GET']) def name_lookup(dataset): start = time.time() dataset = get_dataset(name) # we have a query from the request query string... # use this to do a like* query on the identities_meta table query = { 'timing': time.time() - start, } results = [] print(results) return jsonify({ 'query': query, 'results': results, })