from flask import Flask, render_template, request, session, redirect, url_for, flash, jsonify from flask_login import current_user from flask_app.decorators import permission_required from flask_app.models import Permission from .predict_xray import detectxray from .predict_ct import detectct from .predict_sevxray import detectsev_xray import os from . import detect @detect.route('/upload.html') @permission_required(Permission.COMMENT) def upload(): session['next'] = request.url # 将当前URL保存到session中 active_page = "index" return render_template('detect/upload.html',active_page=active_page) @detect.route('/upload_chest.html') @permission_required(Permission.COMMENT) def upload_chest(): session['next'] = request.url # 将当前URL保存到session中 return render_template('detect/upload_chest.html') @detect.route('/upload_ct.html') @permission_required(Permission.COMMENT) def upload_ct(): session['next'] = request.url # 将当前URL保存到session中 return render_template('detect/upload_ct.html') @detect.route('/uploaded_chest/', methods = ['POST', 'GET']) def uploaded_chest(uid): approot=detect.root_path dir_path = os.path.dirname(approot) #flask_app if request.method == 'POST': # check if the post request has the file part if 'file' not in request.files: flash('No file part') return redirect(request.url) file = request.files['file'] if file.filename == '': flash('No selected file') return redirect(request.url) if file: temp_dir = os.path.join(dir_path, 'static/images/upload_img', str(uid)) if(not os.path.exists(temp_dir)): os.makedirs(temp_dir) file.save(os.path.join(temp_dir,'upload_chest.jpg')) #选择的图片保存到指定文件夹 return render_template('detect/predicting.html', redirect_url=url_for('detect.result_chest')) @detect.route('/result_chest') @permission_required(Permission.COMMENT) def result_chest(): session['next'] = request.url # 将当前URL保存到session中 approot = detect.root_path dir_path = os.path.dirname(approot) imagepath = os.path.join(dir_path, 'static/images/upload_img',str(current_user.id),'upload_chest.jpg') pred_type, pred_normal, pred_pneu, pred_covid = detectxray(imagepath) return render_template('detect/results_chest.html', pred_type=pred_type, pred_covid=pred_covid, pred_pneu=pred_pneu, pred_normal=pred_normal) @detect.route('/uploaded_ct/', methods = ['POST', 'GET']) def uploaded_ct(uid): approot=detect.root_path dir_path = os.path.dirname(approot) if request.method == 'POST': # check if the post request has the file part if 'file' not in request.files: flash('No file part') return redirect(request.url) file = request.files['file'] if file.filename == '': flash('No selected file') return redirect(request.url) if file: temp_dir = os.path.join(dir_path, 'static/images/upload_img', str(uid)) if (not os.path.exists(temp_dir)): os.makedirs(temp_dir) file.save(os.path.join(temp_dir, 'upload_ct.jpg')) # 选择的图片保存到指定文件夹 return render_template('detect/predicting.html', redirect_url=url_for('detect.result_ct')) @detect.route('/result_ct') @permission_required(Permission.COMMENT) def result_ct(): session['next'] = request.url # 将当前URL保存到session中 approot = detect.root_path dir_path = os.path.dirname(approot) imagepath = os.path.join(dir_path, 'static/images/upload_img',str(current_user.id),'upload_ct.jpg') pred_type,pred_normal,pred_pneu,pred_covid=detectct(imagepath) return render_template('detect/results_ct.html',pred_type=pred_type,pred_covid=pred_covid,pred_pneu=pred_pneu,pred_normal=pred_normal) @detect.route('/sev_xray') def sev_xray(): return render_template('detect/predicting.html', redirect_url=url_for('detect.result_sevxray')) @detect.route('/result_sevxray') @permission_required(Permission.COMMENT) def result_sevxray(): session['next'] = request.url # 将当前URL保存到session中 approot = detect.root_path dir_path = os.path.dirname(approot) imagepath = os.path.join(dir_path, 'static/images/upload_img',str(current_user.id),'upload_chest.jpg') pred_type,pred_mild,pred_sev=detectsev_xray(imagepath) return render_template('detect/results_sevxray.html',pred_type=pred_type,pred_mild=pred_mild,pred_sev=pred_sev)