File size: 2,532 Bytes
2b3cb99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import tensorflow as tf
from tensorflow.keras.models import load_model
import warnings
warnings.filterwarnings("ignore")
import matplotlib.pyplot as plt
import streamlit as st
import cv2
import numpy as np
import pandas as pd
import base64
import imgaug.augmenters as iaa
aug = iaa.Sharpen(alpha=(1.0), lightness=(1.5))
from st_clickable_images import clickable_images
st.title('Brain MR Image segmentation ')
data_load_state = st.text('Loading data...')
gdown --id 1UQIRoLzDCM2vAp0fiQwwuhDXVocPrGXd
unet=load_model('unet.h5',compile=False)
data_load_state.text('Loading data...done!')
st.subheader('Select a image in which you wish to detect tumor')

#=============================================================================
def plot_final(Data,return_image=False):
    image1=cv2.imread(Data)
    image = aug.augment_image(image1)
    image=image[:,:,1]
    image[image <0.2]=0.5
    image = image / 255
    predicted  = unet.predict(image[np.newaxis,:,:])
    predicted[predicted <0.25]=0
    img = predicted[0,:,:,0]
    mean,std=cv2.meanStdDev(img)
    
    pixels = cv2.countNonZero(img)
    image_area = img.shape[0] * img.shape[1]
    area_ratio = (pixels / image_area) * 100
    img = img*255
    img[img<1]=1
    img[img>100]=255
    M= cv2.moments(img)
    cX = int(M["m10"] / M["m00"])
    cY = int(M["m01"] / M["m00"])
    if return_image:
      return img,area_ratio,std,(cX,cY)
    else:
      return area_ratio,std,(cX,cY)

#===========================================================================


images = []
for file in ["1.jpeg", "2.jpeg","3.jpeg", "4.jpeg", "5.jpeg"]:
    with open(file, "rb") as image:
        encoded = base64.b64encode(image.read()).decode()
        images.append(f"data:image/jpeg;base64,{encoded}")

clicked = clickable_images(
    images,
    titles=[f"Image #{str(i)}" for i in range(2)],
    div_style={"display": "flex", "justify-content": "center", "flex-wrap": "wrap"},
    img_style={"margin": "5px", "height": "200px"},
)
#===========================================================================

if clicked>-1:
  mask,area,std,coordinates = plot_final(str(clicked)+".tif",return_image=True)
  fig = plt.figure()
  plt.imshow(cv2.imread(str(clicked)+".tif"))
  plt.imshow(mask,alpha=0.4,cmap='gray')
  if area==0.0:
      plt.title("No tumor detected", fontsize=20)
  else:
      plt.title("Area={} \n STD={} \n Centroid={}".format(area,std,(coordinates[0],coordinates[1])))
  plt.xticks([])
  plt.yticks([])

  st.pyplot(fig)

else:
  st.markdown("No Image selected")