ujan2003 commited on
Commit
d1d11ed
·
verified ·
1 Parent(s): 538d35e

Upload 5 files

Browse files
Files changed (5) hide show
  1. app.py +53 -0
  2. index.html +19 -0
  3. script.js +29 -0
  4. styles.css +35 -0
  5. trained.h5 +3 -0
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify
2
+ import tensorflow as tf
3
+ import numpy as np
4
+ from tensorflow.keras.preprocessing import image
5
+ import os
6
+ from werkzeug.utils import secure_filename
7
+
8
+ app = Flask(__name__)
9
+
10
+ # Load trained model
11
+ model = tf.keras.models.load_model("trained.h5")
12
+
13
+ # Define allowed extensions
14
+ ALLOWED_EXTENSIONS = {"png", "jpg", "jpeg"}
15
+
16
+ def allowed_file(filename):
17
+ return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
18
+
19
+ def preprocess_image(img_path):
20
+ img = image.load_img(img_path, target_size=(150, 150)) # Resize to model input size
21
+ img_array = image.img_to_array(img) / 255.0 # Normalize
22
+ img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
23
+ return img_array
24
+
25
+ @app.route("/predict", methods=["POST"])
26
+ def predict():
27
+ if "file" not in request.files:
28
+ return jsonify({"error": "No file uploaded"}), 400
29
+
30
+ file = request.files["file"]
31
+
32
+ if file.filename == "":
33
+ return jsonify({"error": "No selected file"}), 400
34
+
35
+ if file and allowed_file(file.filename):
36
+ filename = secure_filename(file.filename)
37
+ file_path = os.path.join("uploads", filename)
38
+ file.save(file_path)
39
+
40
+ img_array = preprocess_image(file_path)
41
+ prediction = model.predict(img_array)[0][0]
42
+
43
+ os.remove(file_path) # Clean up
44
+
45
+ result = "Pneumonia Detected" if prediction > 0.5 else "No Pneumonia"
46
+ confidence = float(prediction) if prediction > 0.5 else 1 - float(prediction)
47
+
48
+ return jsonify({"result": result, "confidence": round(confidence * 100, 2)})
49
+
50
+ return jsonify({"error": "Invalid file format"}), 400
51
+
52
+ if __name__ == "__main__":
53
+ app.run(debug=True)
index.html ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Pneumonia Detection</title>
7
+ <link rel="stylesheet" href="styles.css">
8
+ </head>
9
+ <body>
10
+ <div class="container">
11
+ <h1>Pneumonia Detection from X-ray</h1>
12
+ <input type="file" id="fileInput" accept="image/*">
13
+ <button onclick="uploadImage()">Upload & Predict</button>
14
+ <div id="result"></div>
15
+ </div>
16
+
17
+ <script src="script.js"></script>
18
+ </body>
19
+ </html>
script.js ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function uploadImage() {
2
+ let fileInput = document.getElementById("fileInput");
3
+ let file = fileInput.files[0];
4
+
5
+ if (!file) {
6
+ alert("Please select an image file.");
7
+ return;
8
+ }
9
+
10
+ let formData = new FormData();
11
+ formData.append("file", file);
12
+
13
+ fetch("http://127.0.0.1:5000/predict", {
14
+ method: "POST",
15
+ body: formData
16
+ })
17
+ .then(response => response.json())
18
+ .then(data => {
19
+ if (data.error) {
20
+ document.getElementById("result").innerText = "Error: " + data.error;
21
+ } else {
22
+ document.getElementById("result").innerText = `Result: ${data.result} (Confidence: ${data.confidence}%)`;
23
+ }
24
+ })
25
+ .catch(error => {
26
+ console.error("Error:", error);
27
+ document.getElementById("result").innerText = "Prediction failed.";
28
+ });
29
+ }
styles.css ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ body {
2
+ font-family: Arial, sans-serif;
3
+ background: #f8f9fa;
4
+ text-align: center;
5
+ padding: 50px;
6
+ }
7
+
8
+ .container {
9
+ background: white;
10
+ padding: 20px;
11
+ border-radius: 8px;
12
+ box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
13
+ width: 300px;
14
+ margin: auto;
15
+ }
16
+
17
+ button {
18
+ background: #007bff;
19
+ color: white;
20
+ border: none;
21
+ padding: 10px 15px;
22
+ cursor: pointer;
23
+ margin-top: 10px;
24
+ border-radius: 5px;
25
+ }
26
+
27
+ button:hover {
28
+ background: #0056b3;
29
+ }
30
+
31
+ #result {
32
+ margin-top: 20px;
33
+ font-size: 18px;
34
+ font-weight: bold;
35
+ }
trained.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a829fd6b8fcb8c6f8be5849e73a88f2a4c7dd9a4d303e4ff0f3b5ae7b21ebe79
3
+ size 23877888