-
Notifications
You must be signed in to change notification settings - Fork 340
/
dlibdetect.py
48 lines (41 loc) · 1.81 KB
/
dlibdetect.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
from detect import ObjectDetector
import dlib
import cv2
FACE_PAD = 50
class FaceDetectorDlib(ObjectDetector):
def __init__(self, model_name, basename='frontal-face', tgtdir='.'):
self.tgtdir = tgtdir
self.basename = basename
self.detector = dlib.get_frontal_face_detector()
self.predictor = dlib.shape_predictor(model_name)
def run(self, image_file):
print(image_file)
img = cv2.imread(image_file)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = self.detector(gray, 1)
images = []
bb = []
for (i, rect) in enumerate(faces):
x = rect.left()
y = rect.top()
w = rect.right() - x
h = rect.bottom() - y
bb.append((x,y,w,h))
images.append(self.sub_image('%s/%s-%d.jpg' % (self.tgtdir, self.basename, i + 1), img, x, y, w, h))
print('%d faces detected' % len(images))
for (x, y, w, h) in bb:
self.draw_rect(img, x, y, w, h)
# Fix in case nothing found in the image
outfile = '%s/%s.jpg' % (self.tgtdir, self.basename)
cv2.imwrite(outfile, img)
return images, outfile
def sub_image(self, name, img, x, y, w, h):
upper_cut = [min(img.shape[0], y + h + FACE_PAD), min(img.shape[1], x + w + FACE_PAD)]
lower_cut = [max(y - FACE_PAD, 0), max(x - FACE_PAD, 0)]
roi_color = img[lower_cut[0]:upper_cut[0], lower_cut[1]:upper_cut[1]]
cv2.imwrite(name, roi_color)
return name
def draw_rect(self, img, x, y, w, h):
upper_cut = [min(img.shape[0], y + h + FACE_PAD), min(img.shape[1], x + w + FACE_PAD)]
lower_cut = [max(y - FACE_PAD, 0), max(x - FACE_PAD, 0)]
cv2.rectangle(img, (lower_cut[1], lower_cut[0]), (upper_cut[1], upper_cut[0]), (255, 0, 0), 2)