Spaces:
				
			
			
	
			
			
					
		Running
		
	
	
	
			
			
	
	
	
	
		
		
					
		Running
		
	Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | @@ -1,90 +1,139 @@ | |
| 1 | 
             
            import os
         | 
| 2 | 
            -
             | 
| 3 | 
            -
             | 
|  | |
|  | |
| 4 | 
             
            import gradio as gr
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 5 | 
             
            import cv2
         | 
| 6 | 
             
            import numpy as np
         | 
| 7 | 
            -
            import paddlehub as hub
         | 
| 8 | 
             
            import onnxruntime
         | 
|  | |
|  | |
|  | |
|  | |
| 9 |  | 
| 10 | 
            -
            #  | 
| 11 | 
            -
             | 
| 12 | 
            -
             | 
| 13 | 
            -
             | 
| 14 | 
            -
             | 
|  | |
|  | |
|  | |
| 15 |  | 
| 16 | 
            -
             | 
| 17 | 
            -
             | 
| 18 | 
            -
             | 
|  | |
| 19 |  | 
| 20 | 
            -
             | 
| 21 | 
            -
            u2net_model = hub.Module(name='U2Net') 
         | 
| 22 |  | 
| 23 | 
            -
             | 
|  | |
| 24 |  | 
| 25 | 
            -
             | 
| 26 | 
            -
             | 
| 27 | 
            -
                if  | 
| 28 | 
            -
                     | 
| 29 | 
            -
             | 
| 30 | 
            -
             | 
| 31 | 
            -
             | 
|  | |
|  | |
|  | |
| 32 | 
             
                else:
         | 
| 33 | 
            -
                     | 
| 34 | 
            -
             | 
| 35 | 
            -
                 | 
| 36 | 
            -
             | 
| 37 | 
            -
                if  | 
| 38 | 
            -
                     | 
| 39 | 
            -
                 | 
| 40 | 
            -
                     | 
| 41 | 
            -
                return  | 
| 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 | 
             
                output = outputs[0][0]
         | 
|  | |
| 68 | 
             
                output = output.transpose(1, 2, 0)
         | 
| 69 | 
            -
                output =  | 
| 70 | 
            -
                 | 
|  | |
|  | |
|  | |
| 71 |  | 
| 72 | 
            -
             | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 73 |  | 
| 74 | 
            -
            def process_image(input_image, mask_option):
         | 
| 75 | 
            -
                """Main function for Gradio interface."""
         | 
| 76 | 
            -
                imageio.imwrite("./data/data.png", input_image)
         | 
| 77 |  | 
| 78 | 
            -
                image = prepare_image(input_image)
         | 
| 79 | 
            -
                mask = generate_mask(input_image, method=mask_option)
         | 
| 80 | 
            -
                
         | 
| 81 | 
            -
                inpainted_image = inpaint_image(image, mask)
         | 
| 82 | 
            -
                inpainted_image = inpainted_image.resize(Image.open("./data/data.png").size)
         | 
| 83 | 
            -
                inpainted_image.save("./dataout/data_mask.png")
         | 
| 84 | 
            -
                return "./dataout/data_mask.png", "./data/data_mask.png"
         | 
| 85 |  | 
| 86 | 
             
            iface = gr.Interface(
         | 
| 87 | 
            -
                fn= | 
| 88 | 
             
                inputs=[
         | 
| 89 | 
             
                    gr.Image(label="Input Image", type="numpy"),
         | 
| 90 | 
             
                    gr.Radio(choices=["automatic", ], 
         | 
|  | |
| 1 | 
             
            import os
         | 
| 2 | 
            +
            os.system("wget https://huggingface.co/Carve/LaMa-ONNX/resolve/main/lama_fp32.onnx")
         | 
| 3 | 
            +
            os.system("pip install onnxruntime imageio")
         | 
| 4 | 
            +
            import cv2
         | 
| 5 | 
            +
            import paddlehub as hub
         | 
| 6 | 
             
            import gradio as gr
         | 
| 7 | 
            +
            import torch
         | 
| 8 | 
            +
            from PIL import Image, ImageOps
         | 
| 9 | 
            +
            import numpy as np
         | 
| 10 | 
            +
            import imageio
         | 
| 11 | 
            +
            os.mkdir("data")
         | 
| 12 | 
            +
            os.mkdir("dataout")
         | 
| 13 | 
            +
            model = hub.Module(name='U2Net')
         | 
| 14 | 
             
            import cv2
         | 
| 15 | 
             
            import numpy as np
         | 
|  | |
| 16 | 
             
            import onnxruntime
         | 
| 17 | 
            +
            import torch
         | 
| 18 | 
            +
            from PIL import Image
         | 
| 19 | 
            +
            sess_options = onnxruntime.SessionOptions()
         | 
| 20 | 
            +
            rmodel = onnxruntime.InferenceSession('lama_fp32.onnx', sess_options=sess_options)
         | 
| 21 |  | 
| 22 | 
            +
            # Source https://github.com/advimman/lama
         | 
| 23 | 
            +
            def get_image(image):
         | 
| 24 | 
            +
                if isinstance(image, Image.Image):
         | 
| 25 | 
            +
                    img = np.array(image)
         | 
| 26 | 
            +
                elif isinstance(image, np.ndarray):
         | 
| 27 | 
            +
                    img = image.copy()
         | 
| 28 | 
            +
                else:
         | 
| 29 | 
            +
                    raise Exception("Input image should be either PIL Image or numpy array!")
         | 
| 30 |  | 
| 31 | 
            +
                if img.ndim == 3:
         | 
| 32 | 
            +
                    img = np.transpose(img, (2, 0, 1))  # chw
         | 
| 33 | 
            +
                elif img.ndim == 2:
         | 
| 34 | 
            +
                    img = img[np.newaxis, ...]
         | 
| 35 |  | 
| 36 | 
            +
                assert img.ndim == 3
         | 
|  | |
| 37 |  | 
| 38 | 
            +
                img = img.astype(np.float32) / 255
         | 
| 39 | 
            +
                return img
         | 
| 40 |  | 
| 41 | 
            +
             | 
| 42 | 
            +
            def ceil_modulo(x, mod):
         | 
| 43 | 
            +
                if x % mod == 0:
         | 
| 44 | 
            +
                    return x
         | 
| 45 | 
            +
                return (x // mod + 1) * mod
         | 
| 46 | 
            +
             | 
| 47 | 
            +
             | 
| 48 | 
            +
            def scale_image(img, factor, interpolation=cv2.INTER_AREA):
         | 
| 49 | 
            +
                if img.shape[0] == 1:
         | 
| 50 | 
            +
                    img = img[0]
         | 
| 51 | 
             
                else:
         | 
| 52 | 
            +
                    img = np.transpose(img, (1, 2, 0))
         | 
| 53 | 
            +
             | 
| 54 | 
            +
                img = cv2.resize(img, dsize=None, fx=factor, fy=factor, interpolation=interpolation)
         | 
| 55 | 
            +
             | 
| 56 | 
            +
                if img.ndim == 2:
         | 
| 57 | 
            +
                    img = img[None, ...]
         | 
| 58 | 
            +
                else:
         | 
| 59 | 
            +
                    img = np.transpose(img, (2, 0, 1))
         | 
| 60 | 
            +
                return img
         | 
| 61 | 
            +
             | 
| 62 | 
            +
             | 
| 63 | 
            +
            def pad_img_to_modulo(img, mod):
         | 
| 64 | 
            +
                channels, height, width = img.shape
         | 
| 65 | 
            +
                out_height = ceil_modulo(height, mod)
         | 
| 66 | 
            +
                out_width = ceil_modulo(width, mod)
         | 
| 67 | 
            +
                return np.pad(
         | 
| 68 | 
            +
                    img,
         | 
| 69 | 
            +
                    ((0, 0), (0, out_height - height), (0, out_width - width)),
         | 
| 70 | 
            +
                    mode="symmetric",
         | 
| 71 | 
            +
                )
         | 
| 72 | 
            +
             | 
| 73 | 
            +
             | 
| 74 | 
            +
            def prepare_img_and_mask(image, mask, device, pad_out_to_modulo=8, scale_factor=None):
         | 
| 75 | 
            +
                out_image = get_image(image)
         | 
| 76 | 
            +
                out_mask = get_image(mask)
         | 
| 77 | 
            +
             | 
| 78 | 
            +
                if scale_factor is not None:
         | 
| 79 | 
            +
                    out_image = scale_image(out_image, scale_factor)
         | 
| 80 | 
            +
                    out_mask = scale_image(out_mask, scale_factor, interpolation=cv2.INTER_NEAREST)
         | 
| 81 | 
            +
             | 
| 82 | 
            +
                if pad_out_to_modulo is not None and pad_out_to_modulo > 1:
         | 
| 83 | 
            +
                    out_image = pad_img_to_modulo(out_image, pad_out_to_modulo)
         | 
| 84 | 
            +
                    out_mask = pad_img_to_modulo(out_mask, pad_out_to_modulo)
         | 
| 85 | 
            +
             | 
| 86 | 
            +
                out_image = torch.from_numpy(out_image).unsqueeze(0).to(device)
         | 
| 87 | 
            +
                out_mask = torch.from_numpy(out_mask).unsqueeze(0).to(device)
         | 
| 88 | 
            +
             | 
| 89 | 
            +
                out_mask = (out_mask > 0) * 1
         | 
| 90 | 
            +
             | 
| 91 | 
            +
                return out_image, out_mask
         | 
| 92 | 
            +
             | 
| 93 | 
            +
             | 
| 94 | 
            +
            def predict(jpg, msk):
         | 
| 95 | 
            +
             | 
| 96 | 
            +
             | 
| 97 | 
            +
                imagex = Image.open(jpg)
         | 
| 98 | 
            +
                mask = Image.open(msk).convert("L")
         | 
| 99 | 
            +
             | 
| 100 | 
            +
                image, mask = prepare_img_and_mask(imagex.resize((512, 512)), mask.resize((512, 512)), 'cpu')
         | 
| 101 | 
            +
                # Run the model
         | 
| 102 | 
            +
                outputs = rmodel.run(None, {'image': image.numpy().astype(np.float32), 'mask': mask.numpy().astype(np.float32)})
         | 
| 103 | 
            +
             | 
| 104 | 
             
                output = outputs[0][0]
         | 
| 105 | 
            +
                # Postprocess the outputs
         | 
| 106 | 
             
                output = output.transpose(1, 2, 0)
         | 
| 107 | 
            +
                output = output.astype(np.uint8)
         | 
| 108 | 
            +
                output = Image.fromarray(output)
         | 
| 109 | 
            +
                output = output.resize(imagex.size)
         | 
| 110 | 
            +
                output.save("/home/user/app/dataout/data_mask.png")
         | 
| 111 | 
            +
             | 
| 112 |  | 
| 113 | 
            +
            def infer(img,option):
         | 
| 114 | 
            +
              print(type(img))
         | 
| 115 | 
            +
              print(type(img["image"]))
         | 
| 116 | 
            +
              print(type(img["mask"]))
         | 
| 117 | 
            +
              imageio.imwrite("./data/data.png", img["image"])
         | 
| 118 | 
            +
              if option == "automatic (U2net)":
         | 
| 119 | 
            +
                  result = model.Segmentation(
         | 
| 120 | 
            +
                      images=[cv2.cvtColor(img["image"], cv2.COLOR_RGB2BGR)],
         | 
| 121 | 
            +
                      paths=None,
         | 
| 122 | 
            +
                      batch_size=1,
         | 
| 123 | 
            +
                      input_size=320,
         | 
| 124 | 
            +
                      output_dir='output',
         | 
| 125 | 
            +
                      visualization=True)
         | 
| 126 | 
            +
                  im = Image.fromarray(result[0]['mask'])
         | 
| 127 | 
            +
                  im.save("./data/data_mask.png")
         | 
| 128 | 
            +
              else:
         | 
| 129 | 
            +
                  imageio.imwrite("./data/data_mask.png", img["mask"])
         | 
| 130 | 
            +
              predict("./data/data.png", "./data/data_mask.png")    
         | 
| 131 | 
            +
              return "./dataout/data_mask.png","./data/data_mask.png"
         | 
| 132 |  | 
|  | |
|  | |
|  | |
| 133 |  | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 134 |  | 
| 135 | 
             
            iface = gr.Interface(
         | 
| 136 | 
            +
                fn=infer,
         | 
| 137 | 
             
                inputs=[
         | 
| 138 | 
             
                    gr.Image(label="Input Image", type="numpy"),
         | 
| 139 | 
             
                    gr.Radio(choices=["automatic", ], 
         | 
 
			
