Example of Alibaba cloud IOT platform IoT Studio calling face recognition service

Posted by camarosource on Mon, 30 Mar 2020 16:35:07 +0200


Current IoT The studio service development module provides the calling module of face recognition API in the cloud market, but there is no direct way to call the Alibaba cloud face recognition API. In this paper, the image is uploaded to the oss service from the device side, and then the image URL is passed through the uplink message, and the Alibaba cloud face recognition service is called using the NodeJS node, and the results are stored through the MySQL cloud database.

Step By Step

1, Device end development




2,Device Code Sample

import com.alibaba.taro.AliyunIoTSignUtil;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectRequest;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class FaceDemoUpload {

    public static String productKey = "a1o9N******";
    public static String deviceName = "d******";
    public static String deviceSecret = "7Ukl6Ka80nIEXhUwCmw7XrUC********";
    public static String regionId = "cn-shanghai";

    // Object model - attribute reporting topic
    private static String pubTopic = "/sys/" + productKey + "/" + deviceName + "/thing/event/property/post";
    // User defined topic, defined in the product topic list location
    private static String subTopic = "/sys/" + productKey + "/" + deviceName + "/thing/event/property/post_reply";

    private static MqttClient mqttClient;

    public static void main(String[] args) {

        ScheduledExecutorService scheduledThreadPool = new ScheduledThreadPoolExecutor(1,
                new ThreadFactoryBuilder().setNameFormat("thread-runner-%d").build());

        scheduledThreadPool.scheduleAtFixedRate(() -> postDeviceProperties(), 10, 5, TimeUnit.SECONDS);

        try {
            mqttClient.subscribe(subTopic); // Subscribe to Topic
        } catch (MqttException e) {
            System.out.println("error:" + e.getMessage());

        // Set subscription listening
        mqttClient.setCallback(new MqttCallback() {
            public void connectionLost(Throwable throwable) {
                System.out.println("connection Lost");


            public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
                System.out.println("Sub message");
                System.out.println("Topic : " + s);
                System.out.println(new String(mqttMessage.getPayload())); //Printout message payLoad

            public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {



     * Initialize Client object
    private static void initAliyunIoTClient() {

        try {
            // Parameters required to construct a connection
            String clientId = "java" + System.currentTimeMillis();
            Map<String, String> params = new HashMap<>(16);
            params.put("productKey", productKey);
            params.put("deviceName", deviceName);
            params.put("clientId", clientId);
            String timestamp = String.valueOf(System.currentTimeMillis());
            params.put("timestamp", timestamp);
            // cn-shanghai
            String targetServer = "tcp://" + productKey + ".iot-as-mqtt." + regionId + ".aliyuncs.com:1883";

            String mqttclientId = clientId + "|securemode=3,signmethod=hmacsha1,timestamp=" + timestamp + "|";
            String mqttUsername = deviceName + "&" + productKey;
            String mqttPassword = AliyunIoTSignUtil.sign(params, deviceSecret, "hmacsha1");

            connectMqtt(targetServer, mqttclientId, mqttUsername, mqttPassword);

        } catch (Exception e) {
            System.out.println("initAliyunIoTClient error " + e.getMessage());

    public static void connectMqtt(String url, String clientId, String mqttUsername, String mqttPassword) throws Exception {

        MemoryPersistence persistence = new MemoryPersistence();
        mqttClient = new MqttClient(url, clientId, persistence);
        MqttConnectOptions connOpts = new MqttConnectOptions();
        // MQTT 3.1.1
//        connOpts.setCleanSession(true);



     * Reporting attributes
    private static void postDeviceProperties() {

        try {
            //Reporting data
            //Advanced version model - attribute reporting payload
            System.out.println("Escalation attribute value");

            //Upload local pictures
            // Request body  C:\Users\Administrator\Desktop\timg.jpg
            String pic_path = "C:\\Users\\Administrator\\Desktop\\timg.jpg";//Path to local picture

            String picURL = uploadPicToOss(pic_path);

            String payloadJson = "{\"params\":{\"textdata\":\"" + picURL + "\"}}";
            MqttMessage message = new MqttMessage(payloadJson.getBytes("utf-8"));
            mqttClient.publish(pubTopic, message);
        } catch (Exception e) {

    public static String uploadPicToOss(String filePath) {
        // Endpoint takes Shanghai as an example. Please fill in other regions according to the actual situation.
        String endpoint = "http://oss-cn-shanghai.aliyuncs.com";
        String bucketName = "taro******";  // bucket name
        String key = "pic/timg.jpg";   // File path and name
        // Alicloud primary account AccessKey has access to all APIs, which is very risky. It is strongly recommended that you create and use RAM account for API access or daily operation and maintenance. Please log in https://ram.console.aliyun.com to create RAM account.
        String accessKeyId = "LTAIOZZg********";
        String accessKeySecret = "v7CjUJCMk7j9aKduMA************";

        // Create an OSSClient instance.
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        // Create a PutObjectRequest object.
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, new File(filePath));

        // If you need to set the storage type and access rights when uploading, please refer to the following example code.
        ObjectMetadata metadata = new ObjectMetadata();

        // metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());

        // Upload the file.

        // Close OSSClient.

        return "https://"+ bucketname +". Oss-cn-shanghai.aliyuncs.com / "+ key; / / returns the URL that the image can be directly accessed by the public network in OSS

Reference link: Connect alicloud IoT based on open source JAVA MQTT Client

2, Service development module building

1. Device trigger

2. Node.js script

* @param {Object} payload Output from previous node
* @param {Object} node Specify the output of a node
* @param {Object} query Output of the first node of the service flow
* @param {Object} context  { appKey, appSecret }

module.exports = async function (payload, node, query, context) {

  const Core = require('@alicloud/pop-core');

  var client = new Core({
    accessKeyId: 'LTAIOZZg********',
    accessKeySecret: 'v7CjUJCMk7j9aKduMA************',
    endpoint: 'https://face.cn-shanghai.aliyuncs.com',
    apiVersion: '2018-12-03'

  var params = {
    "ImageUrl": query.props.textdata.value

  var requestOption = {
    method: 'POST'

    var result = await client.request('GetFaceAttribute', params, requestOption);
    return result;

Reference link: Alibaba cloud new version of face recognition NODEJS use example tutorial

3, Cloud database MySQL

Database table creation statement:

/*------- CREATE SQL---------*/
CREATE TABLE `iotface1` (
  `face_num` INT DEFAULT NULL,
  `gender` INT DEFAULT NULL,
  `expression` INT DEFAULT NULL

Reference link: IoT Studio service development MySQL data flow example Demo

Deployment and operation

4, Run tests

1. Device side log

2. Node log

3. Database input insert query

Reference link

IoT Studio service development MySQL data flow example Demo
Alibaba cloud Internet of things platform blog overview
Alibaba cloud new face recognition overview

Topics: Java MySQL Database Eclipse