-
Notifications
You must be signed in to change notification settings - Fork 4
/
detector.py
117 lines (107 loc) · 3.98 KB
/
detector.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import cv2
import json
import os
import argparse
import time
import numpy as np
from PIL import Image
import torchvision.transforms as transforms
from face_detection import FaceDetector
from face_detection.config import cfg as detection_cfg
from datasets import utils
parser = argparse.ArgumentParser()
parser.add_argument('--dataset', dest='dataset', help='dataset name', default='', type=str)
parser.add_argument('--root_dir', dest='root_dir', help='Path to test dataset', default='', type=str)
parser.add_argument('--filename_list', dest='filename_list', help='filename list of images', default='', type=str)
args = parser.parse_args()
def get_list_from_filenames(file_path):
# input: relative path to .txt file with file names
# output: list of relative path names
with open(file_path) as f:
lines = f.read().splitlines()
return lines
def show_Pose_300w_LP():
face_detector = FaceDetector(detection_cfg)
root_dir = args.root_dir
filenamelist = get_list_from_filenames(args.filename_list)
for image_name in filenamelist:
image_path = os.path.join(root_dir, image_name +".jpg")
mat_path = os.path.join(root_dir, image_name + '.mat')
# Crop the face loosely
pt2d = utils.get_pt2d_from_mat(mat_path)
x_min = min(pt2d[0,:])
y_min = min(pt2d[1,:])
x_max = max(pt2d[0,:])
y_max = max(pt2d[1,:])
# k = 0.2 to 0.40
k = np.random.random_sample() * 0.2 + 0.2
x_min -= 0.6 * k * abs(x_max - x_min)
y_min -= 2 * k * abs(y_max - y_min)
x_max += 0.6 * k * abs(x_max - x_min)
y_max += 0.6 * k * abs(y_max - y_min)
bboxes = np.array([[x_min,y_min, x_max, y_max, 1.0]])
image = cv2.imread(image_path)
image = face_detector.draw_bboxes(image, bboxes)
cv2.imshow('image', image)
k = cv2.waitKey(500)
def show_AFLW2000():
face_detector = FaceDetector(detection_cfg)
root_dir = args.root_dir
filenamelist = get_list_from_filenames(args.filename_list)
for image_name in filenamelist:
image_path = os.path.join(root_dir, image_name +".jpg")
mat_path = os.path.join(root_dir, image_name + '.mat')
# Crop the face loosely
pt2d = utils.get_pt2d_from_mat(mat_path)
x_min = min(pt2d[0,:])
y_min = min(pt2d[1,:])
x_max = max(pt2d[0,:])
y_max = max(pt2d[1,:])
k = 0.20
x_min -= 0.6 * k * abs(x_max - x_min)
y_min -= 2 * k * abs(y_max - y_min)
x_max += 0.6 * k * abs(x_max - x_min)
y_max += 0.6 * k * abs(y_max - y_min)
bboxes = np.array([[x_min,y_min, x_max, y_max, 1.0]])
image = cv2.imread(image_path)
image = face_detector.draw_bboxes(image, bboxes)
cv2.imshow('image', image)
k = cv2.waitKey(500)
return
def main():
dataset = args.dataset
root_dir = args.root_dir
filenamelist = get_list_from_filenames(args.filename_list)
face_detector = FaceDetector(detection_cfg)
count = 0
det_count =0
faceboxes = {}
if dataset == 'BIWI':
img_ext='_rgb.png'
else:
img_ext = '.jpg'
for image_name in filenamelist:
image_path = os.path.join(root_dir, image_name + img_ext)
image = cv2.imread(image_path)
bboxes = face_detector.detect_faces(image)
count += 1
if len(bboxes) > 0:
det_count +=1
print("images, %s, detected face %s" %(count, det_count))
x_min, y_min, x_max, y_max = bboxes[0][:4]
xmin = int(x_min)
ymin = int(y_min)
xmax = int(x_max)
ymax = int(y_max)
bbox = [xmin, ymin, xmax, ymax]
faceboxes[image_name] = bbox
# if count >10:
# break
headpose_file = os.path.join('datasets', 'filenamelists', dataset + '_facebbox_disgard.json')
with open(headpose_file, 'w') as f:
json.dump(faceboxes, f, indent=4)
return
if __name__== '__main__':
#show_AFLW2000()
#show_Pose_300w_LP()
main()