Kalbe-x-Bangkit commited on
Commit
f766128
1 Parent(s): 528572f

#Comment detection section.

Browse files
Files changed (1) hide show
  1. app.py +82 -82
app.py CHANGED
@@ -33,88 +33,88 @@ enhancement_type = st.sidebar.selectbox(
33
  ["Invert", "High Pass Filter", "Unsharp Masking", "Histogram Equalization", "CLAHE"]
34
  )
35
 
36
- H = 224
37
- W = 224
38
-
39
- @st.cache_resource
40
- def load_model():
41
- model = tf.keras.models.load_model("model-detection.h5", compile=False)
42
- model.compile(
43
- loss={
44
- "bbox": "mse",
45
- "class": "sparse_categorical_crossentropy"
46
- },
47
- optimizer=tf.keras.optimizers.Adam(),
48
- metrics={
49
- "bbox": ['mse'],
50
- "class": ['accuracy']
51
- }
52
- )
53
- return model
54
-
55
- def preprocess_image(image):
56
- """ Preprocess the image to the required size and normalization. """
57
- image = cv2.resize(image, (W, H))
58
- image = (image - 127.5) / 127.5 # Normalize to [-1, +1]
59
- image = np.expand_dims(image, axis=0).astype(np.float32)
60
- return image
61
-
62
- def predict(model, image):
63
- """ Predict bounding box and label for the input image. """
64
- pred_bbox, pred_class = model.predict(image)
65
- pred_label_confidence = np.max(pred_class, axis=1)[0]
66
- pred_label = np.argmax(pred_class, axis=1)[0]
67
- return pred_bbox[0], pred_label, pred_label_confidence
68
-
69
- def draw_bbox(image, bbox):
70
- """ Draw bounding box on the image. """
71
- h, w, _ = image.shape
72
- x1, y1, x2, y2 = bbox
73
- x1, y1, x2, y2 = int(x1 * w), int(y1 * h), int(x2 * w), int(y2 * h)
74
- image = cv2.rectangle(image, (x1, y1), (x2, y2), (255, 0, 0), 2)
75
- return image
76
-
77
- # st.title("Chest X-ray Disease Detection")
78
-
79
- # st.write("Upload a chest X-ray image and click on 'Detect' to find out if there's any disease.")
80
-
81
- model = load_model()
82
-
83
- uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
84
-
85
- if uploaded_file is not None:
86
- file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
87
- image = cv2.imdecode(file_bytes, 1)
88
-
89
- st.image(image, caption='Uploaded Image.', use_column_width=True)
90
-
91
- if st.button('Detect'):
92
- st.write("Processing...")
93
- input_image = preprocess_image(image)
94
- pred_bbox, pred_label, pred_label_confidence = predict(model, input_image)
95
-
96
- # Updated label mapping based on the dataset
97
- label_mapping = {
98
- 0: 'Atelectasis',
99
- 1: 'Cardiomegaly',
100
- 2: 'Effusion',
101
- 3: 'Infiltrate',
102
- 4: 'Mass',
103
- 5: 'Nodule',
104
- 6: 'Pneumonia',
105
- 7: 'Pneumothorax'
106
- }
107
-
108
- if pred_label_confidence < 0.2:
109
- st.write("May not detect a disease.")
110
- else:
111
- pred_label_name = label_mapping[pred_label]
112
- st.write(f"Prediction Label: {pred_label_name}")
113
- st.write(f"Prediction Bounding Box: {pred_bbox}")
114
- st.write(f"Prediction Confidence: {pred_label_confidence:.2f}")
115
-
116
- output_image = draw_bbox(image.copy(), pred_bbox)
117
- st.image(output_image, caption='Detected Image.', use_column_width=True)
118
 
119
 
120
  # Utility Functions
 
33
  ["Invert", "High Pass Filter", "Unsharp Masking", "Histogram Equalization", "CLAHE"]
34
  )
35
 
36
+ # H = 224
37
+ # W = 224
38
+
39
+ # @st.cache_resource
40
+ # def load_model():
41
+ # model = tf.keras.models.load_model("model-detection.h5", compile=False)
42
+ # model.compile(
43
+ # loss={
44
+ # "bbox": "mse",
45
+ # "class": "sparse_categorical_crossentropy"
46
+ # },
47
+ # optimizer=tf.keras.optimizers.Adam(),
48
+ # metrics={
49
+ # "bbox": ['mse'],
50
+ # "class": ['accuracy']
51
+ # }
52
+ # )
53
+ # return model
54
+
55
+ # def preprocess_image(image):
56
+ # """ Preprocess the image to the required size and normalization. """
57
+ # image = cv2.resize(image, (W, H))
58
+ # image = (image - 127.5) / 127.5 # Normalize to [-1, +1]
59
+ # image = np.expand_dims(image, axis=0).astype(np.float32)
60
+ # return image
61
+
62
+ # def predict(model, image):
63
+ # """ Predict bounding box and label for the input image. """
64
+ # pred_bbox, pred_class = model.predict(image)
65
+ # pred_label_confidence = np.max(pred_class, axis=1)[0]
66
+ # pred_label = np.argmax(pred_class, axis=1)[0]
67
+ # return pred_bbox[0], pred_label, pred_label_confidence
68
+
69
+ # def draw_bbox(image, bbox):
70
+ # """ Draw bounding box on the image. """
71
+ # h, w, _ = image.shape
72
+ # x1, y1, x2, y2 = bbox
73
+ # x1, y1, x2, y2 = int(x1 * w), int(y1 * h), int(x2 * w), int(y2 * h)
74
+ # image = cv2.rectangle(image, (x1, y1), (x2, y2), (255, 0, 0), 2)
75
+ # return image
76
+
77
+ # # st.title("Chest X-ray Disease Detection")
78
+
79
+ # # st.write("Upload a chest X-ray image and click on 'Detect' to find out if there's any disease.")
80
+
81
+ # model = load_model()
82
+
83
+ # uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
84
+
85
+ # if uploaded_file is not None:
86
+ # file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
87
+ # image = cv2.imdecode(file_bytes, 1)
88
+
89
+ # st.image(image, caption='Uploaded Image.', use_column_width=True)
90
+
91
+ # if st.button('Detect'):
92
+ # st.write("Processing...")
93
+ # input_image = preprocess_image(image)
94
+ # pred_bbox, pred_label, pred_label_confidence = predict(model, input_image)
95
+
96
+ # # Updated label mapping based on the dataset
97
+ # label_mapping = {
98
+ # 0: 'Atelectasis',
99
+ # 1: 'Cardiomegaly',
100
+ # 2: 'Effusion',
101
+ # 3: 'Infiltrate',
102
+ # 4: 'Mass',
103
+ # 5: 'Nodule',
104
+ # 6: 'Pneumonia',
105
+ # 7: 'Pneumothorax'
106
+ # }
107
+
108
+ # if pred_label_confidence < 0.2:
109
+ # st.write("May not detect a disease.")
110
+ # else:
111
+ # pred_label_name = label_mapping[pred_label]
112
+ # st.write(f"Prediction Label: {pred_label_name}")
113
+ # st.write(f"Prediction Bounding Box: {pred_bbox}")
114
+ # st.write(f"Prediction Confidence: {pred_label_confidence:.2f}")
115
+
116
+ # output_image = draw_bbox(image.copy(), pred_bbox)
117
+ # st.image(output_image, caption='Detected Image.', use_column_width=True)
118
 
119
 
120
  # Utility Functions