def search(): try: user_id = request.args.get('user_id') group_id = request.args.get('group_id') if not user_id: return jsonify({'code': 400, 'msg': 'user_id is required.'}) elif not group_id: return jsonify({'code': 400, 'msg': 'group_id is required.'}) expires = current_app.config['OSS_URL_EXPIRES'] caches = get_caches_by_user_id_and_group_id(user_id, group_id) data = [] notified = [] for cache in caches: notified.append(cache) photo = get_image(cache[3]) oss_url = get_oss_url(photo[2], expires) one = { 'user_id': cache[1], 'group_id': cache[2], 'image_id': cache[3], 'url': oss_url, 'url_express': expires } # start = now() data.append(one) return jsonify({'code': 200, 'data': data}) except Exception as e: return jsonify({'code': 500, 'error': "{0}".format(e)})
def get(image_id): try: if request.args.get('expires'): expires = int(request.args.get('expires')) else: expires = current_app.config['OSS_URL_EXPIRES'] print(expires) if not image_id: return jsonify({'code': 400, 'msg': 'image_id is required.'}) # 1、获得photo image = get_image(image_id) oss_url = get_oss_url(image[2], expires) if not image: return jsonify({ 'code': 400, 'msg': 'can not found image by id [{0}]'.format(image_id) }) return jsonify({'code': 200, 'oss_url': oss_url}) except Exception as e: return jsonify({'code': 500, 'error': "{0}".format(e)})
def notify_task(app, cache_ids): with app.app_context(): notify_url = current_app.config['NOTIFY_URL'] expires = current_app.config['OSS_URL_EXPIRES'] print("begin to send notify task") data = [] notified = [] for cache_id in cache_ids: if cache_id: cache = get_cache(cache_id) notified.append(cache) photo = get_image(cache[3]) oss_url = get_oss_url(photo[2], expires) one = { 'user_id': cache[1], 'group_id': cache[2], 'image_id': cache[3], 'url': oss_url, 'url_express': expires } # start = now() data.append(one) result = {'notify_time': nowTime, 'data': data} try: r.post(notify_url, data=json.dumps(result), timeout=5.0, headers={'content-type': 'application/json'}) except Exception as e: print(e) # traceback.print_exc() for temp in notified: update_cache(temp[1], temp[2], temp[3], True, temp[0]) print("send notify to %s finish" % notify_url)
def search(): try: group_id = request.args.get('group_id') image_id = request.args.get('image_id') if not image_id: return jsonify({'code': 400, 'msg': 'image_id is required.'}) elif not group_id: return jsonify({'code': 400, 'msg': 'group_id is required.'}) # 1、获得photo image = get_image(image_id) # 2、获得指定group_id下的所有photo target_images = get_images_by_group_id(group_id) if not image: return jsonify({ 'code': 400, 'msg': 'cant not find image by id {0}'.format(image_id) }) if not target_images: return jsonify({ 'code': 400, 'msg': 'cant not find any image by group id {0}'.format(group_id) }) # 3、比对features值 sorted_matched_images = find_similar(image, target_images) result_body = [] for matched in sorted_matched_images: oss_path = matched[2] expires = current_app.config['OSS_URL_EXPIRES'] oss_url = get_oss_url(oss_path, expires) result_body.append({ 'id': matched[0], 'name': matched[1], 'url': oss_url, 'expires': expires }) response = jsonify({'code': 200, 'data': result_body}) response.headers.add('Access-Control-Allow-Origin', '*') return response except Exception as e: return jsonify({'code': 500, 'error': "{0}".format(e)})
def search_by_photo(): """upload a photo to group.""" global local_filename if request.method == 'POST': group_id = request.form['group_id'] if not group_id: return jsonify({'code': 400, 'msg': 'group_id is required.'}) if 'photo' not in request.files: return jsonify({'code': 400, 'msg': 'photo is required.'}) photo = request.files['photo'] try: # 1、校验组是否存在 group = get_group(group_id) if not group: return jsonify({ 'code': 400, 'msg': 'can not found group by id [{0}]'.format(group_id) }) # 2、获得指定group_id下的所有photo target_images = get_images_by_group_id(group_id) if not target_images: return jsonify({ 'code': 400, 'msg': 'cant not find any image by group id {0}'.format(group_id) }) limit = float(current_app.config['FEATURES_MAX_MATCH_LIMIT']) limit = 2 * (1 - limit) print("limit {0}".format(limit)) # 3、上传图片 photos = UploadSet('photos', IMAGES) configure_uploads(current_app, photos) filename = photos.save(photo) local_filename = photos.path(filename) # 4、获得图片的base64编码 photo_base64 = base64.b64encode(open(local_filename, 'rb').read()) encode_str = str(photo_base64, 'utf-8') # 5、通过第三方接口获得图片的特征值 ret = r.post(current_app.config['GET_FEATURE_URL'], data=json.dumps({'image': encode_str}), headers={'content-type': 'application/json'}) response = json.loads(ret.text) print(filename) if 'body' in response: body = response['body'] image = (None, filename, None, int(group_id), json.dumps(body)) # 6、比对feature值 sorted_matched_images = find_similar(image, target_images) result_body = [] for matched in sorted_matched_images: oss_path = matched[2] expires = current_app.config['OSS_URL_EXPIRES'] oss_url = get_oss_url(oss_path, expires) result_body.append({ 'id': matched[0], 'name': matched[1], 'url': oss_url, 'expires': expires }) return jsonify({'code': 200, 'data': result_body}) else: return jsonify(response) except Exception as e: traceback.print_exc() return jsonify({'code': 500, 'error': '{0}'.format(e)}) finally: # 6、删除临时图片 try: if os.path.isfile(local_filename): os.remove(local_filename) except FileNotFoundError: print("delete not exits file") except Exception: traceback.print_exc() else: return jsonify({'code': 400, 'msg': 'upload image failed.'})