-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathextract_bbox.py
64 lines (57 loc) · 2 KB
/
extract_bbox.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from os.path import basename, exists
import os
from glob import glob
from tqdm import tqdm
from argparse import ArgumentParser
from skimage import io, img_as_float32
import numpy as np
import torch
import face_detection
fa = face_detection.FaceAlignment(face_detection.LandmarksType._2D, flip_input=False, device='cuda')
def detect_bbox(img_names):
bboxs = []
for img_name in img_names:
img = img_as_float32(io.imread(img_name)).transpose((2, 0, 1))
img = np.transpose(img[np.newaxis], (0,2,3,1))[...,::-1]
bbox = fa.get_detections_for_batch(img*255)
if bbox is not None:
bboxs.append(bbox[0])
else:
bboxs.append(None)
assert(len(bboxs)==len(img_names))
return bboxs
def main(args):
# file_images = glob('/data5/gy/mead/images_evp_25/t*/*')
# file_images = glob('/data/gy/vox/voxs_images/*')
# file_images = glob('/data/gy/vox/voxs_images/*')
file_images = glob('/data2/gy/lrw/lrw_images/*')
file_images.sort()
#file_images = file_images[6669*24:6669*25]
p = args.part
# t = len(file_images)//9 + 1
t = len(file_images)
#f = open('bboxs_extract.txt', 'w')
for fi in tqdm(file_images[t*p:t*(p+1)]):
out = basename(fi)
# outpath =f'./bboxs/{out}.npy's
outpath =f'/data2/gy/lrw/lrw_bbox/{out}.npy'
if exists(outpath):
if exists(outpath):
try:
np.load(outpath, allow_pickle=True)
continue
except:
# f.write(out+'\n')
print(outpath)
#else:
#f.write(out+'\n')
images = glob(fi+'/*.jpg')
images.sort()
bboxs = detect_bbox(images)
np.save(outpath, bboxs)
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("--files", default="*", help="filenames")
parser.add_argument("--part", default="0", type=int, help="part")
args = parser.parse_args()
main(args)