eCloudtureeCloudture
  • Training
    • Cloud Map
    • Cloud Training
    • Cloud Certification
  • Career
    • University Cloud Seeding Program
  • Resource
    • Blog
    • Examinations Center
    • Venue Rental
  • eCloudture
    • About eCloudture
    • Sharing Story
    • Contact
  • chinese
    • Training
      • Cloud Map
      • Cloud Training
      • Cloud Certification
    • Career
      • University Cloud Seeding Program
    • Resource
      • Blog
      • Examinations Center
      • Venue Rental
    • eCloudture
      • About eCloudture
      • Sharing Story
      • Contact
    • chinese
    • Home
    • All Post
    • AIoT Face Recognition with Raspberry Pi

    AIoT Face Recognition with Raspberry Pi

    • Posted by Shelly Yu
    • Categories All Post
    • Date 11/08/2019
    • Comments 0 comment

    Overview

    This workshop runs a chain of AIoT architecture: AI – IoT, which was inspired by the project https://github.com/fxgsell/GG-Edge-Inference.

    In this demo, a raspberry pi will be used as an IoT device to get local data by constructing a face recognition architecture. Part 1 will be an IoT device setup, then Part 2 will be face recognition setup.

    Architecture

    Step by step

    • In this part, we ‘ll use a raspberry pi as the IoT Device. In order to provide face image data, some packages such as Greengrass SDK, OpenCV and dlib need to be installed. Fortunately OpenCV is already installed in the below image, but dlib needs to be manually installed which may take lots of time due to the slow compile speed of raspberry pi.

    Setup OS for your raspberry pi

    1. Download Raspbian Image
    2. Flash the OS image to the SD card on raspberry pi
      • Etcher for mac/windows
    3. Wait about 5~10 minutes
    4. You have completed the OS installation for your raspberry pi.

    Raspberry Pi Wifi Setup

    1. Connect your raspberry pi to the monitor
    2. Insert the SD card onto the raspberry pi, and boot it up. > Default Password is turtlebot
    3. Connect to the wifi
    4. Get the IP address
    ifconfig
    
    1. Note the IP address down
    • If you don’t have a monitor, follow this paragraph

    Raspberry Pi Packages Installation

    1. Install packages (3 minutes)
    sudo pip install awscli boto3 imutils
    
    1. Install dlib (1 hour) Follow this paragraph

    2. Face_Recognition (5 minutes)

    sudo pip2 --no-cache-dir install face_recognition
    

    Setup AWS Credential On Your Raspberry Pi

    1. Find out your AWS Credential (Access key)

    1. Setup you AWS Credential in terminal
    aws configure
    
    • AWS Access Key ID [None]: Type Your Access Key ID
    • AWS Secret Access Key [None]: Type Your Secret Access Key
    • Default region name [None]: us-east-1
    • Default output format [None]: json

    IoT Device Greengrass Installation

    1. You are now ready to set up Raspberry Pi for AWS IoT Greengrass. First, run the following commands in raspberry pi terminal:
    #### Run these commands in your raspberry pi####
    sudo adduser --system ggc_user
    sudo addgroup --system ggc_group
    sudo apt-get install rpi-update
    sudo rpi-update b81a11258fc911170b40a0b09bbd63c84bc5ad59
    
    1. Reboot
    sudo reboot
    
    1. Check the kernel version
    ### connect to pi###
    ssh pi@ip_address
    uname -a
    

    It should be like this:

    1. To improve security on the Pi device, enable hard link and soft link protection on the operating system at startup.
    cd /etc/sysctl.d
    ls
    sudo nano 98-rpi.conf
    
    1. Add following two lines to 98-rpi.conf
    fs.protected_hardlinks = 1
    fs.protected_symlinks = 1
    

    1. Press Ctrl+x , and then Y,Enter to save & exit.
    2. Reboot Again
    sudo reboot
    
    1. Confirm the Check
    ### connect to pi###
    ssh pi@ip_address
    sudo sysctl -a 2> /dev/null | grep fs.protected
    

    1. Edit your command line boot file to enable and mount memory groups. This allows AWS IoT Greengrass to set the memory limit for Lambda functions. Without this, the Greengrass daemon is unable to run.
    cd /boot/
    cgroup_enable=memory cgroup_memory=1
    
    ### Final Reboot ###
    sudo reboot
    
    1. Your Raspberry Pi should now be ready for AWS IoT Greengrass. Next, install the green grass SDK.
    cd ~
    wget https://d1onfpft10uf5o.cloudfront.net/greengrass-core/downloads/1.7.0/greengrass-linux-armv7l-1.7.0.tar.gz
    sudo tar -xzvf greengrass-linux-armv7l-1.7.0.tar.gz -C / 
    

    Take a break

    Now, basic installation on the raspberry pi has been completed, and we’ll move on to the next part.

    Step by step

    In this part, a face recognition architecture will be built.
    This section will guide the user through: – Setup AWS S3, Lambda manually. – Setup Greengrass group by a script. – Setup Greengrass Lambda for face detection by a script. – Setup another Lambda for face recognition architecture by a script.

    Setup your Amazon S3 bucket

    1. Select S3 service from the AWS console.
    2. Create a new S3 bucket, entering a UNIQUE name.

    Setup your AWS Lambda Function

    1. Create a new AWS Lambda

      • Choose Autor from scratch

      • Make a function name (For example: IoT-Face-Detection-Demo)
      • Choose Python 2.7 as the runtime
      • Choose Choosing an existing role
      • Choose lambda_basic_execution

      • If you cannot find lambda_basic_execution
        • choose Create a custom role from the role drop-down
        • Create a new role lambda_basic_execution

    2. After creating your new Lambda function, we need to publish a new version then assign an alias pointing to that version

      • First, select your new Lambda function and click the Action button, and select Publish new version from the drop-down menu as shown:

      • Next, click Publish

      • Now, click the Action button again, and select Create Alias from the drop-down menu as shown:

      • Next, fill in the Name field with the word latest and select the latest Version which would be 1 as shown here, then click the Create button

      • If successful, you should see the following:

    Setup AWS Credential On Your Computer/Laptop

    1. This time, set up your credential on your computer/laptop in order to run the following steps. Actually, you can run all scripts on your raspberry pi, but sometimes it’s laggy, so it depends on you.

    1. Setup you AWS Credential in terminal (Same as Lab1 did) bash
      aws configure

    Create Greengrass Group

    • Now we are going to create a Greengrass group by running a script.
    1. Get the project from AWS-IoT-Face-Recognition-Analysis bash
      git clone https://gitlab.com/ecloudture/aws-iot-face-recognition-analysis.git
      cd AWS-IoT-Face-Recognition-Analysis/script/1-greengrass-configuration/
    2. Within this directory, we will use the create-greengrass-config.py script to generate the necessary groups for publishing our Greengrass function:

      python3 create-greengrass-config.py --create-group NAME_YOU_WANT --bucket NAME_U_CREATED --function NAME_U_CREATED
      

      The above command is executed with the following parameters:

      • –create-group \: the group_name that will appear in your AWS IoT console referring to this device
      • –bucket \: the bucket_name of the Amazon S3 bucket you created earlier
      • –function : the function_name of the AWS Lambda function you created earlier
    3. If successful, you should see similar output to the following:

    Install Certificate Into Your Pi

    • The certificate is the key to the connection between your IoT Device and AWS IoT. So you need to install the certificate into your raspberry pi. You can use either scp or Filezilla to transport the file.
    1. Installation through scp
    scp certificates.tar.gz pi@ip_address:~
    
    1. SDK Installation
    ssh pi@ip_address
    ls
    sudo tar -xzvf certificates.tar.gz -C /greengrass
    
    1. Get the root.ca.pem. Here we use VeriSign Endpoints instead of Amazon Trust Services Endpoints.
    cd /greengrass/certs/
    sudo wget -O root.ca.pem https://www.symantec.com/content/en/us/enterprise/verisign/roots/VeriSign-Class%203-Public-Primary-Certification-Authority-G5.pem
    cat root.ca.pem  #check the content
    
    1. Start the Greengrass daemon
    cd /greengrass/ggc/core
    sudo ./greengrassd start
    exit #back to your computer/laptop
    

    Deploy the Greengrass group to the Device

    1. Go to AWS IoT Console , Click Greengrass ,then click Groups
    2. Click the Actions button, and select Deploy as shown below:

    1. Next, click Automatic detection.

    1. Next, we will choose to grant permissions to Greengrass by clicking Grant permission (shown below):

    1. We will need to wait a bit for the deployment to be successful.

    1. Next, we go to Resources on the left panel.
      In order to get access to the camera, a new resource needs to be added. Click Add local resources

    1. For resource name, enter Camera.
      Choose Device for Resource Type.
      In Device Path, enter /dev/video0.

    1. In Group owner file access permission select Automatically add OS group permission of the Linux group that owns the resources Then click select in Lambda function affiliations

    1. Choose the lambda you created earlier , For example: IoT-Face-Detection-Demo.
      Then select Read And Write Access, then click Done.

    1. Make sure you have linked the right lambda. Then click save.
    2. Click Action again. Then click deploy
    3. Wait until it is successfully connected.
    • Once success, a lambda would be deployed on your IoT device.
    • If you did not a success, try to restart Greengrass daemon and redeploy Greengrass.
    ##connect to your pi##
    ssh pi@ip_address
    cd /greengrass/ggc/core
    sudo ./greengrassd restart
    

    AWS IoT Connection

    1. From the AWS console, select IoT Core. We should see the AWS IoT Monitoring console as shown below, with a dot on the graph indicating a successful connection to our device.

    Setup Up Face Detection

    • Here we are going to deploy the first lambda function working with face detection. Once the lambda detect faces, it would upload the snapshot to your S3 bucket. You can monitor the log of your lambda in cloudwatch log.

    Setup properties

    cp ../properties{.sample,}.mk
    nano ../properties.mk
    

    Run the script

    • This script will automatically upload python files to the lambda you created before. Once completed, the lambda would be deployed on your raspberry pi through IoT Greengrass.
    cd ~/GG-Edge-Inference
    cd 2-face-detection
    make
    
    • If you are on the right track, then your camera on the device should be triggered on very soon like this:

    Debug Tool: Cloudwatch log

    If you encounter some problems: 1. Go to AWS console, then choose cloudwatch 2. choose log on the left panel

    1. find the lambda function you created earlier
    2. get your bugs, usually are packaged issue, ex. cannot find module: cv2
    3. If your lambda runs properly, the log should be like this.

    Setup Face recognition

    • Here we are going to run a script for creating another lambda function. This time the lambda function manages face recognition part and connect to DynamoDB & Rekognition.
    cd ~/GG-Edge-Inference
    cd 3-hybrid-face-recognition
    make
    
    • This will trigger cloudFormation to start.
      Go to cloudFormation in the aws console, then find Hybrid-Face-Recognition. Wait until its success, it should be like this:

    See the result within the device

    • In order to view the video from the device:
      • You need a monitor connected to raspberry pi and run below command cat /tmp/results.mjpeg | mplayer - -demuxer lavf -lavfdopts format=mjpeg:probesize=32
      • If /tmp/results.mjpeg does not exist, it means your local lambda did not work successfully at all. Find the bug in the cloudwatch log, or try to restart your raspberry pi & Greengrass daemon.

    Check the result on AWS IoT Core

    1. In the IoT console, got to the Test section.
    2. Subscribe to the topic face_recognition/inference, it will show you who the device sees. There are more topics available:

      • face_recognition/inference: the faces it sees
      • face_recognition/admin: some info messages or errors if there is.
      • face_recognition/new: where the device posts face it doesn’t know (Base64 encoded jpeg).
      • face_recognition/match/"Thing_Name": used to update the name of a face in a device.
    3. To rename a user: post a message to the face_recognition/match/Thing_Name with content {"OLD_NAME": "NEW_NAME"}

    Conclusion

    Congratulations! now you have learned: – Learn basic Linux skill to operate raspberry pi – Create IoT connection with Raspberry Pi – Construct a face recognition architecture by using AWS cloud services

    Gitlab Project Link

    https://gitlab.com/ecloudture/aws-iot-face-recognition-analysis

    • Share:
    Shelly Yu

    Previous post

    Amazon Personalize
    11/08/2019

    Next post

    Use-EMR-to-Build-Machine-Learning-Model
    11/08/2019

    You may also like

    reInvent_facebook_post_werner_Eng
    AWS re:Invent 2021 – Werner Vogels Keynote
    6 December, 2021
    reInvent-Adam-Eng_工作區域 1
    AWS re:Invent 2021 – Adam Selipsky Keynote
    1 December, 2021
    shutterstock_134102588
    AutoScaling – Health check v.s. Status Check
    25 November, 2021

    Leave A Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    搜尋文章

    Categories

    • AI/ML
    • All Post
    • AWS re:Invent
    • Big Data
    • Container
    • Foundation
    • News
    • Security
    • Serverless
    Phone : +886 2 22801777
    Mail : info@ecloudture.com

    Training

    • Cloud Training
    • Cloud Map
    • Cloud Certification

    Talent

    • University Cloud Seeding Program

    Resource

    • Blog
    • Examinations Center
    • Venue Rental

    eCloudture

    • About eCloudture
    • Sharing Story
    • Contact

    • Privacy
    • Terms
    • Sitemap