12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- from flask import Blueprint, request, jsonify, redirect, url_for, current_app, send_from_directory, make_response
- import datetime
- import os
- import shutil
- bp = Blueprint('v1', __name__, url_prefix='/api/v1')
- # get flask app
- def allowed_file(filename):
- ALLOWED_EXTENSIONS = set(['png', 'jpg', 'JPG', 'PNG', 'bmp'])
- return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
- @bp.route('/')
- def home():
- return make_response(jsonify({
- 'status': 0,
- 'msg': "api/v1"
- }))
- @bp.route('/upload', methods=['GET', 'POST'])
- def upload_file():
- file = request.files['file']
- print(datetime.datetime.now(), file.filename)
- if file and allowed_file(file.filename):
- src_path = os.path.join(current_app.config['UPLOAD_FOLDER'], file.filename)
- file.save(src_path)
- shutil.copy(src_path, './tmp/ct')
- image_path = os.path.join('./tmp/ct', file.filename)
- print(src_path, image_path)
- pid, image_info = paddlex.main.c_main(image_path, current_app.model)
- return jsonify({'status': 1,
- 'image_url': 'http://127.0.0.1:5003/tmp/ct/' + pid,
- 'draw_url': 'http://127.0.0.1:5003/tmp/draw/' + pid,
- 'image_info': image_info
- })
- else:
- return jsonify({
- 'status': -1,
- 'msg': 'file type error'
- })
-
- @bp.route("/download", methods=['GET'])
- def download_file():
- # 需要知道2个参数, 第1个参数是本地目录的path, 第2个参数是文件名(带扩展名)
- return send_from_directory('data', 'testfile.zip', as_attachment=True)
- # show photo
- @bp.route('/tmp/<path:file>', methods=['GET'])
- def show_photo(file):
- if request.method == 'GET':
- if not file is None:
- try:
- image_data = open(f'tmp/{file}', "rb").read()
- response = make_response(image_data)
- response.headers['Content-Type'] = 'image/png'
- except Exception as e:
- response = make_response(jsonify({
- 'status': -1,
- 'msg': str(e)
- }))
- return response
|