陳俞蒨 commited on
Commit
4df43e2
·
1 Parent(s): 545d681

seperate into app.py and segmentation.py

Browse files
__pycache__/segmentation.cpython-312.pyc ADDED
Binary file (3.59 kB). View file
 
app.py CHANGED
@@ -8,68 +8,17 @@ import cv2
8
  from PIL import Image as PIL_Image
9
  from io import BytesIO
10
  import matplotlib.pyplot as plt
 
11
 
12
- # def flip_text(x):
13
- # return x[::-1]
14
- # def flip_image(x):
15
- # return np.fliplr(x)
16
 
17
  def image_segmentation(x, threshold_factor):
18
- cv2.startWindowThread()
19
- def img_to_png(ima, cvt=None):
20
- if cvt:
21
- ima = cv2.cvtColor(ima, cvt)
22
- im = PIL_Image.fromarray(ima)
23
- bio = BytesIO()
24
- im.save(bio, format='png')
25
- return bio.getvalue()
26
-
27
- # img = cv2.imread(x) # 改為使用Gradio傳入的圖片
28
- img = x
29
- # plt.imshow(img[..., ::-1])
30
-
31
- gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
32
- ret, thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
33
- kernel = np.ones((3,3),np.uint8)
34
- opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel, iterations = 2)
35
-
36
- # sure background area
37
- sure_bg = cv2.dilate(opening,kernel,iterations=3)
38
- # Finding sure foreground area
39
- dist_transform = cv2.distanceTransform(opening,cv2.DIST_L2,5)
40
- ret, sure_fg = cv2.threshold(dist_transform,threshold_factor*dist_transform.max(),255,0)
41
- # Finding unknown region
42
- sure_fg = np.uint8(sure_fg)
43
- unknown = cv2.subtract(sure_bg,sure_fg)
44
- # Marker labelling
45
- ret, markers0 = cv2.connectedComponents(sure_fg)
46
- # Add one to all labels so that sure background is not 0, but 1
47
- markers = markers0+1
48
- # Now, mark the region of unknown with zero
49
- markers[unknown==255] = 0
50
- markers = cv2.watershed(img,markers)
51
- img[markers == -1] = [255,0,0]
52
-
53
- # plt.imshow(dist_transform)
54
- # plt.imshow(sure_fg)
55
- # plt.imshow(sure_bg, alpha=0.3)
56
- # plt.imshow(markers)
57
-
58
- # 將 markers 範圍縮放到 0-255 並轉換為 uint8 類型
59
- markers_scaled = (markers - markers.min()) / (markers.max() - markers.min()) * 255
60
- markers_scaled = markers_scaled.astype(np.uint8)
61
-
62
- # 使用 applyColorMap 生成彩色圖像
63
- markers_colored = cv2.applyColorMap(markers_scaled, cv2.COLORMAP_JET)
64
-
65
- return markers_colored # 返回彩色分割結果圖
66
 
67
  with gr.Blocks() as demo:
68
  gr.Markdown("Image Segmentation using Gradio")
69
- # with gr.Tab("Flip Text"):
70
- # text_input = gr.Textbox()
71
- # text_output = gr.Textbox()
72
- # text_button = gr.Button("Flip")
73
  with gr.Tab("Image Segmentation"):
74
  with gr.Row():
75
  image_input = gr.Image()
@@ -83,17 +32,6 @@ with gr.Blocks() as demo:
83
  )
84
  image_button = gr.Button("Flip")
85
 
86
- # with gr.Accordion("Open for More!", open=False):
87
- # gr.Markdown("Look at me...")
88
- # temp_slider = gr.Slider(
89
- # 0, 1,
90
- # value=0.1,
91
- # step=0.1,
92
- # interactive=True,
93
- # label="Slide me",
94
- # )
95
-
96
- # text_button.click(flip_text, inputs=text_input, outputs=text_output)
97
  image_button.click(image_segmentation, inputs=[image_input, temp_slider], outputs=image_output)
98
 
99
  if __name__ == "__main__":
 
8
  from PIL import Image as PIL_Image
9
  from io import BytesIO
10
  import matplotlib.pyplot as plt
11
+ from segmentation import ImageSegmentation
12
 
 
 
 
 
13
 
14
  def image_segmentation(x, threshold_factor):
15
+ segmetation = ImageSegmentation(x, threshold_factor)
16
+ output_img = segmetation.segmented_img
17
+ return output_img
18
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  with gr.Blocks() as demo:
21
  gr.Markdown("Image Segmentation using Gradio")
 
 
 
 
22
  with gr.Tab("Image Segmentation"):
23
  with gr.Row():
24
  image_input = gr.Image()
 
32
  )
33
  image_button = gr.Button("Flip")
34
 
 
 
 
 
 
 
 
 
 
 
 
35
  image_button.click(image_segmentation, inputs=[image_input, temp_slider], outputs=image_output)
36
 
37
  if __name__ == "__main__":
segmentation.ipynb DELETED
The diff for this file is too large to render. See raw diff
 
segmentation.py CHANGED
@@ -7,41 +7,49 @@ from PIL import Image as PIL_Image
7
  from io import BytesIO
8
  import matplotlib.pyplot as plt
9
 
10
- cv2.startWindowThread()
11
- def img_to_png(ima, cvt=None):
12
- if cvt:
13
- ima = cv2.cvtColor(ima, cvt)
14
- im = PIL_Image.fromarray(ima)
15
- bio = BytesIO()
16
- im.save(bio, format='png')
17
- return bio.getvalue()
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
- img = cv2.imread("img/water_coins.jpg") # 改為使用Gradio傳入的圖片
20
- # plt.imshow(img[..., ::-1])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
- gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
23
- ret, thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
24
- kernel = np.ones((3,3),np.uint8)
25
- opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel, iterations = 2)
26
 
27
- # sure background area
28
- sure_bg = cv2.dilate(opening,kernel,iterations=3)
29
- # Finding sure foreground area
30
- dist_transform = cv2.distanceTransform(opening,cv2.DIST_L2,5)
31
- ret, sure_fg = cv2.threshold(dist_transform,0.7*dist_transform.max(),255,0)
32
- # Finding unknown region
33
- sure_fg = np.uint8(sure_fg)
34
- unknown = cv2.subtract(sure_bg,sure_fg)
35
- # Marker labelling
36
- ret, markers0 = cv2.connectedComponents(sure_fg)
37
- # Add one to all labels so that sure background is not 0, but 1
38
- markers = markers0+1
39
- # Now, mark the region of unknown with zero
40
- markers[unknown==255] = 0
41
- markers = cv2.watershed(img,markers)
42
- img[markers == -1] = [255,0,0]
43
 
44
- # plt.imshow(dist_transform)
45
- # plt.imshow(sure_fg)
46
- # plt.imshow(sure_bg, alpha=0.3)
47
- # plt.imshow(markers)
 
7
  from io import BytesIO
8
  import matplotlib.pyplot as plt
9
 
10
+ class ImageSegmentation():
11
+ def __init__(self, x, threshold_factor):
12
+ self.img = x
13
+ self.threshold_factor = threshold_factor
14
+ self.segmented_img = self.segmentation()
15
+
16
+ def img_to_png(ima, cvt=None):
17
+ if cvt:
18
+ ima = cv2.cvtColor(ima, cvt)
19
+ im = PIL_Image.fromarray(ima)
20
+ bio = BytesIO()
21
+ im.save(bio, format='png')
22
+ return bio.getvalue()
23
+
24
+ def segmentation(self):
25
+ cv2.startWindowThread()
26
+ gray = cv2.cvtColor(self.img,cv2.COLOR_BGR2GRAY)
27
+ _, thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
28
+ kernel = np.ones((3,3),np.uint8)
29
+ opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel, iterations = 2)
30
 
31
+ # sure background area
32
+ sure_bg = cv2.dilate(opening,kernel,iterations=3)
33
+ # Finding sure foreground area
34
+ dist_transform = cv2.distanceTransform(opening,cv2.DIST_L2,5)
35
+ _, sure_fg = cv2.threshold(dist_transform,self.threshold_factor*dist_transform.max(),255,0)
36
+ # Finding unknown region
37
+ sure_fg = np.uint8(sure_fg)
38
+ unknown = cv2.subtract(sure_bg,sure_fg)
39
+ # Marker labelling
40
+ _, markers0 = cv2.connectedComponents(sure_fg)
41
+ # Add one to all labels so that sure background is not 0, but 1
42
+ markers = markers0+1
43
+ # Now, mark the region of unknown with zero
44
+ markers[unknown==255] = 0
45
+ markers = cv2.watershed(self.img,markers)
46
+ self.img[markers == -1] = [255,0,0]
47
 
48
+ # markers 範圍縮放到 0-255 並轉換為 uint8 類型
49
+ markers_scaled = (markers - markers.min()) / (markers.max() - markers.min()) * 255
50
+ markers_scaled = markers_scaled.astype(np.uint8)
 
51
 
52
+ # 使用 applyColorMap 生成彩色圖像
53
+ markers_colored = cv2.applyColorMap(markers_scaled, cv2.COLORMAP_JET)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
+ return markers_colored # 返回彩色分割結果圖