SerdarHelli commited on
Commit
a9abdf6
1 Parent(s): 8895ab5

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +132 -0
app.py ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+
3
+
4
+
5
+ import streamlit as st
6
+
7
+ import tensorflow as tf
8
+ from PIL import Image
9
+ import numpy as np
10
+ import cv2
11
+ from Utils import *
12
+
13
+ model_path="C:/Users/pc/Desktop/Merchant_Landmark_V1_30122021/30122021SavedModels/SavedModel100Epochs"
14
+ model = tf.keras.models.load_model(model_path,custom_objects=None)
15
+
16
+
17
+
18
+
19
+ st.subheader("Upload Merchant Knee View")
20
+ image_file = st.file_uploader("Upload Images", type=["dcm"])
21
+
22
+
23
+ examples=["C:/Users/pc/Desktop/Merchant_Landmark_V1_30122021/Dicoms/1.2.392.200036.9107.500.304.423.20111205.94459.10423.dcm"
24
+ ,"C:/Users/pc/Desktop/Merchant_Landmark_V1_30122021/Dicoms/1.2.392.200036.9107.500.304.423.20120225.115452.10423.dcm",
25
+ "C:/Users/pc/Desktop/Merchant_Landmark_V1_30122021/Dicoms/1.2.392.200036.9107.500.304.423.20120509.174735.10423.dcm"]
26
+
27
+ colx1, colx2, colx3 = st.columns(3)
28
+ with colx1:
29
+ st.text("Example Dicom ")
30
+
31
+ if st.button('Example 1'):
32
+ image_file=examples[0]
33
+
34
+ with colx2:
35
+ st.text("Example Dicom ")
36
+
37
+ if st.button('Example 2'):
38
+ image_file=examples[1]
39
+
40
+
41
+ with colx3:
42
+ st.text("Example Dicom ")
43
+
44
+ if st.button('Example 3'):
45
+ image_file=examples[2]
46
+
47
+
48
+ if image_file is not None:
49
+ st.text("Making A Prediction ....")
50
+
51
+
52
+ try:
53
+ data,PatientName,PatientID,SOPInstanceUID,StudyDate,InstitutionAddress,PatientAge,PatientSex=read_dicom(image_file,False,True)
54
+ except:
55
+ data,PatientName,PatientID,SOPInstanceUID,StudyDate,InstitutionAddress,PatientAge,PatientSex=read_dicom(image_file,True,True)
56
+ pass
57
+
58
+
59
+
60
+ img = np.copy(data)
61
+
62
+ #Denoise Image
63
+ kernel =( np.ones((5,5), dtype=np.float32))
64
+ img2=cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel,iterations=2 )
65
+ img2=cv2.erode(img2,kernel,iterations =2)
66
+ if len(img2.shape)==3:
67
+ img2=img2[:,:,0]
68
+
69
+ #Threshhold 100- 4096
70
+ ret,thresh = cv2.threshold(img2,100, 4096, cv2.THRESH_BINARY)
71
+
72
+ #To Thresh uint8 becasue "findContours" doesnt accept uint16
73
+ thresh =((thresh/np.max(thresh))*255).astype('uint8')
74
+ a1,b1=thresh.shape
75
+ #Find Countours
76
+ contours, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
77
+
78
+ #If There is no countour
79
+ if len(contours)==0:
80
+ roi= thresh
81
+
82
+ else:
83
+ #Get Areas
84
+ c_area=np.zeros([len(contours)])
85
+ for i in range(len(contours)):
86
+ c_area[i]= cv2.contourArea(contours[i])
87
+
88
+ #Find Max Countour
89
+ cnts=contours[np.argmax(c_area)]
90
+ x, y, w, h = cv2.boundingRect(cnts)
91
+
92
+ #Posibble Square
93
+ roi = croping(data, x, y, w, h)
94
+
95
+ # Absolute Square
96
+ roi=modification_cropping(roi)
97
+
98
+ # Resize to 256x256 with Inter_Nearest
99
+ roi=cv2.resize(roi,(256,256),interpolation=cv2.INTER_NEAREST)
100
+
101
+ pre=predict(roi,model)
102
+ heatpoint=points_max_value(pre)
103
+ output=put_text_point(roi,heatpoint)
104
+ output,PatellerCongruenceAngle,ParalelTiltAngle=draw_angle(output,heatpoint)
105
+ data_text = {'PatientID': PatientID, 'PatientName': PatientName,
106
+ 'Pateller_Congruence_Angle': PatellerCongruenceAngle,
107
+ 'Paralel_Tilt_Angle':ParalelTiltAngle,
108
+ 'SOP_Instance_UID':SOPInstanceUID,
109
+ "StudyDate" :StudyDate,
110
+ "InstitutionName" :InstitutionAddress,
111
+ "PatientAge" :PatientAge ,
112
+ "PatientSex" :PatientSex,
113
+ }
114
+
115
+
116
+
117
+ col1, col2 = st.columns(2)
118
+ with col1:
119
+ st.text("Original Dicom Image")
120
+
121
+ st.image(np.uint8((data/np.max(data)*255)),width=350)
122
+
123
+
124
+ with col2:
125
+ st.text("Predicted Image ")
126
+
127
+ st.image(np.uint8(output),width=350)
128
+
129
+
130
+
131
+ st.write(data_text)
132
+