Kalbe-x-Bangkit
commited on
Commit
•
79be1c5
1
Parent(s):
25f93ce
Get out from integration detection.
Browse files
app.py
CHANGED
@@ -57,10 +57,10 @@ def load_model_detection():
|
|
57 |
|
58 |
def preprocess_image(image):
|
59 |
""" Preprocess the image to the required size and normalization. """
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
|
65 |
def predict(model_detection, image):
|
66 |
""" Predict bounding box and label for the input image. """
|
@@ -85,13 +85,41 @@ def draw_bbox(image, bbox):
|
|
85 |
|
86 |
model_detection = load_model_detection()
|
87 |
|
88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
|
90 |
-
|
91 |
-
|
92 |
-
|
|
|
|
|
|
|
|
|
93 |
|
94 |
-
|
|
|
95 |
|
96 |
@st.cache_resource
|
97 |
def load_gradcam_model():
|
@@ -494,35 +522,35 @@ if uploaded_file is not None:
|
|
494 |
redirect_button("https://new-ohif-viewer-k7c3gdlxua-et.a.run.app/")
|
495 |
|
496 |
with col2:
|
497 |
-
file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
|
498 |
-
image = cv2.imdecode(file_bytes, 1)
|
499 |
if st.button('Auto Detect'):
|
500 |
-
st.write("Processing...")
|
501 |
-
input_image = preprocess_image(image)
|
502 |
-
pred_bbox, pred_label, pred_label_confidence = predict(model_detection, input_image)
|
503 |
|
504 |
-
# Updated label mapping based on the dataset
|
505 |
-
label_mapping = {
|
506 |
-
|
507 |
-
|
508 |
-
|
509 |
-
|
510 |
-
|
511 |
-
|
512 |
-
|
513 |
-
|
514 |
-
}
|
515 |
|
516 |
-
if pred_label_confidence < 0.2:
|
517 |
-
|
518 |
-
else:
|
519 |
-
|
520 |
-
|
521 |
-
|
522 |
-
|
523 |
|
524 |
-
|
525 |
-
|
526 |
|
527 |
|
528 |
# if st.button('Auto Detect'):
|
|
|
57 |
|
58 |
def preprocess_image(image):
|
59 |
""" Preprocess the image to the required size and normalization. """
|
60 |
+
image = cv2.resize(image, (W_detection, H_detection))
|
61 |
+
image = (image - 127.5) / 127.5 # Normalize to [-1, +1]
|
62 |
+
image = np.expand_dims(image, axis=0).astype(np.float32)
|
63 |
+
return image
|
64 |
|
65 |
def predict(model_detection, image):
|
66 |
""" Predict bounding box and label for the input image. """
|
|
|
85 |
|
86 |
model_detection = load_model_detection()
|
87 |
|
88 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
89 |
+
|
90 |
+
if uploaded_file is not None:
|
91 |
+
file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
|
92 |
+
image = cv2.imdecode(file_bytes, 1)
|
93 |
+
|
94 |
+
st.image(image, caption='Uploaded Image.', use_column_width=True)
|
95 |
+
|
96 |
+
if st.button('Detect'):
|
97 |
+
st.write("Processing...")
|
98 |
+
input_image = preprocess_image(image)
|
99 |
+
pred_bbox, pred_label, pred_label_confidence = predict(model_detection, input_image)
|
100 |
+
|
101 |
+
# Updated label mapping based on the dataset
|
102 |
+
label_mapping = {
|
103 |
+
0: 'Atelectasis',
|
104 |
+
1: 'Cardiomegaly',
|
105 |
+
2: 'Effusion',
|
106 |
+
3: 'Infiltrate',
|
107 |
+
4: 'Mass',
|
108 |
+
5: 'Nodule',
|
109 |
+
6: 'Pneumonia',
|
110 |
+
7: 'Pneumothorax'
|
111 |
+
}
|
112 |
|
113 |
+
if pred_label_confidence < 0.2:
|
114 |
+
st.write("May not detect a disease.")
|
115 |
+
else:
|
116 |
+
pred_label_name = label_mapping[pred_label]
|
117 |
+
st.write(f"Prediction Label: {pred_label_name}")
|
118 |
+
st.write(f"Prediction Bounding Box: {pred_bbox}")
|
119 |
+
st.write(f"Prediction Confidence: {pred_label_confidence:.2f}")
|
120 |
|
121 |
+
output_image = draw_bbox(image.copy(), pred_bbox)
|
122 |
+
st.image(output_image, caption='Detected Image.', use_column_width=True)
|
123 |
|
124 |
@st.cache_resource
|
125 |
def load_gradcam_model():
|
|
|
522 |
redirect_button("https://new-ohif-viewer-k7c3gdlxua-et.a.run.app/")
|
523 |
|
524 |
with col2:
|
525 |
+
# file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
|
526 |
+
# image = cv2.imdecode(file_bytes, 1)
|
527 |
if st.button('Auto Detect'):
|
528 |
+
# st.write("Processing...")
|
529 |
+
# input_image = preprocess_image(image)
|
530 |
+
# pred_bbox, pred_label, pred_label_confidence = predict(model_detection, input_image)
|
531 |
|
532 |
+
# # Updated label mapping based on the dataset
|
533 |
+
# label_mapping = {
|
534 |
+
# 0: 'Atelectasis',
|
535 |
+
# 1: 'Cardiomegaly',
|
536 |
+
# 2: 'Effusion',
|
537 |
+
# 3: 'Infiltrate',
|
538 |
+
# 4: 'Mass',
|
539 |
+
# 5: 'Nodule',
|
540 |
+
# 6: 'Pneumonia',
|
541 |
+
# 7: 'Pneumothorax'
|
542 |
+
# }
|
543 |
|
544 |
+
# if pred_label_confidence < 0.2:
|
545 |
+
# st.write("May not detect a disease.")
|
546 |
+
# else:
|
547 |
+
# pred_label_name = label_mapping[pred_label]
|
548 |
+
# st.write(f"Prediction Label: {pred_label_name}")
|
549 |
+
# st.write(f"Prediction Bounding Box: {pred_bbox}")
|
550 |
+
# st.write(f"Prediction Confidence: {pred_label_confidence:.2f}")
|
551 |
|
552 |
+
# output_image = draw_bbox(image.copy(), pred_bbox)
|
553 |
+
# st.image(output_image, caption='Detected Image.', use_column_width=True)
|
554 |
|
555 |
|
556 |
# if st.button('Auto Detect'):
|