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