Facial recognition

Created
Updated
Author Nicolas Dorriere Reading 1 min

1. Project Structure

/opt/reco/
├── faces/      # Known faces (format: firstname-lastname_xx.jpg)
├── captures/   # Unknown face captures
├── lcd/        # Media to display (images/videos)
└── cronfacial.sh

2. Package Installation

sudo apt update && sudo apt upgrade -y
sudo apt install fswebcam libopenblas-dev liblapack-dev python3-picamera

3. Hardware Configuration

Camera Activation:

sudo raspi-config
# Interfacing Options > Camera > Yes
vcgencmd get_camera  # Should return "supported=1 detected=1"

4. dlib Compilation (25min)

cd /opt
sudo git clone -b 'v19.6' https://github.com/davisking/dlib.git
cd dlib
sudo python3 setup.py install --compiler-flags "-mfpu=neon"

5. face_recognition API Setup

pip3 install face_recognition
cd /opt/ageitgey/face_recognition/examples
python3 facerec_on_raspberry_pi.py

6. Performance Optimization

# Environment variables
export OPENBLAS_NUM_THREADS=1
export OPENBLAS_MAIN_FREE=1

# Disable swap
sudo dphys-swapfile swapoff
sudo dphys-swapfile uninstall

7. System Service Management

# Service file (/lib/systemd/system/recofacial.service)
[Unit]
Description=Facial Recognition Service
After=network.target

[Service]
Type=forking
ExecStart=/opt/reco/cronfacial.sh
Restart=always
RuntimeMaxSec=43200  # 12h auto-restart

[Install]
WantedBy=multi-user.target

8. Advanced Troubleshooting

Memory Allocation Errors:

# Check kernel logs
tail -f /var/log/kern.log | grep "cma_alloc"

# Filesystem repair
fsck /dev/mmcblk0p1
sudo reboot

9. GPIO Relay Control

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
GPIO.output(17, GPIO.LOW)  # Activate relay
time.sleep(4)
GPIO.cleanup()

10. LCD Display Setup

# Framebuffer mirroring
fbcp &  # HDMI to LCD

# Image display
fbi -T 1 -d /dev/fb1 -noverbose -t 5 /opt/reco/lcd/welcome.jpg

# Video playback
omxplayer --orientation 270 /opt/reco/lcd/intro.mp4

11. Maintenance Scripts

# Daily reboot script
0 0 * * * /opt/reco/reboot.sh >/dev/null 2>&1

# Memory monitoring
THRESHOLD=90
MEM_USAGE=$(free | awk '/Mem/{printf("%d"), $3/$2*100}')
[ $MEM_USAGE -gt $THRESHOLD ] && systemctl restart recofacial

12. Technical References