1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2016 Canonical
7
# This program is free software; you can redistribute it and/or modify it under
8
# the terms of the GNU General Public License as published by the Free Software
9
# Foundation; version 3.
11
# This program is distributed in the hope that it will be useful, but WITHOUT
12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16
# You should have received a copy of the GNU General Public License along with
17
# this program; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26
from datahandler import DataHandler
27
from settings import LAST_SCREENSHOT
28
from tools import Singleton, get_data_path
30
logger = logging.getLogger(__name__)
33
class FaceDetection(object):
34
"""Face detection handler class"""
35
__metaclass__ = Singleton
37
# Create the filter cascade
38
faceCascade = cv2.CascadeClassifier(os.path.join(os.path.dirname(__file__), '..', 'logic.xml'))
41
"""Save last screenshot at """
42
self.screenshot_path = os.path.join(get_data_path(), LAST_SCREENSHOT)
44
def detect_faces(self):
45
logger.debug("Detect faces")
47
video_capture = cv2.VideoCapture(0)
48
ret, image = video_capture.read()
49
video_capture.release()
51
# Detect faces in the image
52
faces = self.faceCascade.detectMultiScale(
57
flags=cv2.CASCADE_SCALE_IMAGE
60
# Draw a rectangle around the faces
61
num_faces = len(faces)
67
logger.debug("{} faces detected".format(num_faces))
68
# Draw a rectangle around the faces
69
for (x, y, w, h) in faces:
70
cv2.rectangle(image, (x, y), (x + w, y + h), (255, 36, 36), 5)
72
temp_file = "{}.new.png".format(self.screenshot_path)
73
cv2.imwrite(temp_file, image)
74
os.rename(temp_file, self.screenshot_path)
77
DataHandler().add_one_facedetect_entry(int(time()), num_faces)