Spaces:
givkashi
/
Runtime error

givkashi commited on
Commit
325103c
Β·
verified Β·
1 Parent(s): 3e9db39

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -33
app.py CHANGED
@@ -1,40 +1,72 @@
1
  import os
2
- os.system("wget https://huggingface.co/akhaliq/lama/resolve/main/best.ckpt")
3
  os.system("pip install 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.rename("best.ckpt", "models/best.ckpt")
13
- os.mkdir("dataout")
14
- model = hub.Module(name='U2Net')
15
- def infer(img,option):
16
- print(type(img))
17
- print(type(img["image"]))
18
- print(type(img["mask"]))
19
- imageio.imwrite("./data/data.png", img["image"])
20
- if option == "automatic (U2net)":
21
- result = model.Segmentation(
22
- images=[cv2.cvtColor(img["image"], cv2.COLOR_RGB2BGR)],
23
- paths=None,
24
- batch_size=1,
25
- input_size=320,
26
- output_dir='output',
27
- visualization=True)
28
- im = Image.fromarray(result[0]['mask'])
29
- im.save("./data/data_mask.png")
30
- else:
31
- imageio.imwrite("./data/data_mask.png", img["mask"])
32
- os.system('python predict.py model.path=/home/user/app/ indir=/home/user/app/data/ outdir=/home/user/app/dataout/ device=cpu')
33
- return "./dataout/data_mask.png","./data/data_mask.png"
34
-
35
- inputs = [gr.Image(tool="sketch", label="Input",type="numpy"),gr.inputs.Radio(choices=["automatic (U2net)","manual"], type="value", default="manual", label="Masking option")]
36
- outputs = [gr.outputs.Image(type="file",label="output"),gr.outputs.Image(type="file",label="Mask")]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  title = "LaMa Image Inpainting"
38
  description = "Gradio demo for LaMa: Resolution-robust Large Mask Inpainting with Fourier Convolutions. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below. Masks are generated by U^2net"
39
  article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2109.07161' target='_blank'>Resolution-robust Large Mask Inpainting with Fourier Convolutions</a> | <a href='https://github.com/saic-mdal/lama' target='_blank'>Github Repo</a></p>"
40
- gr.Interface(infer, inputs, outputs, title=title, description=description, article=article).launch()
 
1
  import os
 
2
  os.system("pip install imageio")
3
+ # app.py for Hugging Face Spaces
4
+
5
  import gradio as gr
6
+ from simple_lama_inpainting import SimpleLama
7
+ from PIL import Image
8
  import numpy as np
9
+ import os
10
+
11
+ # Initialize the SimpleLama model
12
+ simple_lama = SimpleLama()
13
+
14
+ def inpaint_image(input_data):
15
+ if input_data is None:
16
+ raise gr.Error("Please upload an image and draw a mask.")
17
+
18
+ # input_data is a dict with 'image' and 'mask' as paths or arrays
19
+ # In Gradio 4+, it's dict with 'image' and 'mask' as PIL Images or paths
20
+ image_path = input_data['image']
21
+ mask_path = input_data['mask']
22
+
23
+ # Open image and mask
24
+ image = Image.open(image_path).convert("RGB")
25
+ mask = Image.open(mask_path).convert("L")
26
+
27
+ # In Gradio sketch, mask is white where drawn (to inpaint), black elsewhere
28
+ # But ensure it's binary if needed, though SimpleLama likely handles grayscale
29
+
30
+ # Perform inpainting
31
+ result = simple_lama(image, mask)
32
+
33
+ return result
34
+
35
+ with gr.Blocks(title="Image Inpainting with SimpleLama") as demo:
36
+ gr.Markdown("# Image Inpainting App")
37
+ gr.Markdown("Upload an image, draw a mask on the areas to inpaint, and click Submit.")
38
+
39
+ with gr.Row():
40
+ input_image = gr.Image(
41
+ label="Upload and Mask Image",
42
+ tool="sketch",
43
+ type="filepath", # Returns paths
44
+ brush_color="#FFFFFF", # White brush for mask
45
+ interactive=True
46
+ )
47
+ output_image = gr.Image(label="Inpainted Image")
48
+
49
+ submit_btn = gr.Button("Submit")
50
+ submit_btn.click(inpaint_image, inputs=[input_image], outputs=[output_image])
51
+
52
+ gr.Examples(
53
+ examples=[
54
+ # ["examples/example1.jpg"],
55
+ ["person512.png"],
56
+ ],
57
+ inputs=[input_image],
58
+ label="Try these examples (upload and draw mask)"
59
+ )
60
+
61
+ # For HF Spaces, requirements.txt would include:
62
+ # simple-lama-inpainting
63
+ # gradio
64
+ # pillow
65
+ # torch (if not included in simple-lama)
66
+
67
+ if __name__ == "__main__":
68
+ demo.launch()
69
  title = "LaMa Image Inpainting"
70
  description = "Gradio demo for LaMa: Resolution-robust Large Mask Inpainting with Fourier Convolutions. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below. Masks are generated by U^2net"
71
  article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2109.07161' target='_blank'>Resolution-robust Large Mask Inpainting with Fourier Convolutions</a> | <a href='https://github.com/saic-mdal/lama' target='_blank'>Github Repo</a></p>"
72
+ # gr.Interface(infer, inputs, outputs, title=title, description=description, article=article).launch()