samithcs commited on
Commit
994170a
·
verified ·
1 Parent(s): 1f97e05

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -26
app.py CHANGED
@@ -6,49 +6,60 @@ import cv2
6
  from keras.models import load_model
7
  from util import set_background
8
 
9
- bg = cv2.imread('./bgs/bg5.png')
10
 
11
- blurred_bg = cv2.GaussianBlur(bg, (15, 15), 0)
 
 
12
 
13
- cv2.imwrite('./bgs/bg5_blur.png', blurred_bg)
14
 
15
- # Set background
16
- set_background('./bgs/bg5_blur.png')
 
 
 
 
 
 
17
 
18
- # Set title and header
19
- st.title('🩺 Pneumonia Classifier Application')
20
- st.header('Upload a Chest X-ray Image')
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
- # Upload file
23
- file = st.file_uploader(
24
- "Upload a chest X-ray image",
25
- type=['jpeg', 'jpg', 'png'],
26
- label_visibility="visible"
27
- )
28
 
29
- # Load model
30
- model_path = os.path.join(os.path.dirname(__file__), 'model', 'pneumonia_classifier.keras')
31
- model = load_model(model_path)
32
 
33
 
34
- class_names = ['NORMAL', 'PNEUMONIA']
 
 
 
 
35
 
36
- # Display image and classify
37
  if file is not None:
38
- image = Image.open(file).convert('L')
39
  st.image(image, caption="Uploaded X-ray", use_container_width=True)
40
 
41
-
42
  img = np.array(image)
43
  img = cv2.resize(img, (128, 128))
44
  img = img / 255.0
45
- img = np.expand_dims(img, axis=(0, -1))
46
 
47
- # Predict
48
  prediction = model.predict(img)
49
  class_idx = np.argmax(prediction)
50
  confidence = np.max(prediction)
51
 
52
- # Display results
53
- st.write("## Prediction: {}".format(class_names[class_idx]))
54
- st.write("### Confidence: {:.2f}%".format(confidence * 100))
 
6
  from keras.models import load_model
7
  from util import set_background
8
 
 
9
 
10
+ st.set_page_config(page_title="Pneumonia Classifier", layout="centered")
11
+ st.title("🩺 Pneumonia Classifier Application")
12
+ st.header("Upload a Chest X-ray Image")
13
 
 
14
 
15
+ @st.cache_resource
16
+ def load_bg():
17
+ bg_path = os.path.join(os.path.dirname(__file__), "bgs", "bg5.png")
18
+ bg = cv2.imread(bg_path)
19
+ if bg is None:
20
+ return None
21
+ blurred = cv2.GaussianBlur(bg, (15, 15), 0)
22
+ return blurred
23
 
24
+ @st.cache_resource
25
+ def load_pneumonia_model():
26
+ model_path = os.path.join(
27
+ os.path.dirname(__file__),
28
+ "model",
29
+ "pneumonia_classifier.keras"
30
+ )
31
+ return load_model(model_path)
32
+
33
+
34
+ bg_img = load_bg()
35
+ if bg_img is not None:
36
+ tmp_bg = "/tmp/bg_blur.png"
37
+ cv2.imwrite(tmp_bg, bg_img)
38
+ set_background(tmp_bg)
39
 
 
 
 
 
 
 
40
 
41
+ model = load_pneumonia_model()
42
+ class_names = ["NORMAL", "PNEUMONIA"]
 
43
 
44
 
45
+ file = st.file_uploader(
46
+ "Upload a chest X-ray image",
47
+ type=["jpeg", "jpg", "png"]
48
+ )
49
+
50
 
 
51
  if file is not None:
52
+ image = Image.open(file).convert("L")
53
  st.image(image, caption="Uploaded X-ray", use_container_width=True)
54
 
 
55
  img = np.array(image)
56
  img = cv2.resize(img, (128, 128))
57
  img = img / 255.0
58
+ img = np.expand_dims(img, axis=(0, -1))
59
 
 
60
  prediction = model.predict(img)
61
  class_idx = np.argmax(prediction)
62
  confidence = np.max(prediction)
63
 
64
+ st.success(f"Prediction: {class_names[class_idx]}")
65
+ st.info(f"Confidence: {confidence * 100:.2f}%")