Spaces:
Running
Running
Update SegBody.py
Browse files- SegBody.py +79 -2
SegBody.py
CHANGED
@@ -1,3 +1,80 @@
|
|
1 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import spaces
|
2 |
+
from transformers import pipeline
|
3 |
+
import numpy as np
|
4 |
+
import cv2
|
5 |
+
import insightface
|
6 |
+
from insightface.app import FaceAnalysis
|
7 |
+
from PIL import Image, ImageDraw
|
8 |
|
9 |
+
|
10 |
+
# Initialize face detection
|
11 |
+
#app = FaceAnalysis(providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
|
12 |
+
app = FaceAnalysis(providers=['CUDAExecutionProvider'])
|
13 |
+
app.prepare(ctx_id=0, det_size=(640, 640))
|
14 |
+
|
15 |
+
# Initialize segmentation pipeline
|
16 |
+
segmenter = pipeline(model="mattmdjaga/segformer_b2_clothes", device="cuda")
|
17 |
+
|
18 |
+
@spaces.GPU(enable_queue=True)
|
19 |
+
def remove_face(img, mask):
|
20 |
+
# Convert image to numpy array
|
21 |
+
img_arr = np.asarray(img)
|
22 |
+
|
23 |
+
# Run face detection
|
24 |
+
faces = app.get(img_arr)
|
25 |
+
|
26 |
+
# Get the first face
|
27 |
+
faces = faces[0]['bbox']
|
28 |
+
|
29 |
+
# Width and height of face
|
30 |
+
w = faces[2] - faces[0]
|
31 |
+
h = faces[3] - faces[1]
|
32 |
+
|
33 |
+
# Make face locations bigger
|
34 |
+
faces[0] = faces[0] - (w*0.5) # x left
|
35 |
+
faces[2] = faces[2] + (w*0.5) # x right
|
36 |
+
faces[1] = faces[1] - (h*0.5) # y top
|
37 |
+
faces[3] = faces[3] + (h*0.2) # y bottom
|
38 |
+
|
39 |
+
# Convert to [(x_left, y_top), (x_right, y_bottom)]
|
40 |
+
face_locations = [(faces[0], faces[1]), (faces[2], faces[3])]
|
41 |
+
|
42 |
+
# Draw black rect onto mask
|
43 |
+
img1 = ImageDraw.Draw(mask)
|
44 |
+
img1.rectangle(face_locations, fill=0)
|
45 |
+
|
46 |
+
return mask
|
47 |
+
|
48 |
+
@spaces.GPU(enable_queue=True)
|
49 |
+
def segment_body(original_img, face=True):
|
50 |
+
# Make a copy
|
51 |
+
img = original_img.copy()
|
52 |
+
|
53 |
+
# Segment image
|
54 |
+
segments = segmenter(img)
|
55 |
+
|
56 |
+
# Create list of masks
|
57 |
+
segment_include = ["Hat", "Hair", "Sunglasses", "Upper-clothes", "Skirt", "Pants", "Dress", "Belt", "Left-shoe", "Right-shoe", "Face", "Left-leg", "Right-leg", "Left-arm", "Right-arm", "Bag","Scarf"]
|
58 |
+
mask_list = []
|
59 |
+
for s in segments:
|
60 |
+
if(s['label'] in segment_include):
|
61 |
+
mask_list.append(s['mask'])
|
62 |
+
|
63 |
+
|
64 |
+
# Paste all masks on top of eachother
|
65 |
+
final_mask = np.array(mask_list[0])
|
66 |
+
for mask in mask_list:
|
67 |
+
current_mask = np.array(mask)
|
68 |
+
final_mask = final_mask + current_mask
|
69 |
+
|
70 |
+
# Convert final mask from np array to PIL image
|
71 |
+
final_mask = Image.fromarray(final_mask)
|
72 |
+
|
73 |
+
# Remove face
|
74 |
+
if(face==False):
|
75 |
+
final_mask = remove_face(img.convert('RGB'), final_mask)
|
76 |
+
|
77 |
+
# Apply mask to original image
|
78 |
+
img.putalpha(final_mask)
|
79 |
+
|
80 |
+
return img, final_mask
|