Spaces:
Paused
Paused
Delete src
Browse files- src/dataset/test_preprocess.py +0 -155
src/dataset/test_preprocess.py
DELETED
|
@@ -1,155 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import numpy as np
|
| 3 |
-
from PIL import Image
|
| 4 |
-
import torch
|
| 5 |
-
import torchvision.transforms as transforms
|
| 6 |
-
from transformers import CLIPImageProcessor
|
| 7 |
-
import librosa
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
def process_bbox(bbox, expand_radio, height, width):
|
| 11 |
-
"""
|
| 12 |
-
raw_vid_path:
|
| 13 |
-
bbox: format: x1, y1, x2, y2
|
| 14 |
-
radio: expand radio against bbox size
|
| 15 |
-
height,width: source image height and width
|
| 16 |
-
"""
|
| 17 |
-
|
| 18 |
-
def expand(bbox, ratio, height, width):
|
| 19 |
-
|
| 20 |
-
bbox_h = bbox[3] - bbox[1]
|
| 21 |
-
bbox_w = bbox[2] - bbox[0]
|
| 22 |
-
|
| 23 |
-
expand_x1 = max(bbox[0] - ratio * bbox_w, 0)
|
| 24 |
-
expand_y1 = max(bbox[1] - ratio * bbox_h, 0)
|
| 25 |
-
expand_x2 = min(bbox[2] + ratio * bbox_w, width)
|
| 26 |
-
expand_y2 = min(bbox[3] + ratio * bbox_h, height)
|
| 27 |
-
|
| 28 |
-
return [expand_x1,expand_y1,expand_x2,expand_y2]
|
| 29 |
-
|
| 30 |
-
def to_square(bbox_src, bbox_expend, height, width):
|
| 31 |
-
|
| 32 |
-
h = bbox_expend[3] - bbox_expend[1]
|
| 33 |
-
w = bbox_expend[2] - bbox_expend[0]
|
| 34 |
-
c_h = (bbox_expend[1] + bbox_expend[3]) / 2
|
| 35 |
-
c_w = (bbox_expend[0] + bbox_expend[2]) / 2
|
| 36 |
-
|
| 37 |
-
c = min(h, w) / 2
|
| 38 |
-
|
| 39 |
-
c_src_h = (bbox_src[1] + bbox_src[3]) / 2
|
| 40 |
-
c_src_w = (bbox_src[0] + bbox_src[2]) / 2
|
| 41 |
-
|
| 42 |
-
s_h, s_w = 0, 0
|
| 43 |
-
if w < h:
|
| 44 |
-
d = abs((h - w) / 2)
|
| 45 |
-
s_h = min(d, abs(c_src_h-c_h))
|
| 46 |
-
s_h = s_h if c_src_h > c_h else s_h * (-1)
|
| 47 |
-
else:
|
| 48 |
-
d = abs((h - w) / 2)
|
| 49 |
-
s_w = min(d, abs(c_src_w-c_w))
|
| 50 |
-
s_w = s_w if c_src_w > c_w else s_w * (-1)
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
c_h = (bbox_expend[1] + bbox_expend[3]) / 2 + s_h
|
| 54 |
-
c_w = (bbox_expend[0] + bbox_expend[2]) / 2 + s_w
|
| 55 |
-
|
| 56 |
-
square_x1 = c_w - c
|
| 57 |
-
square_y1 = c_h - c
|
| 58 |
-
square_x2 = c_w + c
|
| 59 |
-
square_y2 = c_h + c
|
| 60 |
-
|
| 61 |
-
x1, y1, x2, y2 = square_x1, square_y1, square_x2, square_y2
|
| 62 |
-
ww = x2 - x1
|
| 63 |
-
hh = y2 - y1
|
| 64 |
-
cc_x = (x1 + x2)/2
|
| 65 |
-
cc_y = (y1 + y2)/2
|
| 66 |
-
# 1:1
|
| 67 |
-
ww = hh = min(ww, hh)
|
| 68 |
-
x1, x2 = round(cc_x - ww/2), round(cc_x + ww/2)
|
| 69 |
-
y1, y2 = round(cc_y - hh/2), round(cc_y + hh/2)
|
| 70 |
-
|
| 71 |
-
return [round(x1), round(y1), round(x2), round(y2)]
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
bbox_expend = expand(bbox, expand_radio, height=height, width=width)
|
| 75 |
-
processed_bbox = to_square(bbox, bbox_expend, height=height, width=width)
|
| 76 |
-
|
| 77 |
-
return processed_bbox
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
def get_audio_feature(audio_path, feature_extractor):
|
| 81 |
-
audio_input, sampling_rate = librosa.load(audio_path, sr=16000)
|
| 82 |
-
assert sampling_rate == 16000
|
| 83 |
-
|
| 84 |
-
audio_features = []
|
| 85 |
-
window = 750*640
|
| 86 |
-
for i in range(0, len(audio_input), window):
|
| 87 |
-
audio_feature = feature_extractor(audio_input[i:i+window],
|
| 88 |
-
sampling_rate=sampling_rate,
|
| 89 |
-
return_tensors="pt",
|
| 90 |
-
).input_features
|
| 91 |
-
audio_features.append(audio_feature)
|
| 92 |
-
audio_features = torch.cat(audio_features, dim=-1)
|
| 93 |
-
return audio_features, len(audio_input) // 640
|
| 94 |
-
|
| 95 |
-
def image_audio_to_tensor(align_instance, feature_extractor, image_path, audio_path, limit=100, image_size=512, area=1.25):
|
| 96 |
-
|
| 97 |
-
clip_processor = CLIPImageProcessor()
|
| 98 |
-
|
| 99 |
-
to_tensor = transforms.Compose([
|
| 100 |
-
transforms.ToTensor(),
|
| 101 |
-
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
|
| 102 |
-
])
|
| 103 |
-
mask_to_tensor = transforms.Compose([
|
| 104 |
-
transforms.ToTensor(),
|
| 105 |
-
])
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
imSrc_ = Image.open(image_path).convert('RGB')
|
| 109 |
-
w, h = imSrc_.size
|
| 110 |
-
|
| 111 |
-
_, _, bboxes_list = align_instance(np.array(imSrc_)[:,:,[2,1,0]], maxface=True)
|
| 112 |
-
|
| 113 |
-
if len(bboxes_list) == 0:
|
| 114 |
-
return None
|
| 115 |
-
bboxSrc = bboxes_list[0]
|
| 116 |
-
|
| 117 |
-
x1, y1, ww, hh = bboxSrc
|
| 118 |
-
x2, y2 = x1 + ww, y1 + hh
|
| 119 |
-
|
| 120 |
-
mask_img = np.zeros_like(np.array(imSrc_))
|
| 121 |
-
ww, hh = (x2-x1) * area, (y2-y1) * area
|
| 122 |
-
center = [(x2+x1)//2, (y2+y1)//2]
|
| 123 |
-
x1 = max(center[0] - ww//2, 0)
|
| 124 |
-
y1 = max(center[1] - hh//2, 0)
|
| 125 |
-
x2 = min(center[0] + ww//2, w)
|
| 126 |
-
y2 = min(center[1] + hh//2, h)
|
| 127 |
-
mask_img[int(y1):int(y2), int(x1):int(x2)] = 255
|
| 128 |
-
mask_img = Image.fromarray(mask_img)
|
| 129 |
-
|
| 130 |
-
w, h = imSrc_.size
|
| 131 |
-
scale = image_size / min(w, h)
|
| 132 |
-
new_w = round(w * scale / 64) * 64
|
| 133 |
-
new_h = round(h * scale / 64) * 64
|
| 134 |
-
if new_h != h or new_w != w:
|
| 135 |
-
imSrc = imSrc_.resize((new_w, new_h), Image.LANCZOS)
|
| 136 |
-
mask_img = mask_img.resize((new_w, new_h), Image.LANCZOS)
|
| 137 |
-
else:
|
| 138 |
-
imSrc = imSrc_
|
| 139 |
-
|
| 140 |
-
clip_image = clip_processor(
|
| 141 |
-
images=imSrc.resize((224, 224), Image.LANCZOS), return_tensors="pt"
|
| 142 |
-
).pixel_values[0]
|
| 143 |
-
audio_input, audio_len = get_audio_feature(audio_path, feature_extractor)
|
| 144 |
-
|
| 145 |
-
audio_len = min(limit, audio_len)
|
| 146 |
-
|
| 147 |
-
sample = dict(
|
| 148 |
-
face_mask=mask_to_tensor(mask_img),
|
| 149 |
-
ref_img=to_tensor(imSrc),
|
| 150 |
-
clip_images=clip_image,
|
| 151 |
-
audio_feature=audio_input[0],
|
| 152 |
-
audio_len=audio_len
|
| 153 |
-
)
|
| 154 |
-
|
| 155 |
-
return sample
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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