[addb71]: / routes / main.py

Download this file

62 lines (48 with data), 2.0 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from flask import Blueprint, request, jsonify, render_template
import sys
import os
import cv2 # Added import
from utils.segmentation import segment_lung
from utils.classification import classify_disease
from utils.report_generator import generate_report
# Ensure directories exist
os.makedirs('app/static/uploaded_images', exist_ok=True)
os.makedirs('app/static/output_images', exist_ok=True)
main = Blueprint('main', __name__)
@main.route('/', methods=['GET'])
def index():
return render_template('index.html')
@main.route('/analyze', methods=['POST'])
def analyze():
try:
file = request.files['xray']
name = request.form['name']
age = request.form['age']
gender = request.form['gender']
# Create paths using os.path.join for cross-platform compatibility
upload_dir = 'app/static/uploaded_images'
output_dir = 'app/static/output_images'
xray_path = os.path.join(upload_dir, file.filename)
mask_path = os.path.join(output_dir, f'mask_{file.filename}')
report_path = os.path.join(output_dir, f'report_{os.path.splitext(file.filename)[0]}.pdf')
file.save(xray_path)
# Segment the lung
mask = segment_lung(xray_path)
cv2.imwrite(mask_path, mask)
# Classify disease
disease, confidence, severity = classify_disease(xray_path)
# Generate report
generate_report(name, age, gender, xray_path, mask_path, disease, severity, report_path)
return jsonify({
"success": True,
"disease": disease,
"severity": severity,
"confidence": f"{confidence:.2f}",
"segmented_image": f'mask_{file.filename}',
"pdf_report": f'report_{os.path.splitext(file.filename)[0]}.pdf'
})
except Exception as e:
return jsonify({
"success": False,
"error": str(e)
}), 500