diff --git a/facedetection.py b/facedetection.py index f449bb7..32f40fc 100644 --- a/facedetection.py +++ b/facedetection.py @@ -3,40 +3,37 @@ import cv2 -class FaceDetector(): - def __init__(self, detection_threshold=0.6): - self.mean = np.float32(np.array([-2.1179, -2.0357, -1.8044])) - self.std = np.float32(np.array([0.0171, 0.0175, 0.0174])) - self.detection_threshold = detection_threshold - self.targetimageSize = [224, 224] - self.image = None - def detect_faces(self, frame, model): - self.resizeImage(frame) +def detect_faces(frame, model, detection_threshold ): - outputs, _ = model.faceDetection.run([], {'input': self.image}) - outputs = outputs[0] - faceLocation = np.argmax(outputs[0].flatten()) - x = faceLocation % 56 - y = faceLocation // 56 + image = resizeImage(frame) - if outputs[0, y, x] < self.detection_threshold: - return None + outputs, _ = model.faceDetection.run([], {'input': image}) + outputs = outputs[0] + faceLocation = np.argmax(outputs[0].flatten()) + x = faceLocation % 56 + y = faceLocation // 56 - r = outputs[1, y, x] * 112. - results= (((x * 4) - r, (y * 4) - r, r*2,r*2)) - results = np.array(results).astype(np.float32) - results[[0,2]] *= frame.shape[1] / 224. - results[[1,3]] *= frame.shape[0] / 224. + if outputs[0, y, x] < detection_threshold: + return None - return results + r = outputs[1, y, x] * 112. + results= (((x * 4) - r, (y * 4) - r, r*2,r*2)) + results = np.array(results).astype(np.float32) + results[[0,2]] *= frame.shape[1] / 224. + results[[1,3]] *= frame.shape[0] / 224. - def resizeImage(self, frame): - image = cv2.resize(frame, self.targetimageSize, interpolation=cv2.INTER_LINEAR) * self.std + self.mean - image = np.expand_dims(image, 0) - image = np.transpose(image, (0,3,1,2)) - self.image = image + return results + +def resizeImage(frame): + mean = np.float32(np.array([-2.1179, -2.0357, -1.8044])) + std = np.float32(np.array([0.0171, 0.0175, 0.0174])) + targetimageSize = [224, 224] + image = cv2.resize(frame, targetimageSize, interpolation=cv2.INTER_LINEAR) * std + mean + image = np.expand_dims(image, 0) + image = np.transpose(image, (0,3,1,2)) + return image diff --git a/tracker.py b/tracker.py index da50a76..63eafee 100644 --- a/tracker.py +++ b/tracker.py @@ -52,7 +52,7 @@ class Models(): class Tracker(): def __init__(self, width, height, messageQueue, featureType, model_type=3, detection_threshold=0.6, threshold=0.6, silent=False): - self.FaceDetector = facedetection.FaceDetector(detection_threshold = detection_threshold) + self.detection_threshold = detection_threshold self.EyeTracker = eyes.EyeTracker() self.Landmarks = landmarks.Landmarks(width, height, threshold) self.model = Models(model_type = model_type) @@ -119,7 +119,7 @@ class Tracker(): if self.face is None: start_fd = time.perf_counter() - self.face = self.FaceDetector.detect_faces(frame, self.model) + self.face = facedetection.detect_faces(frame, self.model, self.detection_threshold) duration_fd = 1000 * (time.perf_counter() - start_fd) if self.face is None: return self.early_exit("No faces found", start)