Sitting posture alarm

In this tutorial we will build a simple sitting posture alarm.

Hardware list:

  • HS device
  • Raspberry Pi 3b
  • Buzzer

Workflow

2-1

Since currently the HS model zoo does not include pose estimation model, we simplify the problem and use our Scene Recorder to achieve the goal.

Here we consider two senarios that need to be distinguished:

  • Normal sitting posture
  • Unhealthy sitting posture

as shown below:

2-2

Preparation

Firstly, for convenience we record the senarios by running the Scene recorder Python script on a PC host and save the result to a local file.

Then we copy the save file to a Raspberry Pi.

On PC host:

3-1

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


if __name__ == '__main__':
    cv2.namedWindow("Scene Recorder", cv2.WINDOW_NORMAL)
    try:
        net = hs.SceneRecorder(zoom=True, verbose=2)
        while True:
            result = net.run()
            key = cv2.waitKey(5)
            prob = net.record(result, key, saveFilename="./record.dat")
            cv2.imshow("Scene Recorder", result[0])
            cv2.waitKey(1)
    finally:
        cv2.destroyAllWindows()

We have to repeatly train and test the recorder (by press 1 for recording correct sitting posture and 2 for incorrect posture, then r to refresh the classifier).

Implementation

4-1

Controlling script for buzzer:

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

import RPi.GPIO as GPIO

PIN = 7


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


def warning(on=True):
    GPIO.output(PIN, not on)


def clean():
    GPIO.cleanup()

Processing script:

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

from SungemSDK.api import hsapi as hs
import buzzer

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

    try:
        net = hs.SceneRecorder(zoom=True, verbose=2)

        # Auto load recorded senarios
        result = net.run()
        net.record(result, ord("l"), saveFilename="./record.dat")

        while True:
            result = net.run()
            prob = net.record(result, -1, saveFilename="./record.dat")

            counter = (counter + 1) if prob[1] > 0.5 else 0
            buzzer.warning(counter > 10)  # Trigger alarm when `bad posture` appears for 10 frames
    finally:
        buzzer.clean()