v1.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. '''
  4. @Contact : liuyuqi.gov@msn.cn
  5. @Time : 2023/11/09 14:21:52
  6. @License : Copyright © 2017-2022 liuyuqi. All Rights Reserved.
  7. @Desc : api v1
  8. '''
  9. from flask import Blueprint, request, jsonify, current_app, send_from_directory, make_response
  10. import datetime
  11. import os
  12. import shutil
  13. from medical_assist import predict
  14. bp = Blueprint('v1', __name__, url_prefix='/api/v1')
  15. def allowed_file(filename):
  16. ALLOWED_EXTENSIONS = set(['png', 'jpg', 'JPG', 'PNG', 'bmp'])
  17. return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
  18. @bp.route('/')
  19. def home():
  20. return make_response(jsonify({
  21. 'status': 0,
  22. 'msg': "api/v1"
  23. }))
  24. @bp.route('/upload', methods=['GET','POST'])
  25. def upload_file():
  26. ''' upload file '''
  27. if 'file' not in request.files:
  28. return jsonify({
  29. 'status': -1,
  30. 'msg': 'no file part'
  31. })
  32. file = request.files['file']
  33. print(datetime.datetime.now(), file.filename)
  34. if file and allowed_file(file.filename):
  35. src_path = os.path.join(current_app.config['UPLOAD_FOLDER'], file.filename)
  36. file.save(src_path)
  37. shutil.copy(src_path, './tmp/ct')
  38. image_path = os.path.join('./tmp/ct', file.filename)
  39. print(src_path, image_path)
  40. pid, image_info = predict(image_path, current_app.pdx_model)
  41. return jsonify({'status': 1,
  42. 'image_url': 'http://127.0.0.1:5003/tmp/ct/' + pid,
  43. 'draw_url': 'http://127.0.0.1:5003/tmp/draw/' + pid,
  44. 'image_info': image_info
  45. })
  46. else:
  47. return jsonify({
  48. 'status': -1,
  49. 'msg': 'file type error'
  50. })
  51. @bp.route("/download", methods=['GET'])
  52. def download_file():
  53. # 需要知道2个参数, 第1个参数是本地目录的path, 第2个参数是文件名(带扩展名)
  54. return send_from_directory('data', 'testfile.zip', as_attachment=True)
  55. # show photo
  56. @bp.route('/tmp/<path:file>', methods=['GET'])
  57. def show_photo(file):
  58. if request.method == 'GET':
  59. if not file is None:
  60. try:
  61. image_data = open(f'tmp/{file}', "rb").read()
  62. response = make_response(image_data)
  63. response.headers['Content-Type'] = 'image/png'
  64. except Exception as e:
  65. response = make_response(jsonify({
  66. 'status': -1,
  67. 'msg': str(e)
  68. }))
  69. return response