|
|
|
|
|
|
|
import torch |
|
import numpy as np |
|
from torchvision import models, transforms |
|
from PIL import Image |
|
|
|
|
|
model = models.segmentation.deeplabv3_resnet101(pretrained=True) |
|
model.eval() |
|
|
|
|
|
preprocess = transforms.Compose([ |
|
transforms.ToTensor(), |
|
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), |
|
]) |
|
|
|
|
|
def extract_person(image): |
|
input_tensor = preprocess(image).unsqueeze(0) |
|
|
|
|
|
with torch.no_grad(): |
|
output = model(input_tensor)['out'][0] |
|
output_predictions = output.argmax(0) |
|
|
|
|
|
|
|
person_mask = (output_predictions == 15).cpu().numpy() |
|
|
|
if not person_mask.any(): |
|
return None |
|
|
|
|
|
binary_mask = np.where(person_mask, 1, 0).astype(np.uint8) |
|
|
|
|
|
extracted_person = Image.new("RGBA", image.size, (0, 0, 0, 0)) |
|
for y in range(image.size[1]): |
|
for x in range(image.size[0]): |
|
|
|
if binary_mask[y, x] == 1: |
|
extracted_person.putpixel((x, y), image.getpixel((x, y))) |
|
|
|
|
|
|
|
|
|
target_width = 800 |
|
aspect_ratio = image.size[1] / image.size[0] |
|
target_height = int(target_width * aspect_ratio) |
|
extracted_person = extracted_person.resize((target_width, target_height)) |
|
|
|
|
|
return extracted_person |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|