~didrocks/+junk/face-detection-15.04

« back to all changes in this revision

Viewing changes to facedetection/facedetection.py

  • Committer: Didier Roche
  • Date: 2016-05-10 23:09:11 UTC
  • Revision ID: didier.roche@canonical.com-20160510230911-c7xr490zrj3yrzxd
New version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
# Copyright (C) 2016 Canonical
 
3
#
 
4
# Authors:
 
5
#  Didier Roche
 
6
#
 
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.
 
10
#
 
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
 
14
# details.
 
15
#
 
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
 
19
 
 
20
import logging
 
21
import os
 
22
import cv2
 
23
import numpy
 
24
from time import time
 
25
 
 
26
from datahandler import DataHandler
 
27
from settings import LAST_SCREENSHOT
 
28
from tools import Singleton, get_data_path
 
29
 
 
30
logger = logging.getLogger(__name__)
 
31
 
 
32
 
 
33
class FaceDetection(object):
 
34
    """Face detection handler class"""
 
35
    __metaclass__ = Singleton
 
36
 
 
37
    # Create the filter cascade
 
38
    faceCascade = cv2.CascadeClassifier(os.path.join(os.path.dirname(__file__), '..', 'logic.xml'))
 
39
 
 
40
    def __init__(self):
 
41
        """Save last screenshot at """
 
42
        self.screenshot_path = os.path.join(get_data_path(), LAST_SCREENSHOT)
 
43
 
 
44
    def detect_faces(self):
 
45
        logger.debug("Detect faces")
 
46
        # Read the image
 
47
        video_capture = cv2.VideoCapture(0)
 
48
        ret, image = video_capture.read()
 
49
        video_capture.release()
 
50
 
 
51
        # Detect faces in the image
 
52
        faces = self.faceCascade.detectMultiScale(
 
53
            image,
 
54
            scaleFactor=1.1,
 
55
            minNeighbors=5,
 
56
            minSize=(30, 30),
 
57
            flags=cv2.CASCADE_SCALE_IMAGE
 
58
        )
 
59
 
 
60
        # Draw a rectangle around the faces
 
61
        num_faces = len(faces)
 
62
 
 
63
        # Introduce a bug
 
64
        #num_faces = -10
 
65
 
 
66
        if num_faces > 0:
 
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)
 
71
 
 
72
            temp_file = "{}.new.png".format(self.screenshot_path)
 
73
            cv2.imwrite(temp_file, image)
 
74
            os.rename(temp_file, self.screenshot_path)
 
75
            timestamp = time()
 
76
 
 
77
        DataHandler().add_one_facedetect_entry(int(time()), num_faces)