-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmake_Bbox.py
74 lines (57 loc) · 2.48 KB
/
make_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
65
66
67
68
69
70
71
72
73
74
'''
This Code is made for pre-processing
the The Face Synthetics dataset published from
https://github.com/microsoft/FaceSynthetics
'''
import natsort
import os
from PIL import Image
from retinaface import RetinaFace
import matplotlib.pyplot as plt
import argparse
parser = argparse.ArgumentParser()
#parser.add_argument('--name', type=str)
parser.add_argument('--datasetPath', type=str, default= "/data2/MS-FaceSynthetic")
parser.add_argument('--saveDir', type=str, default= "bbox_leftcorner_coord")
args = parser.parse_args()
root = args.datasetPath
img_path = os.path.join(root, "img")
img_list = natsort.natsorted(os.listdir(img_path))
X_mean = 0
Y_mean = 0
num_of_None_detection = 0
print("total length: ", len(img_list))
bbox_leftcorner_coord_path = os.path.join(root, args.saveDir) # per-defined floder
for idx in range(len(img_list)):
print(idx, " ing ...")
num_str = str(idx).zfill(6)
file = open(bbox_leftcorner_coord_path + "/" + num_str +"_bbox.txt", "w")
resp = RetinaFace.detect_faces(img_path = img_path + '/' + img_list[idx])
#print(resp["face_1"]['facial_area']) # [201, 158, 383, 407] -> x1, y1 , x2, y2
#resp["face_1"]['facial_area'][:2][0] # y coordinate left corner
#resp["face_1"]['facial_area'][:2][1] # x coordinate left corner
try:
#check whether it is detected
X_mean = resp["face_1"]['facial_area'][2] - resp["face_1"]['facial_area'][0]
#X_extra = 128 - resp["face_1"]['facial_area'][:2][0]
if resp["face_1"]['facial_area'][0] + 256 >= 512:
resp["face_1"]['facial_area'][0] = 255
if resp["face_1"]['facial_area'][1] + 256 >= 512:
resp["face_1"]['facial_area'][1] = 255
#img = Image.open(img_path + '/' + img_list[idx])
#plt.imshow(img.crop((resp["face_1"]['facial_area'][0], resp["face_1"]['facial_area'][1], resp["face_1"]['facial_area'][0]+ 256, resp["face_1"]['facial_area'][1] + 256)))
#plt.savefig('bbox_image_here.png')
X_extra = (256 - (resp["face_1"]['facial_area'][2] - resp["face_1"]['facial_area'][0]))//2
file.write(str(resp["face_1"]['facial_area'][0]- X_extra))
file.write(" ")
file.write(str(resp["face_1"]['facial_area'][1]))
except TypeError:
num_of_None_detection += 1
#assume centered box
file.write("128")
file.write(" ")
file.write("128")
pass
file.close()
print("num_of_None_detection: ", num_of_None_detection)
print("Done")