Spaces:
				
			
			
	
			
			
		Sleeping
		
	
	
	
			
			
	
	
	
	
		
		
		Sleeping
		
	
		陳俞蒨
		
	commited on
		
		
					Commit 
							
							·
						
						545d681
	
1
								Parent(s):
							
							055d826
								
Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | @@ -1,7 +1,100 @@ | |
|  | |
| 1 | 
             
            import gradio as gr
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 2 |  | 
| 3 | 
            -
            def  | 
| 4 | 
            -
             | 
|  | |
|  | |
| 5 |  | 
| 6 | 
            -
             | 
| 7 | 
            -
             | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            import numpy as np
         | 
| 2 | 
             
            import gradio as gr
         | 
| 3 | 
            +
            import sys,  time
         | 
| 4 | 
            +
            import ipywidgets as widget
         | 
| 5 | 
            +
            from IPython.display import display
         | 
| 6 | 
            +
            import numpy as np
         | 
| 7 | 
            +
            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()
         | 
| 76 | 
            +
                        image_output = gr.Image()
         | 
| 77 | 
            +
                    temp_slider = gr.Slider(
         | 
| 78 | 
            +
                        0, 1,
         | 
| 79 | 
            +
                        value=0.5,
         | 
| 80 | 
            +
                        step=0.01,
         | 
| 81 | 
            +
                        interactive=True,
         | 
| 82 | 
            +
                        label="Threshold Factor",
         | 
| 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__":
         | 
| 100 | 
            +
                demo.launch()
         |