Spaces:
Runtime error
Runtime error
Ahsen Khaliq
commited on
Commit
·
5a3dfd3
1
Parent(s):
816a3e6
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import cv2
|
3 |
+
import paddlehub as hub
|
4 |
+
import gradio as gr
|
5 |
+
import torch
|
6 |
+
from PIL import Image
|
7 |
+
import numpy as np
|
8 |
+
|
9 |
+
# Images
|
10 |
+
torch.hub.download_url_to_file('https://cdn.pixabay.com/photo/2018/08/12/16/59/ara-3601194_1280.jpg', 'parrot.jpg')
|
11 |
+
torch.hub.download_url_to_file('https://cdn.pixabay.com/photo/2016/10/21/14/46/fox-1758183_1280.jpg', 'fox.jpg')
|
12 |
+
model = hub.Module(name='U2Net')
|
13 |
+
def infer(img):
|
14 |
+
img.save("./data/data.png")
|
15 |
+
result = model.Segmentation(
|
16 |
+
images=[cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)],
|
17 |
+
paths=None,
|
18 |
+
batch_size=1,
|
19 |
+
input_size=320,
|
20 |
+
output_dir='output',
|
21 |
+
visualization=True)
|
22 |
+
im = Image.fromarray(result[0]['mask'])
|
23 |
+
im.save("./data/data_mask.png")
|
24 |
+
os.system('python predict.py model.path=./big-lama indir=./data outdir=./dataout device=cpu')
|
25 |
+
return "./dataout/data_mask.png"
|
26 |
+
inputs = gr.inputs.Image(type='file', label="Original Image")
|
27 |
+
outputs = gr.outputs.Image(type="numpy",label="output")
|
28 |
+
title = "U^2-Net"
|
29 |
+
description = "demo for U^2-Net. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below."
|
30 |
+
article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2005.09007'>U^2-Net: Going Deeper with Nested U-Structure for Salient Object Detection</a> | <a href='https://github.com/xuebinqin/U-2-Net'>Github Repo</a></p>"
|
31 |
+
examples = [
|
32 |
+
['fox.jpg'],
|
33 |
+
['parrot.jpg']
|
34 |
+
]
|
35 |
+
gr.Interface(infer, inputs, outputs, title=title, description=description, article=article, examples=examples).launch()
|