Spaces:
Runtime error
Runtime error
Commit
Β·
50e4934
1
Parent(s):
f9787a2
Update app.py
Browse files
app.py
CHANGED
@@ -2,33 +2,19 @@ import os
|
|
2 |
os.system('/usr/local/bin/python -m pip install --upgrade pip')
|
3 |
os.system("pip install opencv-python")
|
4 |
os.system("pip install pillow")
|
|
|
5 |
|
6 |
import gradio as gr
|
7 |
import cv2
|
8 |
import numpy as np
|
9 |
from PIL import Image
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
img = cv2.imread(img)
|
14 |
-
|
15 |
-
# Define the initial mask
|
16 |
-
mask = np.zeros(img.shape[:2], dtype=np.uint8)
|
17 |
-
bgdModel = np.zeros((1, 65), np.float64)
|
18 |
-
fgdModel = np.zeros((1, 65), np.float64)
|
19 |
-
|
20 |
-
# Define the rectangle for the GrabCut algorithm
|
21 |
-
rect = (50, 50, img.shape[1]-100, img.shape[0]-100)
|
22 |
-
|
23 |
-
# Run the GrabCut algorithm
|
24 |
-
cv2.grabCut(img, mask, rect, bgdModel, fgdModel, 5, cv2.GC_INIT_WITH_RECT)
|
25 |
-
|
26 |
-
# Get the final mask
|
27 |
-
mask2 = np.where((mask==2)|(mask==0), 0, 1).astype('uint8')
|
28 |
-
|
29 |
-
# Multiply the image with the mask to remove the background
|
30 |
-
img = img * mask2[:, :, np.newaxis]
|
31 |
|
|
|
|
|
|
|
32 |
return img
|
33 |
|
34 |
def inference(img):
|
@@ -48,4 +34,4 @@ gr.Interface(
|
|
48 |
description="Ingresa una imagen para convertirla a silueta",
|
49 |
article="",
|
50 |
css="Footer {visibility: hidden}"
|
51 |
-
).launch()
|
|
|
2 |
os.system('/usr/local/bin/python -m pip install --upgrade pip')
|
3 |
os.system("pip install opencv-python")
|
4 |
os.system("pip install pillow")
|
5 |
+
os.system("pip install rembg")
|
6 |
|
7 |
import gradio as gr
|
8 |
import cv2
|
9 |
import numpy as np
|
10 |
from PIL import Image
|
11 |
|
12 |
+
import rembg
|
13 |
+
from rembg import remove
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
def remove_background(img):
|
16 |
+
img = Image.open(img)
|
17 |
+
img = rembg.remove(img)
|
18 |
return img
|
19 |
|
20 |
def inference(img):
|
|
|
34 |
description="Ingresa una imagen para convertirla a silueta",
|
35 |
article="",
|
36 |
css="Footer {visibility: hidden}"
|
37 |
+
).launch()
|