Kalbe-x-Bangkit commited on
Commit
dc50c8f
1 Parent(s): 6dd868b

Rearrange detection.

Browse files
Files changed (1) hide show
  1. app.py +37 -23
app.py CHANGED
@@ -17,6 +17,7 @@ from datetime import datetime
17
  from tensorflow import image
18
  from tensorflow.python.keras.models import load_model
19
  from keras.applications.densenet import DenseNet121
 
20
  from pydicom.pixel_data_handlers.util import apply_voi_lut
21
 
22
  # Environment Configuration
@@ -34,13 +35,13 @@ enhancement_type = st.sidebar.selectbox(
34
  ["Invert", "High Pass Filter", "Unsharp Masking", "Histogram Equalization", "CLAHE"]
35
  )
36
 
37
- H = 224
38
- W = 224
39
 
40
  @st.cache_resource
41
  def load_model_detection():
42
- model = tf.keras.models.load_model("model-detection.h5", compile=False)
43
- model.compile(
44
  loss={
45
  "bbox": "mse",
46
  "class": "sparse_categorical_crossentropy"
@@ -51,17 +52,30 @@ def load_model_detection():
51
  "class": ['accuracy']
52
  }
53
  )
54
- return model
55
 
56
  def preprocess_image(image):
57
- # """ Preprocess the image to the required size and normalization. """
58
- # W = 320
59
- # H = 320
60
- image = cv2.resize(image, (W, H))
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
 
66
 
67
  # st.title("Chest X-ray Disease Detection")
@@ -369,20 +383,20 @@ def redirect_button(url):
369
  ###########################################################################################
370
 
371
 
372
- def predict(model_detection, image):
373
- """ Predict bounding box and label for the input image. """
374
- pred_bbox, pred_class = model_detection.predict(image)
375
- pred_label_confidence = np.max(pred_class, axis=1)[0]
376
- pred_label = np.argmax(pred_class, axis=1)[0]
377
- return pred_bbox[0], pred_label, pred_label_confidence
378
 
379
- def draw_bbox(image, bbox):
380
- """ Draw bounding box on the image. """
381
- h, w, _ = image.shape
382
- x1, y1, x2, y2 = bbox
383
- x1, y1, x2, y2 = int(x1 * w), int(y1 * h), int(x2 * w), int(y2 * h)
384
- image = cv2.rectangle(image, (x1, y1), (x2, y2), (255, 0, 0), 2)
385
- return image
386
 
387
 
388
  ###########################################################################################
@@ -480,7 +494,7 @@ if uploaded_file is not None:
480
 
481
  with col2:
482
  file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
483
- image = cv2.imdecode(file_bytes, 1)
484
  if st.button('Auto Detect'):
485
  st.write("Processing...")
486
  input_image = preprocess_image(image)
 
17
  from tensorflow import image
18
  from tensorflow.python.keras.models import load_model
19
  from keras.applications.densenet import DenseNet121
20
+ from keras.layers import Dense, GlobalAveragePooling2D
21
  from pydicom.pixel_data_handlers.util import apply_voi_lut
22
 
23
  # Environment Configuration
 
35
  ["Invert", "High Pass Filter", "Unsharp Masking", "Histogram Equalization", "CLAHE"]
36
  )
37
 
38
+ H_detection = 224
39
+ W_detection = 224
40
 
41
  @st.cache_resource
42
  def load_model_detection():
43
+ model_detection = tf.keras.models.load_model("files/model-detection.h5", compile=False)
44
+ model_detection.compile(
45
  loss={
46
  "bbox": "mse",
47
  "class": "sparse_categorical_crossentropy"
 
52
  "class": ['accuracy']
53
  }
54
  )
55
+ return model_detection
56
 
57
  def preprocess_image(image):
58
+ """ Preprocess the image to the required size and normalization. """
59
+ image = cv2.resize(image, (W_detection, H_detection))
 
 
60
  image = (image - 127.5) / 127.5 # Normalize to [-1, +1]
61
  image = np.expand_dims(image, axis=0).astype(np.float32)
62
  return image
63
 
64
+ def predict(model_detection, image):
65
+ """ Predict bounding box and label for the input image. """
66
+ pred_bbox, pred_class = model_detection.predict(image)
67
+ pred_label_confidence = np.max(pred_class, axis=1)[0]
68
+ pred_label = np.argmax(pred_class, axis=1)[0]
69
+ return pred_bbox[0], pred_label, pred_label_confidence
70
+
71
+ def draw_bbox(image, bbox):
72
+ """ Draw bounding box on the image. """
73
+ h, w, _ = image.shape
74
+ x1, y1, x2, y2 = bbox
75
+ x1, y1, x2, y2 = int(x1 * w), int(y1 * h), int(x2 * w), int(y2 * h)
76
+ image = cv2.rectangle(image, (x1, y1), (x2, y2), (255, 0, 0), 2)
77
+ return image
78
+
79
 
80
 
81
  # st.title("Chest X-ray Disease Detection")
 
383
  ###########################################################################################
384
 
385
 
386
+ # def predict(model_detection, image):
387
+ # """ Predict bounding box and label for the input image. """
388
+ # pred_bbox, pred_class = model_detection.predict(image)
389
+ # pred_label_confidence = np.max(pred_class, axis=1)[0]
390
+ # pred_label = np.argmax(pred_class, axis=1)[0]
391
+ # return pred_bbox[0], pred_label, pred_label_confidence
392
 
393
+ # def draw_bbox(image, bbox):
394
+ # """ Draw bounding box on the image. """
395
+ # h, w, _ = image.shape
396
+ # x1, y1, x2, y2 = bbox
397
+ # x1, y1, x2, y2 = int(x1 * w), int(y1 * h), int(x2 * w), int(y2 * h)
398
+ # image = cv2.rectangle(image, (x1, y1), (x2, y2), (255, 0, 0), 2)
399
+ # return image
400
 
401
 
402
  ###########################################################################################
 
494
 
495
  with col2:
496
  file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
497
+ image = cv2.imdecode(file_bytes, 1)
498
  if st.button('Auto Detect'):
499
  st.write("Processing...")
500
  input_image = preprocess_image(image)