Smart Lamp

Here we give a tutorial on how to control a lamp through face detector with HS device. The lamb automatically turns on if face appears, otherwise turn off.

Hardware list:

  • HS device
  • Raspberry Pi 3b
  • 1 channel relay module
  • 5V USB lamp

Workflow

1-1

The built-in model Mobilenet-SSD face detector is used in this DIY demo. The logic is illustrated as below:

2-2

then we control the switch of the lamp through a 1 channel relay module. Connect GPIO pin on Raspberry Pi to IN on relay module, GND pin to DC- pin to complete the circult. The lamb will be controlled with the GPIO electrical level triggered by the signal.

Relay module

Implementation

Prepare the Raspberry Pi and install SungemSDK, connect HS device and the relay module. 1

Python script as relay module controllor:

#!/usr/bin/env python3
# coding=utf-8

# lamp.py

import RPi.GPIO as GPIO

PIN = 7

def setup():
    GPIO.setwarnings(False)
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(PIN, GPIO.OUT)

def power(on=True):
    # True high-electrical-level -> turn off; False low-electrical-level -> turn on
    GPIO.output(PIN, on) 

def clean():
    GPIO.cleanup()

Service logic code:

#!/usr/bin/env python3
# coding=utf-8

import sys
sys.path.append("../../SungemSDK-Python")
import hsapi as hs
import lamp


def process(ret):
    img = ret[0]
    face = []
    for box in ret[1]:
        if (box[4] - box[2] > img.shape[1] * 0.8) \
                and (box[5] - box[3] > img.shape[0] * 0.8):
            continue
        face.append(box)
    return face


if __name__ == '__main__':
    lamp.setup()
    counter = 0

    try:
        net = hs.FaceDetector(zoom=True, verbose=0, threshSSD=0.55)
        while True:
            result = net.run()
            faces = process(result)

            counter += 1 if len(faces) > 0 else -1
            if counter > 10:  # detect faces in 10 frames in sequence
                lamp.power(True)  # turn on the light
                counter = 10
            elif counter < 0:
                lamp.power(False)  # turn off
                counter = 0
    finally:
        lamp.clean()

g1

g2

g3