shengqiangShi commited on
Commit
91af7f9
·
verified ·
1 Parent(s): 242bc2f

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -100
app.py CHANGED
@@ -1,100 +1,100 @@
1
- import streamlit as st
2
-
3
- import tensorflow as tf
4
- from PIL import Image
5
- import numpy as np
6
- import cv2
7
- from huggingface_hub import from_pretrained_keras
8
-
9
-
10
- try:
11
- model=from_pretrained_keras("shengqiangshi/toothseg")
12
- except:
13
- model=tf.keras.models.load_model("dental_xray_seg.h5")
14
- pass
15
-
16
- st.header("Segmentation of Teeth in Panoramic X-ray Image")
17
-
18
- examples=["107.png","108.png","109.png"]
19
- link='Check Out Our Github Repo ! [link](https://huggingface.co/spaces/shengqiangShi/toothseg)'
20
- st.markdown(link,unsafe_allow_html=True)
21
-
22
-
23
- def load_image(image_file):
24
- img = Image.open(image_file)
25
- return img
26
-
27
- def convert_one_channel(img):
28
- #some images have 3 channels , although they are grayscale image
29
- if len(img.shape)>2:
30
- img= cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
31
- return img
32
- else:
33
- return img
34
-
35
- def convert_rgb(img):
36
- #some images have 3 channels , although they are grayscale image
37
- if len(img.shape)==2:
38
- img= cv2.cvtColor(img,cv2.COLOR_GRAY2RGB)
39
- return img
40
- else:
41
- return img
42
-
43
-
44
- st.subheader("Upload Dental Panoramic X-ray Image Image")
45
- image_file = st.file_uploader("Upload Images", type=["png","jpg","jpeg"])
46
-
47
-
48
- col1, col2, col3 = st.columns(3)
49
- with col1:
50
- ex=load_image(examples[0])
51
- st.image(ex,width=200)
52
- if st.button('Example 1'):
53
- image_file=examples[0]
54
-
55
- with col2:
56
- ex1=load_image(examples[1])
57
- st.image(ex1,width=200)
58
- if st.button('Example 2'):
59
- image_file=examples[1]
60
-
61
-
62
- with col3:
63
- ex2=load_image(examples[2])
64
- st.image(ex2,width=200)
65
- if st.button('Example 3'):
66
- image_file=examples[2]
67
-
68
-
69
- if image_file is not None:
70
-
71
- img=load_image(image_file)
72
-
73
- st.text("Making A Prediction ....")
74
- st.image(img,width=850)
75
-
76
- img=np.asarray(img)
77
-
78
- img_cv=convert_one_channel(img)
79
- img_cv=cv2.resize(img_cv,(512,512), interpolation=cv2.INTER_LANCZOS4)
80
- img_cv=np.float32(img_cv/255)
81
-
82
- img_cv=np.reshape(img_cv,(1,512,512,1))
83
- prediction=model.predict(img_cv)
84
- predicted=prediction[0]
85
- predicted = cv2.resize(predicted, (img.shape[1],img.shape[0]), interpolation=cv2.INTER_LANCZOS4)
86
- mask=np.uint8(predicted*255)#
87
- _, mask = cv2.threshold(mask, thresh=0, maxval=255, type=cv2.THRESH_BINARY+cv2.THRESH_OTSU)
88
- kernel =( np.ones((5,5), dtype=np.float32))
89
- mask=cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel,iterations=1 )
90
- mask=cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel,iterations=1 )
91
- cnts,hieararch=cv2.findContours(mask,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
92
- output = cv2.drawContours(convert_rgb(img), cnts, -1, (255, 0, 0) , 3)
93
-
94
-
95
- if output is not None :
96
- st.subheader("Predicted Image")
97
- st.write(output.shape)
98
- st.image(output,width=850)
99
-
100
- st.text("DONE ! ....")
 
1
+ import streamlit as st
2
+
3
+ import tensorflow as tf
4
+ from PIL import Image
5
+ import numpy as np
6
+ import cv2
7
+ from huggingface_hub import from_pretrained_keras
8
+
9
+
10
+ try:
11
+ model=from_pretrained_keras("SerdarHelli/Segmentation-of-Teeth-in-Panoramic-X-ray-Image-Using-U-Net")
12
+ except:
13
+ model=tf.keras.models.load_model("dental_xray_seg.h5")
14
+ pass
15
+
16
+ st.header("Segmentation of Teeth in Panoramic X-ray Image Using UNet")
17
+
18
+ examples=["107.png","108.png","109.png"]
19
+ link='Check Out Our Github Repo ! [link](https://github.com/SerdarHelli/Segmentation-of-Teeth-in-Panoramic-X-ray-Image-Using-U-Net)'
20
+ st.markdown(link,unsafe_allow_html=True)
21
+
22
+
23
+ def load_image(image_file):
24
+ img = Image.open(image_file)
25
+ return img
26
+
27
+ def convert_one_channel(img):
28
+ #some images have 3 channels , although they are grayscale image
29
+ if len(img.shape)>2:
30
+ img= cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
31
+ return img
32
+ else:
33
+ return img
34
+
35
+ def convert_rgb(img):
36
+ #some images have 3 channels , although they are grayscale image
37
+ if len(img.shape)==2:
38
+ img= cv2.cvtColor(img,cv2.COLOR_GRAY2RGB)
39
+ return img
40
+ else:
41
+ return img
42
+
43
+
44
+ st.subheader("Upload Dental Panoramic X-ray Image Image")
45
+ image_file = st.file_uploader("Upload Images", type=["png","jpg","jpeg"])
46
+
47
+
48
+ col1, col2, col3 = st.columns(3)
49
+ with col1:
50
+ ex=load_image(examples[0])
51
+ st.image(ex,width=200)
52
+ if st.button('Example 1'):
53
+ image_file=examples[0]
54
+
55
+ with col2:
56
+ ex1=load_image(examples[1])
57
+ st.image(ex1,width=200)
58
+ if st.button('Example 2'):
59
+ image_file=examples[1]
60
+
61
+
62
+ with col3:
63
+ ex2=load_image(examples[2])
64
+ st.image(ex2,width=200)
65
+ if st.button('Example 3'):
66
+ image_file=examples[2]
67
+
68
+
69
+ if image_file is not None:
70
+
71
+ img=load_image(image_file)
72
+
73
+ st.text("Making A Prediction ....")
74
+ st.image(img,width=850)
75
+
76
+ img=np.asarray(img)
77
+
78
+ img_cv=convert_one_channel(img)
79
+ img_cv=cv2.resize(img_cv,(512,512), interpolation=cv2.INTER_LANCZOS4)
80
+ img_cv=np.float32(img_cv/255)
81
+
82
+ img_cv=np.reshape(img_cv,(1,512,512,1))
83
+ prediction=model.predict(img_cv)
84
+ predicted=prediction[0]
85
+ predicted = cv2.resize(predicted, (img.shape[1],img.shape[0]), interpolation=cv2.INTER_LANCZOS4)
86
+ mask=np.uint8(predicted*255)#
87
+ _, mask = cv2.threshold(mask, thresh=0, maxval=255, type=cv2.THRESH_BINARY+cv2.THRESH_OTSU)
88
+ kernel =( np.ones((5,5), dtype=np.float32))
89
+ mask=cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel,iterations=1 )
90
+ mask=cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel,iterations=1 )
91
+ cnts,hieararch=cv2.findContours(mask,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
92
+ output = cv2.drawContours(convert_rgb(img), cnts, -1, (255, 0, 0) , 3)
93
+
94
+
95
+ if output is not None :
96
+ st.subheader("Predicted Image")
97
+ st.write(output.shape)
98
+ st.image(output,width=850)
99
+
100
+ st.text("DONE ! ....")