DzmitryXXL commited on
Commit
9c68243
1 Parent(s): 3ff0551

Upload 5 files

Browse files
Files changed (6) hide show
  1. .gitattributes +4 -0
  2. app.py +116 -66
  3. teeth_01.png +0 -0
  4. teeth_02.png +0 -0
  5. teeth_03.png +0 -0
  6. teeth_04.png +0 -0
.gitattributes CHANGED
@@ -33,3 +33,7 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ teeth_01.png filter=lfs diff=lfs merge=lfs -text
37
+ teeth_02.png filter=lfs diff=lfs merge=lfs -text
38
+ teeth_03.png filter=lfs diff=lfs merge=lfs -text
39
+ teeth_04.png filter=lfs diff=lfs merge=lfs -text
app.py CHANGED
@@ -4,13 +4,14 @@ from PIL import Image
4
  import numpy as np
5
  import cv2
6
  import matplotlib.pyplot as plt
7
-
 
8
 
9
  model=tf.keras.models.load_model("dental_xray_seg.h5")
10
 
11
  st.header("Segmentation of Teeth in Panoramic X-ray Image")
12
 
13
- examples=["teeth_01.png","teeth_02.png","teeth_03.png","teeth_04.png","teeth_05.png"]
14
 
15
  def load_image(image_file):
16
  img = Image.open(image_file)
@@ -31,12 +32,82 @@ def convert_rgb(img):
31
  return img
32
  else:
33
  return img
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
 
36
- st.subheader("Upload Dental Panoramic X-ray Image Image")
37
  image_file = st.file_uploader("Upload Images", type=["png","jpg","jpeg"])
38
 
39
- col1, col2, col3, col4, col5 = st.columns(5)
40
  with col1:
41
  ex=load_image(examples[0])
42
  st.image(ex,width=200)
@@ -61,69 +132,48 @@ with col4:
61
  if st.button('Example 4'):
62
  image_file=examples[3]
63
 
64
- with col5:
65
- ex2=load_image(examples[4])
66
- st.image(ex2,width=200)
67
- if st.button('Example 5'):
68
- image_file=examples[4]
69
-
70
  if image_file is not None:
71
 
72
- img=load_image(image_file)
73
 
74
- st.text("Making A Prediction ....")
75
- st.image(img,width=850)
76
 
77
- img=np.asarray(img)
78
-
79
- img_cv=convert_one_channel(img)
80
- img_cv=cv2.resize(img_cv,(512,512), interpolation=cv2.INTER_LANCZOS4)
81
- img_cv=np.float32(img_cv/255)
82
-
83
- img_cv=np.reshape(img_cv,(1,512,512,1))
84
- prediction=model.predict(img_cv)
85
- predicted=prediction[0]
86
- predicted = cv2.resize(predicted, (img.shape[1],img.shape[0]), interpolation=cv2.INTER_LANCZOS4)
87
- mask=np.uint8(predicted*255)#
88
- _, mask = cv2.threshold(mask, thresh=0, maxval=255, type=cv2.THRESH_BINARY+cv2.THRESH_OTSU)
89
- kernel =( np.ones((5,5), dtype=np.float32))
90
- mask=cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel,iterations=1 )
91
- mask=cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel,iterations=1 )
92
- cnts,hieararch=cv2.findContours(mask,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
93
- output = cv2.drawContours(convert_rgb(img), cnts, -1, (255, 0, 0) , 3)
94
-
95
-
96
- if output is not None :
97
- st.subheader("Predicted Image")
98
- st.write(output.shape)
99
- st.image(output,width=850)
100
-
101
- st.text("DONE ! ....")
102
-
103
- #if image_file is not None:
104
- # img=load_image(image_file)
105
- #
106
- # st.text("Making A Prediction ....")
107
- # st.image(img,width=850)
108
- #
109
- # img=np.asarray(img)
110
- #
111
- # img_cv=convert_one_channel(img)
112
- # img_cv=cv2.resize(img_cv,(512,512), interpolation=cv2.INTER_LANCZOS4)
113
- # img_cv=np.float32(img_cv/255)
114
- #
115
- # img_cv=np.reshape(img_cv,(1,512,512,1))
116
- # predict_img=model.predict(img_cv)
117
- # predict=predict_img[1,:,:,0]
118
- # plt.imsave("predict.png",predict_img)
119
- #
120
- # ## Plotting - Пример результата
121
- # img = cv2.imread(image_file)
122
- #
123
- # predict1 = cv2.resize(predict_img, (img.shape[1], img.shape[0]), interpolation=cv2.INTER_LANCZOS4)
124
- #
125
- # mask = np.uint8(predict1 * 255)
126
- # _, mask = cv2.threshold(mask, thresh=255/2, maxval=255, type=cv2.THRESH_BINARY)
127
- # cnts, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
128
- # img = cv2.drawContours(img, cnts, -1, (255, 0, 0), 2)
129
- # cv2_imshow(img)
 
4
  import numpy as np
5
  import cv2
6
  import matplotlib.pyplot as plt
7
+ from imutils import perspective
8
+ from scipy.spatial import distance as dist
9
 
10
  model=tf.keras.models.load_model("dental_xray_seg.h5")
11
 
12
  st.header("Segmentation of Teeth in Panoramic X-ray Image")
13
 
14
+ examples=["teeth_01.png","teeth_02.png","teeth_03.png","teeth_04.png"]
15
 
16
  def load_image(image_file):
17
  img = Image.open(image_file)
 
32
  return img
33
  else:
34
  return img
35
+
36
+ def midpoint(ptA, ptB):
37
+ return ((ptA[0] + ptB[0]) * 0.5, (ptA[1] + ptB[1]) * 0.5)
38
+
39
+ def CCA_Analysis(orig_image,predict_image,erode_iteration,open_iteration):
40
+ kernel1 =( np.ones((5,5), dtype=np.float32))
41
+ kernel_sharpening = np.array([[-1,-1,-1],
42
+ [-1,9,-1],
43
+ [-1,-1,-1]])
44
+ image = predict_image
45
+ image2 =orig_image
46
+ image=cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel1,iterations=open_iteration )
47
+ image = cv2.filter2D(image, -1, kernel_sharpening)
48
+ image=cv2.erode(image,kernel1,iterations =erode_iteration)
49
+ image=cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
50
+ thresh = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
51
+ labels=cv2.connectedComponents(thresh,connectivity=8)[1]
52
+ a=np.unique(labels)
53
+ count2=0
54
+ for label in a:
55
+ if label == 0:
56
+ continue
57
+
58
+ # Create a mask
59
+ mask = np.zeros(thresh.shape, dtype="uint8")
60
+ mask[labels == label] = 255
61
+ # Find contours and determine contour area
62
+ cnts,hieararch = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
63
+ cnts = cnts[0]
64
+ c_area = cv2.contourArea(cnts)
65
+ # threshhold for tooth count
66
+ if c_area>1000:
67
+ count2+=1
68
+
69
+ (x,y),radius = cv2.minEnclosingCircle(cnts)
70
+ rect = cv2.minAreaRect(cnts)
71
+ box = cv2.boxPoints(rect)
72
+ box = np.array(box, dtype="int")
73
+ box = perspective.order_points(box)
74
+ color1 = (list(np.random.choice(range(150), size=3)))
75
+ color =[int(color1[0]), int(color1[1]), int(color1[2])]
76
+ cv2.drawContours(image2,[box.astype("int")],0,color,2)
77
+ (tl,tr,br,bl)=box
78
+
79
+ (tltrX,tltrY)=midpoint(tl,tr)
80
+ (blbrX,blbrY)=midpoint(bl,br)
81
+ # compute the midpoint between the top-left and top-right points,
82
+ # followed by the midpoint between the top-righ and bottom-right
83
+ (tlblX,tlblY)=midpoint(tl,bl)
84
+ (trbrX,trbrY)=midpoint(tr,br)
85
+ # draw the midpoints on the image
86
+ cv2.circle(image2, (int(tltrX), int(tltrY)), 5, (255, 0, 0), -1)
87
+ cv2.circle(image2, (int(blbrX), int(blbrY)), 5, (255, 0, 0), -1)
88
+ cv2.circle(image2, (int(tlblX), int(tlblY)), 5, (255, 0, 0), -1)
89
+ cv2.circle(image2, (int(trbrX), int(trbrY)), 5, (255, 0, 0), -1)
90
+ cv2.line(image2, (int(tltrX), int(tltrY)), (int(blbrX), int(blbrY)),color, 2)
91
+ cv2.line(image2, (int(tlblX), int(tlblY)), (int(trbrX), int(trbrY)),color, 2)
92
+ dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY))
93
+ dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY))
94
+
95
+
96
+
97
+ pixelsPerMetric=1
98
+ dimA = dA * pixelsPerMetric
99
+ dimB = dB *pixelsPerMetric
100
+ cv2.putText(image2, "{:.1f}pixel".format(dimA),(int(tltrX - 15), int(tltrY - 10)), cv2.FONT_HERSHEY_SIMPLEX,0.65, color, 2)
101
+ cv2.putText(image2, "{:.1f}pixel".format(dimB),(int(trbrX + 10), int(trbrY)), cv2.FONT_HERSHEY_SIMPLEX,0.65, color, 2)
102
+ cv2.putText(image2, "{:.1f}".format(label),(int(tltrX - 35), int(tltrY - 5)), cv2.FONT_HERSHEY_SIMPLEX,0.65, color, 2)
103
+ teeth_count=count2
104
+ return image2,teeth_count
105
 
106
 
107
+ st.subheader("Upload Dental Panoramic X-ray Image")
108
  image_file = st.file_uploader("Upload Images", type=["png","jpg","jpeg"])
109
 
110
+ col1, col2, col3, col4 = st.columns(4)
111
  with col1:
112
  ex=load_image(examples[0])
113
  st.image(ex,width=200)
 
132
  if st.button('Example 4'):
133
  image_file=examples[3]
134
 
 
 
 
 
 
 
135
  if image_file is not None:
136
 
137
+ image=cv2.imread(image_file)
138
 
139
+ st.text("Making A Prediction ....")
140
+ st.image(img,width=1100)
141
 
142
+ img=np.asarray(image)
143
+
144
+ img_cv=convert_one_channel(img)
145
+ img_cv=cv2.resize(img_cv,(512,512), interpolation=cv2.INTER_LANCZOS4)
146
+ img_cv=np.float32(img_cv/255)
147
+
148
+ img_cv=np.reshape(img_cv,(1,512,512,1))
149
+ prediction=model.predict(img_cv)
150
+ predicted=prediction[0]
151
+ predicted_rgb = np.expand_dims(predicted, axis=-1)
152
+ plt.imsave("predict.png",predicted_rgb)
153
+
154
+ predict1 = cv2.resize(predicted, (img.shape[1], img.shape[0]), interpolation=cv2.INTER_LANCZOS4)
155
+
156
+ mask = np.uint8(predict1 * 255)
157
+ _, mask = cv2.threshold(mask, thresh=255/2, maxval=255, type=cv2.THRESH_BINARY)
158
+ cnts, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
159
+ img = cv2.drawContours(img, cnts, -1, (255, 0, 0), 2)
160
+
161
+ if img is not None :
162
+ st.subheader("Predicted Image")
163
+ st.write(img.shape)
164
+ st.image(img,width=1100)
165
+
166
+ if image.shape[1] < 3000:
167
+ image = cv2.resize(image,(3100,1150),interpolation=cv2.INTER_LANCZOS4)
168
+ predicted=cv2.imread("predict.png")
169
+ predicted = cv2.resize(predicted, (image.shape[1],image.shape[0]), interpolation=cv2.INTER_LANCZOS4)
170
+ cca_result,teeth_count=CCA_Analysis(image,predicted,3,2)
171
+ if cca_result is not None :
172
+ st.subheader("Predicted Image")
173
+ st.write(cca_result.shape)
174
+ st.image(cca_result,width=1100)
175
+
176
+ st.text(teeth_count,"Teeth Count")
177
+
178
+ st.text("DONE ! ....")
179
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
teeth_01.png CHANGED

Git LFS Details

  • SHA256: 1588eae56e822fae3dbc639b7a35ea9ab6af98abd8b9da260e9d259e5dc0d35e
  • Pointer size: 132 Bytes
  • Size of remote file: 1.63 MB
teeth_02.png CHANGED

Git LFS Details

  • SHA256: 009e8b90ec37875c9c2d6c85d88f9375318c2727375c506fd2d86c71f0ebaf29
  • Pointer size: 132 Bytes
  • Size of remote file: 1.56 MB
teeth_03.png CHANGED

Git LFS Details

  • SHA256: 0e47a548823d5e8b0da2291d2f310028075ae198f8458200dd293f956792ab94
  • Pointer size: 132 Bytes
  • Size of remote file: 1.28 MB
teeth_04.png CHANGED

Git LFS Details

  • SHA256: 479f1dd53d468a3b5891aaf69e9cdc8de3030d5726b866d566c3b302ca8f6dae
  • Pointer size: 132 Bytes
  • Size of remote file: 1.52 MB