Added Segmentation
Browse files- SegBody.py +110 -0
SegBody.py
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
import numpy as np
|
| 3 |
+
import cv2
|
| 4 |
+
import insightface
|
| 5 |
+
from insightface.app import FaceAnalysis
|
| 6 |
+
from PIL import Image, ImageDraw
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
# Initialize face detection
|
| 10 |
+
app = FaceAnalysis(providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
|
| 11 |
+
app.prepare(ctx_id=0, det_size=(640, 640))
|
| 12 |
+
|
| 13 |
+
# Initialize segmentation pipeline
|
| 14 |
+
segmenter = pipeline(model="mattmdjaga/segformer_b2_clothes")
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def remove_face(img, mask):
|
| 18 |
+
# Convert image to numpy array
|
| 19 |
+
img_arr = np.asarray(img)
|
| 20 |
+
|
| 21 |
+
# Run face detection
|
| 22 |
+
faces = app.get(img_arr)
|
| 23 |
+
|
| 24 |
+
# Get the first face
|
| 25 |
+
faces = faces[0]['bbox']
|
| 26 |
+
|
| 27 |
+
# Width and height of face
|
| 28 |
+
w = faces[2] - faces[0]
|
| 29 |
+
h = faces[3] - faces[1]
|
| 30 |
+
|
| 31 |
+
# Make face locations bigger
|
| 32 |
+
faces[0] = faces[0] - (w*0.5) # x left
|
| 33 |
+
faces[2] = faces[2] + (w*0.5) # x right
|
| 34 |
+
faces[1] = faces[1] - (h*0.5) # y top
|
| 35 |
+
faces[3] = faces[3] + (h*0.2) # y bottom
|
| 36 |
+
|
| 37 |
+
# Convert to [(x_left, y_top), (x_right, y_bottom)]
|
| 38 |
+
face_locations = [(faces[0], faces[1]), (faces[2], faces[3])]
|
| 39 |
+
|
| 40 |
+
# Draw black rect onto mask
|
| 41 |
+
img1 = ImageDraw.Draw(mask)
|
| 42 |
+
img1.rectangle(face_locations, fill=0)
|
| 43 |
+
|
| 44 |
+
return mask
|
| 45 |
+
|
| 46 |
+
def segment_body(original_img, face=True):
|
| 47 |
+
# Make a copy
|
| 48 |
+
img = original_img.copy()
|
| 49 |
+
|
| 50 |
+
# Segment image
|
| 51 |
+
segments = segmenter(img)
|
| 52 |
+
|
| 53 |
+
# Create list of masks
|
| 54 |
+
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"]
|
| 55 |
+
mask_list = []
|
| 56 |
+
for s in segments:
|
| 57 |
+
if(s['label'] in segment_include):
|
| 58 |
+
mask_list.append(s['mask'])
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
# Paste all masks on top of eachother
|
| 62 |
+
final_mask = np.array(mask_list[0])
|
| 63 |
+
for mask in mask_list:
|
| 64 |
+
current_mask = np.array(mask)
|
| 65 |
+
final_mask = final_mask + current_mask
|
| 66 |
+
|
| 67 |
+
# Convert final mask from np array to PIL image
|
| 68 |
+
final_mask = Image.fromarray(final_mask)
|
| 69 |
+
|
| 70 |
+
# Remove face
|
| 71 |
+
if(face==False):
|
| 72 |
+
final_mask = remove_face(img.convert('RGB'), final_mask)
|
| 73 |
+
|
| 74 |
+
# Apply mask to original image
|
| 75 |
+
img.putalpha(final_mask)
|
| 76 |
+
|
| 77 |
+
return img, final_mask
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def segment_torso(original_img):
|
| 81 |
+
# Make a copy
|
| 82 |
+
img = original_img.copy()
|
| 83 |
+
|
| 84 |
+
# Segment image
|
| 85 |
+
segments = segmenter(img)
|
| 86 |
+
|
| 87 |
+
# Create list of masks
|
| 88 |
+
segment_include = ["Upper-clothes", "Dress", "Belt", "Face", "Left-arm", "Right-arm"]
|
| 89 |
+
mask_list = []
|
| 90 |
+
for s in segments:
|
| 91 |
+
if(s['label'] in segment_include):
|
| 92 |
+
mask_list.append(s['mask'])
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
# Paste all masks on top of eachother
|
| 96 |
+
final_mask = np.array(mask_list[0])
|
| 97 |
+
for mask in mask_list:
|
| 98 |
+
current_mask = np.array(mask)
|
| 99 |
+
final_mask = final_mask + current_mask
|
| 100 |
+
|
| 101 |
+
# Convert final mask from np array to PIL image
|
| 102 |
+
final_mask = Image.fromarray(final_mask)
|
| 103 |
+
|
| 104 |
+
# Remove face
|
| 105 |
+
final_mask = remove_face(img.convert('RGB'), final_mask)
|
| 106 |
+
|
| 107 |
+
# Apply mask to original image
|
| 108 |
+
img.putalpha(final_mask)
|
| 109 |
+
|
| 110 |
+
return img, final_mask
|