def get_tags(): """get tags corresponding to a image""" if not 'img' in request.files: raise InvalidUsage('parameter "img" is missing', status_code=410) try: f = request.files.get('img') img_str = f.read() img = read_img_blob(img_str) except: raise InvalidUsage('Invalid "img" param, must be a blob string', status_code=410) tags = extractor.get_tags(img) out = {} out['tags'] = tags return jsonify(out)
def extract_fea(): imgStr = request.values.get('img') if imgStr is None: raise InvalidUsage('parameter "img" is missing', status_code=410) try: img = read_img_blob(imgStr) except: raise InvalidUsage('Invalid "img" param, must be a base64 string', status_code=410) fea = extractor.extract_imfea(img) is_binary = request.values.get('is_binary') if is_binary and is_binary == 'true': fea = extractor.binarize_fea(fea) fea_obj = ImFeaBin() else: fea_obj = ImFea() fea_obj.f.extend(fea) base64str = base64.b64encode(fea_obj.SerializeToString()) out = {} out['fea'] = base64str return jsonify(out)
def search(): """get tags corresponding to a image""" if not 'img' in request.files: raise InvalidUsage('parameter "img" is missing', status_code=410) try: f = request.files.get('img') img_str = f.read() img = read_img_blob(img_str) except: raise InvalidUsage('Invalid "img" param, must be a blob string', status_code=410) fea = extractor.extract_imfea(img) fea = extractor.binarize_fea(fea) fea_str = ','.join([str(int(t)) for t in fea]) query = QUERY.replace('##fea##', fea_str) print(query) result = es.search(index='im_data', doc_type='obj', body=query) rs = [] if 'hits' in result and \ 'hits' in result['hits']: #distinct all_imgs = set([]) hits = result['hits']['hits'] for hit in hits: o = hit['_source'] o['score'] = hit['_score'] #update im_src im_src = '/img/{}'.format(o['im_src']) if not im_src in all_imgs: o['im_src'] = im_src all_imgs.add(im_src) rs.append(o) print all_imgs out = {} out['hits'] = rs return jsonify(out)