1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- from flask import Flask, request, jsonify
- import os
- from urllib.request import urlretrieve
- import time
- from zzz import ImageSearchEngine
- app = Flask(__name__)
- UPLOAD_FOLDER = 'static/images'
- ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif', 'bmp'}
- TOP_K = 5
- MIN_SCORE = 0.0
- MAX_SCORE = 100.0
- os.makedirs(UPLOAD_FOLDER, exist_ok=True)
- search_engine = ImageSearchEngine()
- @app.route("/", methods=["POST"])
- def invoke():
- """处理图片上传请求"""
- try:
-
- data = request.get_json()
- if not data:
- return jsonify({'error': '请求必须包含JSON数据'}), 400
-
- print(f"Received data: {data}")
- image_url = data.get('url')
-
- time_base = str(time.time() * 1000)
- image_path = os.path.join(UPLOAD_FOLDER, time_base + os.path.basename(image_url))
- urlretrieve(image_url, image_path)
-
- start_time = time.time()
- vector = search_engine._process_image(image_path)
- os.remove(image_path)
- return jsonify({"vector_spend_time": time.time() - start_time,"vector": vector.tolist()}),200
-
- except Exception as e:
- return jsonify({'error': str(e)}), 500
- if __name__ == '__main__':
- app.run(host='0.0.0.0', port=5001, debug=False)
|