a b/docpat/views.py
1
from django.shortcuts import render
2
import pickle
3
import os
4
from django.core.files.storage import FileSystemStorage
5
import numpy as np
6
import tensorflow
7
from tensorflow.keras.models import load_model
8
from tensorflow.keras.preprocessing.image import load_img, img_to_array
9
import os
10
from django.conf import settings
11
from xc import predict_image  # Import the function from c.py
12
13
14
# Path to your single image
15
16
def load_model(file_path):
17
    if not os.path.exists(file_path):
18
        raise FileNotFoundError(f"The model file at {file_path} does not exist.")
19
    with open(file_path, 'rb') as file:
20
        model = pickle.load(file)
21
    return model
22
def homepage(request):
23
        return render(request,"main.html")
24
def breast(request):
25
     return render(request,"breast.html")
26
def faq(request):
27
        return render(request,"faqs.html")
28
29
def heart(request):
30
        return render(request,"heart.html")
31
32
    
33
def liver(request):
34
        return render(request,"liver.html")
35
36
    
37
def diabetes(request):
38
        return render(request,"diabetes.html")
39
def predictb(request):
40
    if request.method == 'POST':
41
        try:
42
            model_path = r'breast_cancer_svm_model.pkl'
43
            breast_model = load_model(model_path)
44
45
            texture_mean = float(request.POST.get('texture_mean'))
46
            smoothness_mean = float(request.POST.get('smoothness_mean'))
47
            compactness_mean = float(request.POST.get('compactness_mean'))
48
            concave_points_mean = float(request.POST.get('concave_points_mean'))
49
            symmetry_mean = float(request.POST.get('symmetry_mean'))
50
            fractal_dimension_mean = float(request.POST.get('fractal_dimension_mean'))
51
            texture_se = float(request.POST.get('texture_se'))
52
            area_se = float(request.POST.get('area_se'))
53
            smoothness_se = float(request.POST.get('smoothness_se'))
54
            compactness_se = float(request.POST.get('compactness_se'))
55
            concavity_se = float(request.POST.get('concavity_se'))
56
            concave_points_se = float(request.POST.get('concave_points_se'))
57
            symmetry_se = float(request.POST.get('symmetry_se'))
58
            fractal_dimension_se =float(request.POST.get('fractal_dimension_se'))
59
            texture_worst = float(request.POST.get('texture_worst'))
60
            area_worst = float(request.POST.get('area_worst'))
61
            smoothness_worst = float(request.POST.get('smoothness_worst'))
62
            compactness_worst = float(request.POST.get('compactness_worst'))
63
            concavity_worst = float(request.POST.get('concavity_worst'))
64
            concave_points_worst = float(request.POST.get('concave_points_worst'))
65
            symmetry_worst = float(request.POST.get('symmetry_worst'))
66
            fractal_dimension_worst = float(request.POST.get('fractal_dimension_worst'))
67
            data = [ texture_mean, smoothness_mean, compactness_mean,
68
                     concave_points_mean, symmetry_mean, fractal_dimension_mean,  texture_se,
69
                     area_se, smoothness_se, compactness_se, concavity_se, concave_points_se, symmetry_se,
70
                    fractal_dimension_se,  texture_worst,  area_worst, smoothness_worst,
71
                    compactness_worst, concavity_worst, concave_points_worst, symmetry_worst, fractal_dimension_worst]
72
            prediction_result = breast_model.predict([data])
73
            context = {
74
                'prediction_result': prediction_result[0]  
75
            }
76
            return render(request, 'predict.html', context)
77
78
        except Exception as e:
79
            return render(request, 'error.html', {'error': str(e)}) 
80
        
81
def predictd(request):
82
    if request.method == 'POST':
83
        try:
84
            model_path = r'diabetes_model.pkl'
85
            diabetes_model = load_model(model_path)
86
87
            Pregnancies = float(request.POST.get('Pregnancies'))
88
            Glucose =float(request.POST.get('Glucose'))
89
            BloodPressure = float(request.POST.get('BloodPressure'))
90
            SkinThickness = float(request.POST.get('SkinThickness'))
91
            Insulin = float(request.POST.get('Insulin'))
92
            BMI = float(request.POST.get('BMI'))
93
            DiabetesPedigreeFunction = float(request.POST.get('DiabetesPedigreeFunction'))
94
            Age = float(request.POST.get('Age'))
95
            inputs = [[Pregnancies, Glucose, BloodPressure, SkinThickness, Insulin, 
96
                   BMI, DiabetesPedigreeFunction, Age 
97
                   ]]
98
            prediction_result = diabetes_model.predict(inputs)
99
100
101
            context = {
102
                'prediction_result': prediction_result[0]  
103
            }
104
            return render(request, 'predict.html', context)
105
106
        except Exception as e:
107
            return render(request, 'error.html', {'error': str(e)}) 
108
109
def predictl(request):
110
    if request.method == 'POST':
111
        try:
112
            model_path = r'liver_prediction.pkl'
113
            liver_model = load_model(model_path)
114
            age = float(request.POST.get('Age'))
115
            gender = float(request.POST.get('Gender'))
116
            total_bilirubin = float(request.POST.get('Total_Bilirubin'))
117
            direct_bilirubin = float(request.POST.get('Direct_Bilirubin'))
118
            alkaline_phosphotase = float(request.POST.get('Alkaline_Phosphotase'))
119
            alamine_aminotransferase = float(request.POST.get('Alamine_Aminotransferase'))
120
            aspartate_aminotransferase = float(request.POST.get('Aspartate_Aminotransferase'))
121
            total_proteins = float(request.POST.get('Total_Protiens'))
122
            albumin = float(request.POST.get('Albumin'))
123
            albumin_and_globulin_ratio = float(request.POST.get('Albumin_and_Globulin_Ratio'))
124
125
            inputs = [[age, gender, total_bilirubin, direct_bilirubin, alkaline_phosphotase, 
126
                   alamine_aminotransferase, aspartate_aminotransferase, total_proteins, 
127
                   albumin, albumin_and_globulin_ratio]]
128
        
129
            
130
            prediction_result =liver_model.predict(inputs)
131
132
133
            context = {
134
                'prediction_result': prediction_result[0]  
135
            }
136
            return render(request, 'predict.html', context)
137
138
        except Exception as e:
139
            return render(request, 'error.html', {'error': str(e)}) 
140
def predicth(request):
141
    if request.method == 'POST':
142
        try:
143
            model_path = r'heart_disease_model.pkl'
144
            heart_model =(load_model(model_path))
145
            age = float(request.POST.get('age'))
146
            sex = float(request.POST.get('sex'))
147
            cp = float(request.POST.get('cp'))
148
            trestbps = float(request.POST.get('trestbps'))
149
            chol = float(request.POST.get('chol'))
150
            fbs = float(request.POST.get('fbs'))
151
            restecg = float(request.POST.get('restecg'))
152
            thalach = float(request.POST.get('thalach'))
153
            exang = float(request.POST.get('exang'))
154
            oldpeak = float(request.POST.get('oldpeak'))
155
            slope = float(request.POST.get('slope'))
156
            ca = float(request.POST.get('ca'))
157
            thal = float(request.POST.get('thal'))
158
            inputs = [[age, sex,cp,trestbps, chol, fbs, restecg, 
159
                   thalach, exang, oldpeak, 
160
                   slope, ca,thal]]
161
            
162
            prediction_result = heart_model.predict(inputs)
163
164
165
            context = {
166
                'prediction_result': prediction_result[0] 
167
            }
168
            return render(request, 'predict.html', context)
169
170
        except Exception as e:
171
            return render(request, 'error.html', {'error': str(e)}) 
172
def gi(request):
173
    return render(request, 'GI_diseases.html')
174
# View to handle file upload and prediction
175
# views.py
176
177
178
def predictg(request):
179
    if request.method == 'POST' and request.FILES.get('uploadedImage'):
180
        # Handle the uploaded file
181
        uploaded_file = request.FILES['uploadedImage']
182
        file_name = uploaded_file.name
183
184
        # Save the uploaded file temporarily
185
        fs = FileSystemStorage()
186
        file_path = fs.save(file_name, uploaded_file)
187
        full_file_path = fs.path(file_path)
188
189
        # Debugging: Ensure the file path is correct
190
        print(f"File uploaded to: {full_file_path}")
191
192
        # Call the predict_image function from c.py to process the image
193
        predicted_class = predict_image(full_file_path)
194
195
        if predicted_class is not None:
196
            print(f"Predicted class: {predicted_class}")  # Debugging: print the predicted class
197
            return render(request, 'p.html', {
198
                'predicted_class': predicted_class  # Only send predicted class
199
            })
200
        else:
201
            print("Prediction failed")  # Debugging: print if prediction failed
202
            return render(request, 'error.html', {'error': 'Failed to process the image or make predictions.'})
203
204
    else:
205
        return render(request, 'p.html') 
206
def GI_Diseases_info(request):
207
    return render(request,"gi.html")
208
def diabetes_info(request):
209
    return render(request,"diabetes_info.html")
210
def breast_cancer_info(request):
211
    return render(request,'breast_cancer_info.html')
212
def heart_disease_info(request):
213
    return render(request,'heart_disease_info.html')
214
def liver_disease_info(request):
215
    return render(request,'liver_disease_info.html')