Switch to unified view

a b/static/js/serverRequests.js
1
/**************************************************************************************
2
 * Project     : Chest X-Ray Pathology Detection and Localization using Deep Learning
3
 * Author Name : Rammuni Ravidu Suien Silva
4
 * UoW No      : 16267097
5
 * IIT No      : 2016134
6
 * Module      : Final Year Project 20/21
7
 * Supervisor  : Mr Pumudu Fernando
8
9
 * Prototype    : Web Interface - FrontEnd
10
 * File         : Server AJAX requests functions
11
 * University of Westminster, UK || IIT Sri Lanka
12
 **************************************************************************************/
13
14
15
// Image upload front-end
16
// Initialization of page
17
jQuery(function () {
18
    $('.image-section').hide();
19
    $('.loader').hide();
20
    detResults.hide();
21
22
    // Uploaded CXR Preview
23
    function readURL(input) {
24
        if (input.files && input.files[0]) {
25
            $('.img-preview').each(function (i, row_el) {
26
                if (i >= input.files.length || !input.files[i].type.match('image.*')) {
27
                    return false;
28
                }
29
30
                let reader = new FileReader();
31
                let cxrPreviewId = "#cxr-preview-" + i;
32
33
                $(this).css('display', 'inline-block');
34
                reader.onload = function (e) {
35
                    $(cxrPreviewId).css('background-image', 'url(' + e.target.result + ')');
36
                    $(cxrPreviewId).hide();
37
                    $(cxrPreviewId).fadeIn(650);
38
                }
39
                reader.readAsDataURL(input.files[i]);
40
            });
41
        }
42
    }
43
44
    // Image upload button events and animations
45
    $("#imageUpload").on("change", function () {
46
        if ($('#imageUpload').get(0).files.length !== 0) {
47
            $('.image-section').show();
48
            $('.cxr-preview').css('background-image', '');
49
            $('#btn-detect').show();
50
51
            $('.img-preview').hide();
52
            $('#modelSelect').prop('disabled', false)
53
            $('#loader_localized').hide();
54
            $('#btn-save').hide();
55
            $('#btn-localize').hide();
56
            $('.det-row').off("click")
57
            $(".cxr-loc-img").attr('src', "");
58
            detResults.text('');
59
            detResults.hide();
60
            localized = false;
61
62
            readURL(this);
63
        }
64
65
    });
66
67
    // Detection results request
68
    $('#btn-detect').on("click", function () {
69
        $('#imageUpload').prop("disabled", true);
70
71
        let form_data = new FormData();
72
        // Read selected files
73
        let totalFiles = document.getElementById('imageUpload').files.length;
74
        for (let index = 0; index < totalFiles; index++) {
75
            form_data.append("file_" + index, document.getElementById('imageUpload').files[index]);
76
        }
77
78
        // Model request link
79
        let model_id = $("#modelSelect").val()
80
        let urlLink = '/predict/' + model_id
81
        $('#modelSelect').prop('disabled', true)
82
83
        // Show loading animation
84
        $(this).hide();
85
        $('#loader_ani').show();
86
87
        // Requesting the detection results by calling api /predict (ajax POST call)
88
        $.ajax({
89
            type: 'POST',
90
            url: urlLink,
91
            data: form_data,
92
            dataType: 'json',
93
            contentType: false,
94
            cache: false,
95
            processData: false,
96
            async: true,
97
            success: function (data) {
98
                // Displaying detection results
99
                $('#loader_ani').hide();
100
                $('#btn-save').show();
101
                detResults.fadeIn(600);
102
                // Call for the creation of the detection results table
103
                $(cxrResultsDisplayTable(data)).appendTo('#result');
104
                console.log('Detection DONE!');
105
                $('#btn-localize').show();
106
                $('#imageUpload').prop("disabled", false);
107
            },
108
            error: function () {
109
                $('#loader_ani').hide();
110
                alert("Couldn't Scan CXR Image");
111
            },
112
        });
113
        $.ajax({
114
            type: 'GET',
115
            url: '/get_symptoms',
116
            dataType: 'json',
117
            contentType: false,
118
            cache: false,
119
            processData: false,
120
            async: true,
121
            success: function (data) {
122
                symptoms_json = data
123
            },
124
            error: function () {
125
                alert("Couldn't Load Symptoms");
126
            },
127
        });
128
    });
129
130
    // Localization function
131
    $('#btn-localize').on("click", function () {
132
        setLoaderIcon();
133
134
        $('#imageUpload').prop("disabled", true);
135
136
        // Show loading animation
137
        $(this).hide();
138
        $('#loader_ani_localize').show();
139
        $('#btn-save').prop("disabled", true);
140
141
        // Initiating the localization
142
        $.ajax({
143
            url: '/localize',
144
            dataType: 'text',
145
            contentType: false,
146
            cache: false,
147
            processData: false,
148
            async: true,
149
            success: function (data) {
150
                $('#imageUpload').prop("disabled", false);
151
152
                // Displaying detection results
153
                localized = true
154
155
                // Setting initial localized images
156
                $('.det-row').each(function (i, row_el) {
157
                    let pathology_id = i;
158
                    let cxr_popup_img_id = ".cxrPopupImg-" + pathology_id;
159
                    srcAdd(pathology_id, cxr_popup_img_id);
160
                })
161
162
                $('#loader_ani_localize').hide();
163
                detResults.fadeIn(600);
164
                localizationPathAdd(data);
165
166
                $('#loader_localized').show();
167
                $('#btn-save').prop("disabled", false);
168
                console.log('Path adding DONE!');
169
            },
170
            error: function () {
171
                $('#loader_ani_localize').hide();
172
                alert("Couldn't Localize CXR");
173
            },
174
        });
175
    });
176
177
    $('#btn-save').on("click", function () {
178
        printResults();
179
    });
180
181
});