|
|
|
|
@ -1,176 +0,0 @@
|
|
|
|
|
"""
|
|
|
|
|
演示图片控制器
|
|
|
|
|
处理预设图像对比图的展示功能
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
from flask import Blueprint, send_file, jsonify, current_app
|
|
|
|
|
from flask_jwt_extended import jwt_required
|
|
|
|
|
from app.database import Perturbation, Finetune
|
|
|
|
|
import os
|
|
|
|
|
import glob
|
|
|
|
|
|
|
|
|
|
demo_bp = Blueprint('demo', __name__)
|
|
|
|
|
|
|
|
|
|
@demo_bp.route('/images', methods=['GET'])
|
|
|
|
|
def list_demo_images():
|
|
|
|
|
"""获取所有演示图片列表"""
|
|
|
|
|
try:
|
|
|
|
|
demo_images = []
|
|
|
|
|
|
|
|
|
|
# 获取演示原始图片 - 修正路径构建
|
|
|
|
|
# 获取项目根目录(backend目录)
|
|
|
|
|
project_root = os.path.dirname(current_app.root_path)
|
|
|
|
|
original_folder = os.path.join(project_root, current_app.config['DEMO_ORIGINAL_FOLDER'])
|
|
|
|
|
|
|
|
|
|
if os.path.exists(original_folder):
|
|
|
|
|
original_files = glob.glob(os.path.join(original_folder, '*'))
|
|
|
|
|
for file_path in original_files:
|
|
|
|
|
if file_path.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')):
|
|
|
|
|
filename = os.path.basename(file_path)
|
|
|
|
|
name_without_ext = os.path.splitext(filename)[0]
|
|
|
|
|
|
|
|
|
|
# 查找对应的加噪图片
|
|
|
|
|
perturbed_folder = os.path.join(project_root, current_app.config['DEMO_PERTURBED_FOLDER'])
|
|
|
|
|
perturbed_files = glob.glob(os.path.join(perturbed_folder, f"{name_without_ext}*"))
|
|
|
|
|
|
|
|
|
|
# 查找对应的对比图
|
|
|
|
|
comparison_folder = os.path.join(project_root, current_app.config['DEMO_COMPARISONS_FOLDER'])
|
|
|
|
|
comparison_files = glob.glob(os.path.join(comparison_folder, f"{name_without_ext}*"))
|
|
|
|
|
|
|
|
|
|
demo_image = {
|
|
|
|
|
'id': name_without_ext,
|
|
|
|
|
'name': name_without_ext,
|
|
|
|
|
'original': f"/api/demo/image/original/{filename}",
|
|
|
|
|
'perturbed': [f"/api/demo/image/perturbed/{os.path.basename(f)}" for f in perturbed_files],
|
|
|
|
|
'comparisons': [f"/api/demo/image/comparison/{os.path.basename(f)}" for f in comparison_files]
|
|
|
|
|
}
|
|
|
|
|
demo_images.append(demo_image)
|
|
|
|
|
|
|
|
|
|
return jsonify({
|
|
|
|
|
'demo_images': demo_images,
|
|
|
|
|
'total': len(demo_images)
|
|
|
|
|
}), 200
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
return jsonify({'error': f'获取演示图片列表失败: {str(e)}'}), 500
|
|
|
|
|
|
|
|
|
|
@demo_bp.route('/image/original/<filename>', methods=['GET'])
|
|
|
|
|
def get_demo_original_image(filename):
|
|
|
|
|
"""获取演示原始图片"""
|
|
|
|
|
try:
|
|
|
|
|
project_root = os.path.dirname(current_app.root_path)
|
|
|
|
|
file_path = os.path.join(project_root, current_app.config['DEMO_ORIGINAL_FOLDER'], filename)
|
|
|
|
|
|
|
|
|
|
if not os.path.exists(file_path):
|
|
|
|
|
return jsonify({'error': '图片不存在'}), 404
|
|
|
|
|
|
|
|
|
|
return send_file(file_path, as_attachment=False)
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
return jsonify({'error': f'获取原始图片失败: {str(e)}'}), 500
|
|
|
|
|
|
|
|
|
|
@demo_bp.route('/image/perturbed/<filename>', methods=['GET'])
|
|
|
|
|
def get_demo_perturbed_image(filename):
|
|
|
|
|
"""获取演示加噪图片"""
|
|
|
|
|
try:
|
|
|
|
|
project_root = os.path.dirname(current_app.root_path)
|
|
|
|
|
file_path = os.path.join(project_root, current_app.config['DEMO_PERTURBED_FOLDER'], filename)
|
|
|
|
|
|
|
|
|
|
if not os.path.exists(file_path):
|
|
|
|
|
return jsonify({'error': '图片不存在'}), 404
|
|
|
|
|
|
|
|
|
|
return send_file(file_path, as_attachment=False)
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
return jsonify({'error': f'获取加噪图片失败: {str(e)}'}), 500
|
|
|
|
|
|
|
|
|
|
@demo_bp.route('/image/comparison/<filename>', methods=['GET'])
|
|
|
|
|
def get_demo_comparison_image(filename):
|
|
|
|
|
"""获取演示对比图片"""
|
|
|
|
|
try:
|
|
|
|
|
project_root = os.path.dirname(current_app.root_path)
|
|
|
|
|
file_path = os.path.join(project_root, current_app.config['DEMO_COMPARISONS_FOLDER'], filename)
|
|
|
|
|
|
|
|
|
|
if not os.path.exists(file_path):
|
|
|
|
|
return jsonify({'error': '图片不存在'}), 404
|
|
|
|
|
|
|
|
|
|
return send_file(file_path, as_attachment=False)
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
return jsonify({'error': f'获取对比图片失败: {str(e)}'}), 500
|
|
|
|
|
|
|
|
|
|
@demo_bp.route('/algorithms', methods=['GET'])
|
|
|
|
|
def get_demo_algorithms():
|
|
|
|
|
"""获取演示算法信息"""
|
|
|
|
|
try:
|
|
|
|
|
# 从数据库获取扰动算法
|
|
|
|
|
perturbation_algorithms = []
|
|
|
|
|
perturbation_configs = Perturbation.query.all()
|
|
|
|
|
for config in perturbation_configs:
|
|
|
|
|
perturbation_algorithms.append({
|
|
|
|
|
'id': config.id,
|
|
|
|
|
'code': config.method_code,
|
|
|
|
|
'name': config.method_name,
|
|
|
|
|
'type': 'perturbation',
|
|
|
|
|
'description': config.description,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
# 从数据库获取微调算法
|
|
|
|
|
finetune_algorithms = []
|
|
|
|
|
finetune_configs = Finetune.query.all()
|
|
|
|
|
for config in finetune_configs:
|
|
|
|
|
finetune_algorithms.append({
|
|
|
|
|
'id': config.id,
|
|
|
|
|
'code': config.method_code,
|
|
|
|
|
'name': config.method_name,
|
|
|
|
|
'type': 'finetune',
|
|
|
|
|
'description': config.description
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return jsonify({
|
|
|
|
|
'perturbation_algorithms': perturbation_algorithms,
|
|
|
|
|
'finetune_algorithms': finetune_algorithms,
|
|
|
|
|
'evaluation_metrics': [
|
|
|
|
|
{'name': 'FID', 'description': 'Fréchet Inception Distance - 衡量图像质量的指标'},
|
|
|
|
|
{'name': 'LPIPS', 'description': 'Learned Perceptual Image Patch Similarity - 感知相似度'},
|
|
|
|
|
{'name': 'SSIM', 'description': 'Structural Similarity Index - 结构相似性指标'},
|
|
|
|
|
{'name': 'PSNR', 'description': 'Peak Signal-to-Noise Ratio - 峰值信噪比'}
|
|
|
|
|
]
|
|
|
|
|
}), 200
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
return jsonify({'error': f'获取算法信息失败: {str(e)}'}), 500
|
|
|
|
|
|
|
|
|
|
@demo_bp.route('/stats', methods=['GET'])
|
|
|
|
|
def get_demo_stats():
|
|
|
|
|
"""获取演示统计信息"""
|
|
|
|
|
try:
|
|
|
|
|
# 统计演示图片数量
|
|
|
|
|
project_root = os.path.dirname(current_app.root_path)
|
|
|
|
|
original_folder = os.path.join(project_root, current_app.config['DEMO_ORIGINAL_FOLDER'])
|
|
|
|
|
perturbed_folder = os.path.join(project_root, current_app.config['DEMO_PERTURBED_FOLDER'])
|
|
|
|
|
comparison_folder = os.path.join(project_root, current_app.config['DEMO_COMPARISONS_FOLDER'])
|
|
|
|
|
|
|
|
|
|
original_count = len(glob.glob(os.path.join(original_folder, '*'))) if os.path.exists(original_folder) else 0
|
|
|
|
|
perturbed_count = len(glob.glob(os.path.join(perturbed_folder, '*'))) if os.path.exists(perturbed_folder) else 0
|
|
|
|
|
comparison_count = len(glob.glob(os.path.join(comparison_folder, '*'))) if os.path.exists(comparison_folder) else 0
|
|
|
|
|
|
|
|
|
|
# 统计数据库中的算法数量
|
|
|
|
|
perturbation_count = Perturbation.query.count()
|
|
|
|
|
finetune_count = Finetune.query.count()
|
|
|
|
|
total_algorithms = perturbation_count + finetune_count
|
|
|
|
|
|
|
|
|
|
return jsonify({
|
|
|
|
|
'demo_stats': {
|
|
|
|
|
'original_images': original_count,
|
|
|
|
|
'perturbed_images': perturbed_count,
|
|
|
|
|
'comparison_images': comparison_count,
|
|
|
|
|
'supported_algorithms': total_algorithms,
|
|
|
|
|
'perturbation_algorithms': perturbation_count,
|
|
|
|
|
'finetune_algorithms': finetune_count,
|
|
|
|
|
'evaluation_metrics': 4
|
|
|
|
|
}
|
|
|
|
|
}), 200
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
return jsonify({'error': f'获取统计信息失败: {str(e)}'}), 500
|