123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- #!/usr/bin/env python
- # -*- encoding: utf-8 -*-
- '''
- @Contact : liuyuqi.gov@msn.cn
- @Time : 2023/11/09 14:21:52
- @License : Copyright © 2017-2022 liuyuqi. All Rights Reserved.
- @Desc : api v1
- '''
- from flask import Blueprint, request, jsonify, current_app, send_from_directory, make_response
- import datetime
- import os
- import shutil
- from medical_assist import predict
- bp = Blueprint('v1', __name__, url_prefix='/api/v1')
- 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():
- ''' upload file '''
- if 'file' not in request.files:
- return jsonify({
- 'status': -1,
- 'msg': 'no file part'
- })
-
- 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 = predict(image_path, current_app.pdx_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
|