Kalbe-x-Bangkit commited on
Commit
9c412cb
1 Parent(s): 180d083

#Comment for detection section.

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