File size: 3,518 Bytes
6983ecb
 
 
 
 
c5da400
6983ecb
 
 
 
 
7b916d6
 
6983ecb
41e000e
6983ecb
 
7b916d6
 
 
 
 
 
6983ecb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139




import streamlit as st 
import os
from PIL import Image
import numpy as np
import cv2
from Utils import *

from huggingface_hub import hf_hub_download,from_pretrained_keras


model = from_pretrained_keras("SerdarHelli/Knee-View-Merchant-Landmark-Detection")








st.subheader("Upload Merchant Knee View")
image_file = st.file_uploader("Upload Images", type=["dcm"])


examples=["1.3.46.670589.30.1.6.1.149885691756583.1510655758812.1.dcm"
          ,"1.2.392.200036.9125.9.0.235868094.418384128.208354950.dcm",
          "1.2.392.200036.9107.500.304.423.20170526.173028.10423.dcm"]

colx1, colx2, colx3 = st.columns(3)


st.text("Merchant Knee View Dicom Examples ")

with colx1:
    st.text("Example -1 ")

    if st.button('Example 1'):
        image_file=examples[0]

with colx2:
    st.text("Example -2 ")

    if st.button('Example 2'):
        image_file=examples[1]


with colx3:
    st.text("Example -3 ")

    if st.button('Example 3'):
        image_file=examples[2]
    

if image_file is not None:
    st.text("Making A Prediction ....")

    
    try:
        data,PatientName,PatientID,SOPInstanceUID,StudyDate,InstitutionAddress,PatientAge,PatientSex=read_dicom(image_file,False,True)
    except:
        data,PatientName,PatientID,SOPInstanceUID,StudyDate,InstitutionAddress,PatientAge,PatientSex=read_dicom(image_file,True,True)
        pass
    


    img = np.copy(data)
    
    #Denoise Image 
    kernel =( np.ones((5,5), dtype=np.float32))
    img2=cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel,iterations=2 )
    img2=cv2.erode(img2,kernel,iterations =2)
    if len(img2.shape)==3:
        img2=img2[:,:,0]
        
    #Threshhold 100- 4096
    ret,thresh = cv2.threshold(img2,100, 4096, cv2.THRESH_BINARY)
    
    #To Thresh uint8 becasue "findContours" doesnt accept uint16
    thresh =((thresh/np.max(thresh))*255).astype('uint8')
    a1,b1=thresh.shape
    #Find Countours
    contours, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    
    #If There is no countour
    if len(contours)==0:
            roi= thresh
        
    else:
        #Get Areas 
        c_area=np.zeros([len(contours)])
        for i in range(len(contours)):
            c_area[i]= cv2.contourArea(contours[i]) 
            
        #Find Max Countour    
        cnts=contours[np.argmax(c_area)]
        x, y, w, h = cv2.boundingRect(cnts)
        
        #Posibble Square 
        roi = croping(data, x, y, w, h)
        
        # Absolute Square 
        roi=modification_cropping(roi)
        
        # Resize to 256x256 with Inter_Nearest
        roi=cv2.resize(roi,(256,256),interpolation=cv2.INTER_NEAREST)
        
    pre=predict(roi,model)
    heatpoint=points_max_value(pre)
    output=put_text_point(roi,heatpoint)
    output,PatellerCongruenceAngle,ParalelTiltAngle=draw_angle(output,heatpoint)
    data_text = {'PatientID': PatientID, 'PatientName': PatientName,
            'Pateller_Congruence_Angle': PatellerCongruenceAngle,
            'Paralel_Tilt_Angle':ParalelTiltAngle,
            'SOP_Instance_UID':SOPInstanceUID,
            "StudyDate" :StudyDate,
            "InstitutionName" :InstitutionAddress,
            "PatientAge" :PatientAge ,
            "PatientSex" :PatientSex,
            }
    
    
    
    st.text("Original Dicom Image")

    st.image(np.uint8((data/np.max(data)*255)),width=450)


    st.text("Predicted and Cropped-Resized Image ")

    st.image(np.uint8(output),width=450)

    
    
    st.write(data_text)