MeType

Tag: python

Magic Mirror – Face Recognition and general Update

After being absent from this blog for quite some time, I’d like to give you a quick update on my mirror project. The filesystem of my Raspberry Pi’s SD-card got corrupted some time ago and I haven’t found the motivation to reinstall the system and all my scripts since then, well at least not until two days ago. Because I had to setup the system from scratch again anyway I used the opportunity to use the newest Raspbian version “Jessie” this time and also to match my modified mirror interface code with the original from Michael Teeuw again. Some thoughts about the new Linux version: I really like the way systemd handles process managing and starting, seems a lot simpler to me than it was before but that’s not the topic today.

A few months ago I took the code from the Raspberry Pi Face Recognition Treasure Box and modified it in a way so that it’s no longer just able to recognize one valid face but to differentiate between multiple faces and therefore to know who’s standing in front of the mirror. The python script  is now constantly trying to match people to a known set of trained faces and if one is recognized displays a welcome message and personalized information for that user.

Because the number of my components and running processes keeps increasing, I sketched a quick schematic yesterday to not loose sight of who is talking to whom.
Software:Hardware magischer Spiegel

On the bottom of the software side, we have the mirrorcontroller. It is responsible for all kind of GPIO-stuff, like monitoring the motion sensor and a button. In the future it will also controls the colors of some RGB LED’s I will use for lighting up the person in front of the mirror but that’s still to be implemented. If something moves, a relay turns on the display and a signal is send to the face recognition service, which then starts to take pictures and searches them for faces. If the service recognizes a face the data is send to the SocketIOServer, which further sends it to the web interface to display the welcome message and the information for the user. Other features of the SocketIOServer are:

  • Online Banking, to show the users current bank balance
  • Collecting statistics from my Vimeo-videos
  • Investigating how much space is still left on my server
  • Detecting incoming calls via the Fritz!Box (a German internet router and phone system) call monitor
  • Pulling invoices my clients still need to pay from Invoice Ninja

In a future update I will also add gathering my ToDo’s from Wunderlist but until now that’s it.

Node.js – Python Bridge

The method I used to transfer information from my Python script over to my Node.js script by using zerorpc kind of worked but every time the scripts were running for some time they suddenly stopped transferring my data for no apparent reason. Since I wasn’t really a fan of the way I setup the connection in the first place, I didn’t bother finding a solution for my problem and directly started looking for a different way altogether and thanks to John Hobbs I found one. Searching for examples on how to use UNIX sockets I found this article  and after I left a comment on his blog, John Hobbs quickly added a socket server written for Node.js, which could then successfully communicate with my Python script.

import RPi.GPIO as GPIO
import os, os.path
import socket

GPIO.setup(27, GPIO.IN)

def node_bridge(command):
    if os.path.exists( "/tmp/python_node_bridge" ):
        client = socket.socket( socket.AF_UNIX, socket.SOCK_STREAM )
        client.connect( "/tmp/python_node_bridge" )
        client.send( command )
        client.close()
    else:
        print "Couldn't Connect!"

def switchview(27):
    node_bridge("switchview")

try:
    GPIO.add_event_detect(27, GPIO.FALLING, callback=switchview, bouncetime=600)
    while 1:
        time.sleep(100)
var io = require('socket.io').listen(1234);
var net = require('net');

var server = net.createServer(function(python_bridge) {
  python_bridge.on('data', function(data) {
    if(data.toString() == "switchview") {
      io.sockets.emit('switchview');
    }
  });
});
server.listen("/tmp/python_node_bridge");
var socket = io.connect('http://localhost:1234');
view = "view1";

function switchview() {
  if (view == "view1") {
    $('#view1').fadeOut(700);
    $('#view2').fadeIn(700);
    view = "view2"
  } else if (view == "view2") {
    $('#view2').fadeOut(700);
    $('#view1').fadeIn(700);
   view = "view1"
  }
};

socket.on('switchview', function () {
  switchview();
});

These three pieces of code switch between different views for displaying different kind of information on my mirror UI after pressing a Button.

The Python script monitors the GPIO-Pin and waits for the 3.3V to drop to 0V, it then sends a message to my Node.js script, which sends a message to the web interface where the different views are then triggered by a JavaScript function.

Python Uncertainty

Maybe one of my readers is a bit more experienced when it comes to Python – probably not that hard – and can explain to me why my first script does not work and the second one on the other hand does. Biteule and I only came as far as to suspect, that it has something to do with the threading of the GPIO-callbacks but what exactly the problem is neither of us understands. It’s not like zerorpc is not imported and available in the first script when it gets executed, after all it is able to throw an error (LostRemote: Lost remote after 10s heartbeat), so why does it make a difference where I import the module?

import RPi.GPIO as GPIO
import time
import zerorpc

nodebridge = zerorpc.Client()
nodebridge.connect("tcp://127.0.0.1:4242")

GPIO.setmode(GPIO.BCM)
GPIO.setup(27, GPIO.IN)

def switchview(switchview_taster):
    nodebridge.switchview()

try:
    GPIO.add_event_detect(27, GPIO.FALLING, callback=switchview, bouncetime=600)
    while 1:
        time.sleep(100)
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(27, GPIO.IN)

def switchview(switchview_taster):
    import zerorpc
    nodebridge = zerorpc.Client()
    nodebridge.connect("tcp://127.0.0.1:4242")
    nodebridge.switchview()

try:
   GPIO.add_event_detect(27, GPIO.FALLING, callback=switchview, bouncetime=600)
   while 1:
      time.sleep(100)
var io = require('socket.io').listen(1234);
var zerorpc = require("zerorpc");

var pythonbridge = new zerorpc.Server({
  switchview: function(reply) {
    io.sockets.emit('switchview');
    reply(null);
  }
});

pythonbridge.on("error", function(error) {
  console.error("RPC server error:", error);
});

pythonbridge.bind("tcp://0.0.0.0:4242");

UPDATE 12.02.15 – 14:51:

Copyright © 2018 MeType

Theme by Anders NorenUp ↑