• Things used in this project">Things used in this project
  • Story">Story
    • The principle of operation:">The principle of operation:
    • Preparation">Preparation
    • Face Recognition Script">Face Recognition Script
    • RFID Connection">RFID Connection
    • Button + Telegram">Button + Telegram
    • Adding timing to push-button">Adding timing to push-button
  • Schematics">Schematics

Real-Time system that allow to pass only authorized/invited people, using Face-Recognition or NFC cards.
IntermediateWork in progress2 hours17,108
Smart Intercom - Hackster.io - 图1

Things used in this project

Hardware components
Smart Intercom - Hackster.io - 图2 Raspberry Pi 3 Model B
—-

| × | 1 | | | | Smart Intercom - Hackster.io - 图3 | | Raspberry Pi 1 Model B+ | | —- | | |

| × | 1 | | | | | | RPI LCD 3.5 inch | | —- | | |

| × | 1 | | | | | | RFID reader/writer module MFRC522 | | —- | | |

| × | 1 | | | | | | Relay SRD-05VDC-SL-C | | —- | | |

| × | 1 | | | | Smart Intercom - Hackster.io - 图4 | | Raspberry Pi Camera Module | | —- | | |

| × | 1 | | | | Smart Intercom - Hackster.io - 图5 | | LED (generic) | | —- | | |

| × | 2 | | | | | | wifi usb dongle | | —- | | |

| × | 1 | | |

Story

work demonstration

Testing push-button

The principle of operation:

  • Person approaches to the intercom
  • Looks at the camera
  • The system compare his face with authorized people
  • If it finds it, then the door opens and a person can enter
  • There is also another way to pass;
  • The person with the pass card place it to the RFID location and the door opens if it finds the code of this card in its database

This project is still in progress. I hope the final version will be more beautiful.
*
Gonna get rid of RPi 1 and NFC in favor of fingerprint scanner connected to RPi3. Updates are coming…

Preparation

Relay connection

  • Connect relay to RPI 3. In my case I used GPIO12 pin for data, 5v for power and you can choose any GND.

You can test the relay with shell script, just create a simple sh script:

  1. nano open.sh
  2. chmod +x open.sh
  3. sh open.sh

Paste the code below and run it.
Script code:

  1. #!/bin/bash
  2. echo 12 > /sys/class/gpio/export
  3. echo out > /sys/class/gpio/gpio12/direction
  4. echo 0 > /sys/class/gpio/gpio12/value
  5. ping -c 1 localhost
  6. echo 12 > /sys/class/gpio/export
  7. echo out > /sys/class/gpio/gpio12/direction
  8. echo 1 > /sys/class/gpio/gpio12/value

Smart Intercom - Hackster.io - 图6
Camera connection

  • Connect camera module and enable it in raspi-config:

Open the raspi-config tool from the Terminal:

  1. sudo raspi-config

Select Enable camera and hit Enter , then go to Finish and you’ll be prompted to reboot.
To test that the system is installed and working, try the following command:

  1. raspistill -v -o test.jpg

LED Connection
Connect your LED to any GPIO you want + GND. In my case I used GPIO16 for the green LED and GPIO26 for red. When you are done, test it:
Create 2 simple python scripts for the green and red LEDs with the following content:

  • Green led.py

    1. from gpiozero import LED
    2. from time import sleep
    3. led = LED(16)
    4. while True:
    5. led.on()
    6. sleep(3)
    7. led.off()
    8. led.cleanup()
  • Red led2.py

    1. from gpiozero import LED
    2. from time import sleep
    3. led = LED(26)
    4. while True:
    5. led.on()
    6. sleep(3)
    7. led.off()
    8. led.cleanup()

    Smart Intercom - Hackster.io - 图7
    And then test it. If the LEDs are glowing, then everything works good.

    1. python led.py
    2. python led2.py

    Face Recognition Script

    Install this module from pypi using pip3 (or pip2 for Python 2):

    1. pip3 install face_recognition

    Create directory “pic” and “unknown” in Documents for example and place there some face pics of people you know. In my case it’s (“/home/pi/Documents/pic/“) and (“/home/pi/Documents/unknown/“).
    Create a python script with following code:

    1. import face_recognition
    2. import picamera
    3. import numpy as np
    4. import os
    5. camera = picamera.PiCamera()
    6. camera.resolution = (320, 240)
    7. output = np.empty((240, 320, 3), dtype=np.uint8)
    8. print("Loading known face image(s)")
    9. ep_image = face_recognition.load_image_file("/home/pi/Documents/pic/ep.jpg")
    10. ep_face_encoding = face_recognition.face_encodings(ep_image)[0]
    11. vl_image = face_recognition.load_image_file("/home/pi/Documents/pic/vl.jpg")
    12. vl_face_encoding = face_recognition.face_encodings(vl_image)[0]
    13. face_locations = []
    14. face_encodings = []
    15. while True:
    16. print("Capturing image.")
    17. camera.capture(output, format="rgb")
    18. face_locations = face_recognition.face_locations(output)
    19. print("Found {} faces in image.".format(len(face_locations)))
    20. face_encodings = face_recognition.face_encodings(output, face_locations)
    21. for face_encoding in face_encodings:
    22. match = face_recognition.compare_faces([ep_face_encoding,vl_face_encoding], face_encoding)
    23. name = "<Unknown Person>"
    24. os.system("python /home/pi/led2.py &")
    25. import time
    26. date_string = time.strftime("%Y-%m-%d-%H:%M:%S")
    27. camera.capture("/home/pi/Documents/unknown/image-" + date_string + ".jpg")

    Test it.

    1. python facerec.py

    RFID Connection

    Pins:
    Smart Intercom - Hackster.io - 图8
    We need this to connect our RFID module to Raspberry Pi 1.

  • Preparation:

    1. $ sudo nano /etc/modprobe.d/raspi-blacklist.conf
    2. #blacklist spi-bcm2708
    3. $ sudo apt-get install python-dev
    4. $ git clone https://github.com/lthiery/SPI-Py.git
    5. $ cd SPI-Py
    6. $ sudo python setup.py install
  • read.py: When the script finds authorized card, it opens the user picture on the remote RPI 3 (runs LED scripts), then it opens the door.

    1. import MFRC522
    2. import signal
    3. import os
    4. continue_reading = True
    5. MIFAREReader = MFRC522.MFRC522()
    6. cardA = [46,44,187,26,163]
    7. cardB = [176,203,130,124,133]
    8. cardC = [20,38,121,207,132]
    9. def end_read(signal, frame):
    10. global continue_reading
    11. continue_reading = False
    12. print "Ctrl+C captured, ending read."
    13. MIFAREReader.GPIO_CLEEN()
    14. signal.signal(signal.SIGINT, end_read)
    15. while continue_reading:
    16. (status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
    17. if status == MIFAREReader.MI_OK:
    18. print "Card detected"
    19. (status,backData) = MIFAREReader.MFRC522_Anticoll()
    20. if status == MIFAREReader.MI_OK:
    21. print "Card read UID: "+str(backData[0])+","+str(backData[1])+","+str(backData[2])+","+str(backData[3])+","+str(backData[4])
    22. if backData == cardA:
    23. print "Evghenii"
    24. os.system("sshpass -p *password* ssh root@10.0.0.60 fbi -T 1 -d /dev/fb1 -noverbose /home/pi/Documents/pic/ep.jpg")
    25. os.system("sshpass -p *password* ssh root@10.0.0.60 /home/pi/Documents/open.sh 2>/dev/null")
    26. # os.system("sshpass -p *password* ssh root@10.0.0.60 sleep 2")
    27. os.system("sshpass -p *password* ssh root@10.0.0.60 killall fbi")
    28. elif backData == cardB:
    29. print "Vlad"
    30. os.system("sshpass -p *password* ssh root@10.0.0.60 fbi -T 1 -d /dev/fb1 -noverbose /home/pi/Documents/pic/vl.jpg")
    31. os.system("sshpass -p *password* ssh root@10.0.0.60 /home/pi/Documents/open.sh 2>/dev/null")
    32. # os.system("sshpass -p *password* ssh root@10.0.0.60 sleep 2")
    33. os.system("sshpass -p *password* ssh root@10.0.0.60 killall fbi")
    34. elif backData == cardC:
    35. print "is Card C"
    36. else:
    37. print "wrong Card"
    38. os.system("sshpass -p *password* ssh root@10.0.0.60 /home/pi/led2.py 2>/dev/null")
  • Using this example:

    1. $ git clone https://github.com/rasplay/MFRC522-python.git
    2. $ cd MFRC522-python
    3. $ sudo python read.py

    Button + Telegram

    button.py

    1. #!/usr/bin/python
    2. import RPi.GPIO as GPIO
    3. from subprocess import call
    4. from datetime import datetime
    5. import time
    6. import os
    7. shutdownPin = 29
    8. shutdownMinSeconds = 5
    9. # button debounce time in seconds
    10. debounceSeconds = 0.01
    11. GPIO.setmode(GPIO.BOARD)
    12. GPIO.setup(shutdownPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    13. buttonPressedTime = None
    14. def buttonStateChanged(pin):
    15. global buttonPressedTime
    16. if not (GPIO.input(pin)):
    17. # button is down
    18. if buttonPressedTime is None:
    19. buttonPressedTime = datetime.now()
    20. else:
    21. # button is up
    22. if buttonPressedTime is not None:
    23. elapsed = (datetime.now() - buttonPressedTime).total_seconds()
    24. buttonPressedTime = None
    25. if elapsed >= shutdownMinSeconds:
    26. call(['shutdown', '-r', 'now'], shell=True)
    27. elif elapsed >= debounceSeconds:
    28. os.system("bash /home/pi/timed.sh")
    29. # subscribe to button presses
    30. GPIO.add_event_detect(shutdownPin, GPIO.BOTH, callback=buttonStateChanged)
    31. while True:
    32. # sleep to reduce unnecessary CPU usage
    33. time.sleep(5)

    action.sh

    1. #!/bin/bash
    2. sshpass -p password ssh epogonii@ipadress -p1337 notify-send -i /usr/share/icons/gnome/32x32/actions/ring2.png Smart-Intercom Guest_at_the_door
    3. DISPLAY=:0.0 XAUTHORITY=/home/pi/.Xauthority /usr/bin/feh --hide-pointer -x -q -D 5 -B black -F /home/pi/doorway.png &
    4. sh /home/pi/Documents/open.sh > log.out 2> /dev/null
    5. DISPLAY=:0.0 XAUTHORITY=/home/pi/.Xauthority xdotool key "x"
    6. sh /home/pi/tgphoto.sh FaceRec /var/www/html/last.jpg > log.out 2> /dev/null

    Smart Intercom - Hackster.io - 图9
    Added pushbutton to Smart Intercom. It sends you last photo to telegram bot and sends notification to Ubuntu Desktop PC.
    Smart Intercom - Hackster.io - 图10

    Adding timing to push-button

    In order to disable entrance in specific hours via push-button, i made the following bash script:

    1. #!/bin/bash
    2. H=$(date +%H)
    3. if (( 7 <= 10#$H && 10#$H < 19 )); then
    4. echo between 7AM and 19PM
    5. sh /home/pi/action.sh
    6. else
    7. echo error
    8. sh /home/pi/error.sh
    9. fi

    action.sh - opens the door between 7 am and 19 pm
    error.sh - displays restriction image between 19 pm and 7 am error.sh script

    1. #!/bin/bash
    2. DISPLAY=:0.0 XAUTHORITY=/home/pi/.Xauthority /usr/bin/feh --hide-pointer -x -q -D 5 -B black -F /home/pi/error.png &
    3. ping -c 4 localhost 2>/dev/null
    4. DISPLAY=:0.0 XAUTHORITY=/home/pi/.Xauthority xdotool key "x"

    P.S to run this script you will need to install xdotool
    sudo apt install xdotool

    Schematics

    Led connection

    Led connection

    Smart Intercom - Hackster.io - 图11

    relay connection

    relay connection

    Smart Intercom - Hackster.io - 图12

    Push-button connection

    Push-button connection

    Smart Intercom - Hackster.io - 图13