Spaces:
Runtime error
Runtime error
Commit
Β·
f9787a2
1
Parent(s):
65f5da2
Update app.py
Browse files
app.py
CHANGED
@@ -1,134 +1,51 @@
|
|
1 |
import os
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
os.system('/usr/local/bin/python -m pip install --upgrade pip')
|
7 |
-
|
8 |
-
|
9 |
os.system("pip install opencv-python")
|
10 |
-
|
11 |
-
|
12 |
os.system("pip install pillow")
|
13 |
|
14 |
-
|
15 |
-
os.system("pip install rembg")
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
import gradio as gr
|
24 |
-
|
25 |
-
|
26 |
import cv2
|
27 |
-
|
28 |
-
|
29 |
import numpy as np
|
30 |
-
|
31 |
-
|
32 |
from PIL import Image
|
33 |
|
|
|
|
|
|
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
import rembg
|
44 |
-
|
45 |
-
|
46 |
-
from rembg import remove
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
def inference(img):
|
55 |
-
|
56 |
-
|
57 |
-
input_img = cv2.imread(img)
|
58 |
-
|
59 |
-
|
60 |
-
output = remove(input_img[:, :, [2,1,0]])
|
61 |
-
|
62 |
-
|
63 |
-
return output
|
64 |
-
|
65 |
|
|
|
|
|
66 |
|
|
|
|
|
67 |
|
|
|
|
|
68 |
|
|
|
|
|
69 |
|
|
|
70 |
|
71 |
def inference(img):
|
72 |
-
|
73 |
-
|
74 |
-
input_img = cv2.imread(img)
|
75 |
-
|
76 |
-
|
77 |
-
gray = cv2.cvtColor(input_img, cv2.COLOR_BGR2GRAY)
|
78 |
-
|
79 |
-
|
80 |
edges = cv2.Canny(gray, 50, 150)
|
81 |
-
|
82 |
-
|
83 |
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
84 |
-
|
85 |
-
|
86 |
-
silhouette = np.zeros_like(input_img)
|
87 |
-
|
88 |
-
|
89 |
cv2.drawContours(silhouette, contours, -1, (255, 255, 255), 1)
|
90 |
-
|
91 |
-
|
92 |
return silhouette
|
93 |
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
# Crear interfaz de usuario con Gradio
|
106 |
-
|
107 |
-
|
108 |
gr.Interface(
|
109 |
-
|
110 |
-
|
111 |
inference,
|
112 |
-
|
113 |
-
|
114 |
gr.inputs.Image(type="filepath", label="Input"),
|
115 |
-
|
116 |
-
|
117 |
gr.outputs.Image(type="numpy", label="Output"),
|
118 |
-
|
119 |
-
|
120 |
title="Convertir a silueta",
|
121 |
-
|
122 |
-
|
123 |
description="Ingresa una imagen para convertirla a silueta",
|
124 |
-
|
125 |
-
|
126 |
article="",
|
127 |
-
|
128 |
-
|
129 |
css="Footer {visibility: hidden}"
|
130 |
-
|
131 |
-
|
132 |
).launch()
|
133 |
-
|
134 |
-
|
|
|
1 |
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 |
+
def remove_background(img):
|
12 |
+
# Load the image
|
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):
|
35 |
+
output = remove_background(img)
|
36 |
+
gray = cv2.cvtColor(output, cv2.COLOR_BGR2GRAY)
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
edges = cv2.Canny(gray, 50, 150)
|
|
|
|
|
38 |
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
39 |
+
silhouette = np.zeros_like(output)
|
|
|
|
|
|
|
|
|
40 |
cv2.drawContours(silhouette, contours, -1, (255, 255, 255), 1)
|
|
|
|
|
41 |
return silhouette
|
42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
gr.Interface(
|
|
|
|
|
44 |
inference,
|
|
|
|
|
45 |
gr.inputs.Image(type="filepath", label="Input"),
|
|
|
|
|
46 |
gr.outputs.Image(type="numpy", label="Output"),
|
|
|
|
|
47 |
title="Convertir a silueta",
|
|
|
|
|
48 |
description="Ingresa una imagen para convertirla a silueta",
|
|
|
|
|
49 |
article="",
|
|
|
|
|
50 |
css="Footer {visibility: hidden}"
|
|
|
|
|
51 |
).launch()
|
|
|
|