SerdarHelli commited on
Commit
6983ecb
1 Parent(s): 5198e72

Update app.py

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