This repository was archived by the owner on Oct 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdetect_face.py
More file actions
78 lines (62 loc) · 2.53 KB
/
Copy pathdetect_face.py
File metadata and controls
78 lines (62 loc) · 2.53 KB
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
import cv2
import argparse
import time
import numpy as np
class detectFaces:
def __init__(self):
self.detector = cv2.dnn.readNetFromCaffe(
'deploy.prototxt', 'res10_300x300_ssd_iter_140000.caffemodel')
self.embedder = cv2.dnn.readNetFromTorch('openface_nn4.small2.v1.t7')
def detectSingle(self, image):
(h, w) = image.shape[:2]
imageBlob = cv2.dnn.blobFromImage(
cv2.resize(image, (300, 300)), 1.0, (300, 300),
(104.0, 177.0, 123.0), swapRB=False, crop=False)
self.detector.setInput(imageBlob)
detections = self.detector.forward()
if len(detections) > 0:
i = np.argmax(detections[0, 0, :, 2])
confidence = detections[0, 0, i, 2]
if confidence > 0.3:
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
(startX, startY, endX, endY) = box.astype("int")
return [startX, startY, endX, endY]
return [0, 0, 0, 0]
def detectMultiple(self, image):
(h, w) = image.shape[:2]
imageBlob = cv2.dnn.blobFromImage(
cv2.resize(image, (300, 300)), 1.0, (300, 300),
(104.0, 177.0, 123.0), swapRB=False, crop=False)
self.detector.setInput(imageBlob)
detections = self.detector.forward()
if len(detections) == 0:
return [[0, 0, 0, 0]]
boundingBoxes = []
for i in range(0, detections.shape[2]):
confidence = detections[0, 0, i, 2]
if confidence > 0.3:
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
(startX, startY, endX, endY) = box.astype("int")
boundingBoxes.append((startX, startY, endX, endY))
return boundingBoxes
def getFaceEncoding(self, face):
faceBlob = cv2.dnn.blobFromImage(face, 1.0 / 255, (96, 96),
(0, 0, 0), swapRB=True, crop=False)
self.embedder.setInput(faceBlob)
return np.array(self.embedder.forward())
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--image', required=True)
args = parser.parse_args()
detector = detectFaces()
img = cv2.imread(args.image)
img = cv2.resize(img, (960, 720))
while True:
# test detectSingle
start = time.time()
detector.detectSingle(img)
print(time.time() - start)
# test detectMultiple
start = time.time()
detector.detectMultiple(img)
print(time.time() - start)