Kalbe-x-Bangkit commited on
Commit
5e28aaf
1 Parent(s): 9b4ee41

Combine detection button into sidebar.

Browse files
Files changed (1) hide show
  1. app.py +53 -52
app.py CHANGED
@@ -80,7 +80,7 @@ def draw_bbox(image, bbox):
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)
@@ -88,33 +88,33 @@ if uploaded_file is not None:
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
@@ -469,31 +469,32 @@ if uploaded_file is not None:
469
 
470
  with col2:
471
  if st.button('Auto Detect'):
472
- name = uploaded_file.name.split("/")[-1].split(".")[0]
473
- true_bbox_row = df[df['Image Index'] == uploaded_file.name]
474
-
475
- if not true_bbox_row.empty:
476
- x1, y1 = int(true_bbox_row['Bbox [x']), int(true_bbox_row['y'])
477
- x2, y2 = int(true_bbox_row['x_max']), int(true_bbox_row['y_max'])
478
- true_bbox = [x1, y1, x2, y2]
479
- label = true_bbox_row['Finding Label'].values[0]
480
-
481
- pred_bbox = predict(image)
482
- iou = cal_iou(true_bbox, pred_bbox)
483
-
484
- image = cv2.rectangle(image, (x1, y1), (x2, y2), (255, 0, 0), 5) # BLUE
485
- image = cv2.rectangle(image, (pred_bbox[0], pred_bbox[1]), (pred_bbox[2], pred_bbox[3]), (0, 0, 255), 5) # RED
486
-
487
- x_pos = int(image.shape[1] * 0.05)
488
- y_pos = int(image.shape[0] * 0.05)
489
- font_size = 0.7
490
-
491
- cv2.putText(image, f"IoU: {iou:.4f}", (x_pos, y_pos), cv2.FONT_HERSHEY_SIMPLEX, font_size, (255, 0, 0), 2)
492
- cv2.putText(image, f"Label: {label}", (x_pos, y_pos + 30), cv2.FONT_HERSHEY_SIMPLEX, font_size, (255, 255, 255), 2)
493
-
494
- st.image(image, channels="BGR")
495
  else:
496
- st.write("No bounding box and label found for this image.")
 
 
 
 
 
 
497
 
498
  with col3:
499
  if st.button('Generate Grad-CAM'):
 
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)
 
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
 
469
 
470
  with col2:
471
  if st.button('Auto Detect'):
472
+ st.write("Processing...")
473
+ input_image = preprocess_image(image)
474
+ pred_bbox, pred_label, pred_label_confidence = predict(model, input_image)
475
+
476
+ # Updated label mapping based on the dataset
477
+ label_mapping = {
478
+ 0: 'Atelectasis',
479
+ 1: 'Cardiomegaly',
480
+ 2: 'Effusion',
481
+ 3: 'Infiltrate',
482
+ 4: 'Mass',
483
+ 5: 'Nodule',
484
+ 6: 'Pneumonia',
485
+ 7: 'Pneumothorax'
486
+ }
487
+
488
+ if pred_label_confidence < 0.2:
489
+ st.write("May not detect a disease.")
 
 
 
 
 
490
  else:
491
+ pred_label_name = label_mapping[pred_label]
492
+ st.write(f"Prediction Label: {pred_label_name}")
493
+ st.write(f"Prediction Bounding Box: {pred_bbox}")
494
+ st.write(f"Prediction Confidence: {pred_label_confidence:.2f}")
495
+
496
+ output_image = draw_bbox(image.copy(), pred_bbox)
497
+ st.image(output_image, caption='Detected Image.', use_column_width=True)
498
 
499
  with col3:
500
  if st.button('Generate Grad-CAM'):