百度API识别小猫小狗:nodered+baiduAPI

前言

软硬件支持

  • nodered;
  • 需要用到的节点:
  1. node-red-contrib-wfwatch
  2. node-red-contrib-image-output
  3. node-red-contrib-http-request
  4. node-red-contrib-bizwechat

实现步骤:

  1. 创建应用




例:https://aip.baidubce.com/rest/2.0/image-classify/v1/animal?access_token=获取到的access_token

  1. 图片的获取
    自行查看摄像头抓图地址
    由于我的摄像头是EASYN,暂时没有找到抓图地址,并且使用群辉的surveillance station抓图也暂时没有成功,所以我使用的方法是靠Easyn摄像头自动侦测移动拍照,照片通过保存到指定的FTP服务器文件夹(FTP服务器我使用的是群辉,下次补充过程),使用wfwatch节点监控FTP文件夹是否有新图片产生,对新生成的图片进行识别并推送结果。
    image
    image


    高级推送参考贴或者论坛搜索相关帖子
    百度api识别动物.json (16.6 KB)
    [
        {
            "id": "211f2157.c10eae",
            "type": "tab",
            "label": "流程1",
            "disabled": false,
            "info": ""
        },
        {
            "id": "6eb2e72a.e5c7a8",
            "type": "function",
            "z": "211f2157.c10eae",
            "name": "获取access_token",
            "func": "msg.headers = {\n   \"Content-Type\" : \"application/x-www-form-urlencoded\"\n}\nmsg.payload = {\n   \"grant_type\" : \"client_credentials\",\n   \"client_id\" : \"你的API Key\",\n   \"client_secret\" : \"你的Secret Key\"\n}\nreturn msg;",
            "outputs": 1,
            "noerr": 0,
            "x": 390,
            "y": 100,
            "wires": [
                [
                    "91a3a8e.669c358"
                ]
            ]
        },
        {
            "id": "91a3a8e.669c358",
            "type": "http request",
            "z": "211f2157.c10eae",
            "name": "获取access_tkoen",
            "method": "POST",
            "ret": "obj",
            "paytoqs": false,
            "url": "https://aip.baidubce.com/oauth/2.0/token",
            "tls": "",
            "persist": false,
            "proxy": "",
            "authType": "",
            "x": 610,
            "y": 100,
            "wires": [
                [
                    "ec5a1317.f9a0b"
                ]
            ]
        },
        {
            "id": "ec5a1317.f9a0b",
            "type": "function",
            "z": "211f2157.c10eae",
            "name": "保存access_token",
            "func": "flow.set('access_token', msg.payload.access_token); //将access_token储存\nreturn msg;",
            "outputs": 1,
            "noerr": 0,
            "x": 810,
            "y": 100,
            "wires": [
                [
                    "4ffabf56.25cde"
                ]
            ]
        },
        {
            "id": "4ffabf56.25cde",
            "type": "debug",
            "z": "211f2157.c10eae",
            "name": "",
            "active": true,
            "tosidebar": true,
            "console": false,
            "tostatus": false,
            "complete": "true",
            "targetType": "full",
            "x": 990,
            "y": 100,
            "wires": []
        },
        {
            "id": "2d61931e.557b9c",
            "type": "file in",
            "z": "211f2157.c10eae",
            "name": "",
            "filename": "",
            "format": "",
            "chunk": false,
            "sendError": false,
            "encoding": "binary",
            "x": 570,
            "y": 260,
            "wires": [
                [
                    "e7b5e77d.3d4798",
                    "71392818.cfaf28"
                ]
            ]
        },
        {
            "id": "7446262e.463018",
            "type": "delay",
            "z": "211f2157.c10eae",
            "name": "",
            "pauseType": "delay",
            "timeout": "1",
            "timeoutUnits": "seconds",
            "rate": "1",
            "nbRateUnits": "1",
            "rateUnits": "second",
            "randomFirst": "1",
            "randomLast": "5",
            "randomUnits": "seconds",
            "drop": false,
            "x": 280,
            "y": 260,
            "wires": [
                [
                    "2fde816d.43eace"
                ]
            ]
        },
        {
            "id": "e7b5e77d.3d4798",
            "type": "base64",
            "z": "211f2157.c10eae",
            "name": "",
            "action": "",
            "property": "payload",
            "x": 740,
            "y": 260,
            "wires": [
                [
                    "4aa015b3.5c9a2c"
                ]
            ]
        },
        {
            "id": "4aa015b3.5c9a2c",
            "type": "function",
            "z": "211f2157.c10eae",
            "name": "动物识别",
            "func": "msg.headers = {\n    \"Content-Type\" : \"application/x-www-form-urlencoded\"\n}\nmsg.url = `https://aip.baidubce.com/rest/2.0/image-classify/v1/animal?access_token=${flow.get('access_token')}`;\nmsg.payload = {\n    image:msg.payload, //图片Base64编码\n    baike_num:5 //返回百科信息的结果数量\n}\n\nreturn msg;\n",
            "outputs": 1,
            "noerr": 0,
            "x": 920,
            "y": 260,
            "wires": [
                [
                    "d216d0fe.34d28"
                ]
            ]
        },
        {
            "id": "d216d0fe.34d28",
            "type": "http request",
            "z": "211f2157.c10eae",
            "name": "识别请求",
            "method": "POST",
            "ret": "obj",
            "paytoqs": false,
            "url": "",
            "tls": "",
            "persist": false,
            "proxy": "",
            "authType": "",
            "x": 1080,
            "y": 260,
            "wires": [
                [
                    "af0fb26f.c366e",
                    "fb7ba407.33bbb8"
                ]
            ]
        },
        {
            "id": "af0fb26f.c366e",
            "type": "debug",
            "z": "211f2157.c10eae",
            "name": "",
            "active": true,
            "tosidebar": true,
            "console": false,
            "tostatus": false,
            "complete": "payload",
            "targetType": "msg",
            "x": 1270,
            "y": 200,
            "wires": []
        },
        {
            "id": "71392818.cfaf28",
            "type": "image",
            "z": "211f2157.c10eae",
            "name": "",
            "width": "1080",
            "data": "payload",
            "dataType": "msg",
            "thumbnail": false,
            "x": 760,
            "y": 200,
            "wires": []
        },
        {
            "id": "5659c311.8812fc",
            "type": "debug",
            "z": "211f2157.c10eae",
            "name": "",
            "active": true,
            "tosidebar": true,
            "console": false,
            "tostatus": false,
            "complete": "payload",
            "targetType": "msg",
            "x": 390,
            "y": 40,
            "wires": []
        },
        {
            "id": "c903e9dc.fd1ef8",
            "type": "wfwatch",
            "z": "211f2157.c10eae",
            "folder": "photo",
            "x": 70,
            "y": 100,
            "wires": [
                [
                    "d732c2a1.f1d38"
                ]
            ]
        },
        {
            "id": "4ed5eba3.d047f4",
            "type": "debug",
            "z": "211f2157.c10eae",
            "name": "",
            "active": true,
            "tosidebar": true,
            "console": false,
            "tostatus": false,
            "complete": "true",
            "targetType": "full",
            "x": 570,
            "y": 200,
            "wires": []
        },
        {
            "id": "d732c2a1.f1d38",
            "type": "switch",
            "z": "211f2157.c10eae",
            "name": "",
            "property": "payload.changeType",
            "propertyType": "msg",
            "rules": [
                {
                    "t": "eq",
                    "v": "delete",
                    "vt": "str"
                },
                {
                    "t": "else"
                }
            ],
            "checkall": "true",
            "repair": false,
            "outputs": 2,
            "x": 210,
            "y": 100,
            "wires": [
                [
                    "5659c311.8812fc"
                ],
                [
                    "7446262e.463018",
                    "6eb2e72a.e5c7a8",
                    "5659c311.8812fc"
                ]
            ]
        },
        {
            "id": "2fde816d.43eace",
            "type": "change",
            "z": "211f2157.c10eae",
            "name": "文件路径",
            "rules": [
                {
                    "t": "move",
                    "p": "msg.payload.filePath",
                    "pt": "msg",
                    "to": "msg.filename",
                    "tot": "msg"
                }
            ],
            "action": "",
            "property": "",
            "from": "",
            "to": "",
            "reg": false,
            "x": 420,
            "y": 260,
            "wires": [
                [
                    "2d61931e.557b9c",
                    "4ed5eba3.d047f4"
                ]
            ]
        },
        {
            "id": "fb7ba407.33bbb8",
            "type": "link out",
            "z": "211f2157.c10eae",
            "name": "",
            "links": [
                "7c24c551.86efdc"
            ],
            "x": 1215,
            "y": 260,
            "wires": []
        },
        {
            "id": "7c24c551.86efdc",
            "type": "link in",
            "z": "211f2157.c10eae",
            "name": "",
            "links": [
                "fb7ba407.33bbb8"
            ],
            "x": 75,
            "y": 360,
            "wires": [
                [
                    "87349201.502d3"
                ]
            ]
        },
        {
            "id": "87349201.502d3",
            "type": "function",
            "z": "211f2157.c10eae",
            "name": "判断是猫是狗",
            "func": "var len = msg.payload.result.length;\nfor(var i = 0;i<len;i++){\n    var nam = msg.payload.result[i].name;\n    if(nam.indexOf(\"猫\")!=-1){\n         return [msg,null,null];\n         break;\n    }\n    else if(nam.indexOf(\"狗\")!=-1){\n        return [null,msg,null];\n        break;\n    }\n}\nreturn [null,null,msg];",
            "outputs": 3,
            "noerr": 0,
            "x": 250,
            "y": 360,
            "wires": [
                [
                    "e84be81c.243138"
                ],
                [
                    "147eac4.3574f54"
                ],
                [
                    "9fd9d17d.e0e66"
                ]
            ]
        },
        {
            "id": "febc3c0b.5971a",
            "type": "bizwechat-upload",
            "z": "211f2157.c10eae",
            "name": "",
            "bizwechat": "dcc952ef.2d506",
            "x": 740,
            "y": 340,
            "wires": [
                [
                    "c6b54f7e.b505c"
                ]
            ]
        },
        {
            "id": "c6b54f7e.b505c",
            "type": "function",
            "z": "211f2157.c10eae",
            "name": "小猫来了",
            "func": "msg.payload = {\n   \"touser\" : \"@all\",\n   \"msgtype\" : \"mpnews\",\n   \"mpnews\" : {\n       \"articles\":[\n           {\n               \"title\": \"小猫来啦\", \n               \"thumb_media_id\": `${msg.payload.media_id}`,\n               //\"author\": \"Author\",\n               //\"content_source_url\": \"URL\",\n               \"content\": `${msg.payload.media_id}`,\n               \"digest\": \"小猫咪,喵喵喵\"\n            }\n       ]\n   },\n   \"safe\":0\n}\nreturn msg;",
            "outputs": 1,
            "noerr": 0,
            "x": 920,
            "y": 340,
            "wires": [
                [
                    "d67d9ddd.13679"
                ]
            ]
        },
        {
            "id": "d67d9ddd.13679",
            "type": "bizwechat-push",
            "z": "211f2157.c10eae",
            "name": "",
            "bizwechat": "dcc952ef.2d506",
            "x": 1120,
            "y": 340,
            "wires": [
                [
                    "51df7a82.dd6164"
                ]
            ]
        },
        {
            "id": "51df7a82.dd6164",
            "type": "debug",
            "z": "211f2157.c10eae",
            "name": "",
            "active": true,
            "tosidebar": true,
            "console": false,
            "tostatus": false,
            "complete": "true",
            "x": 1310,
            "y": 340,
            "wires": []
        },
        {
            "id": "68d8ffdf.8bdd8",
            "type": "function",
            "z": "211f2157.c10eae",
            "name": "设定上传",
            "func": "msg.type = \"image\"; //上传临时文件必须要的\nmsg.filename = msg.filename; //文件名可以随便写\nreturn msg;",
            "outputs": 1,
            "noerr": 0,
            "x": 560,
            "y": 340,
            "wires": [
                [
                    "febc3c0b.5971a",
                    "88976cf9.bb4bd"
                ]
            ]
        },
        {
            "id": "18714c43.d539b4",
            "type": "bizwechat-upload",
            "z": "211f2157.c10eae",
            "name": "",
            "bizwechat": "dcc952ef.2d506",
            "x": 740,
            "y": 400,
            "wires": [
                [
                    "ea9f9f34.818a5"
                ]
            ]
        },
        {
            "id": "ea9f9f34.818a5",
            "type": "function",
            "z": "211f2157.c10eae",
            "name": "狗来了",
            "func": "msg.payload = {\n   \"touser\" : \"@all\",\n   \"msgtype\" : \"mpnews\",\n   \"mpnews\" : {\n       \"articles\":[\n           {\n               \"title\": \"狗来了\", \n               \"thumb_media_id\": `${msg.payload.media_id}`,\n               //\"author\": \"Author\",\n               //\"content_source_url\": \"URL\",\n               \"content\": `狗来了狗来了`,\n               \"digest\": \"狗来了狗来了\"\n            }\n       ]\n   },\n   \"safe\":0\n}\nreturn msg;",
            "outputs": 1,
            "noerr": 0,
            "x": 910,
            "y": 400,
            "wires": [
                [
                    "ccced67c.7429a8"
                ]
            ]
        },
        {
            "id": "ccced67c.7429a8",
            "type": "bizwechat-push",
            "z": "211f2157.c10eae",
            "name": "",
            "bizwechat": "dcc952ef.2d506",
            "x": 1120,
            "y": 400,
            "wires": [
                [
                    "40ca7ccd.f582b4"
                ]
            ]
        },
        {
            "id": "40ca7ccd.f582b4",
            "type": "debug",
            "z": "211f2157.c10eae",
            "name": "",
            "active": true,
            "tosidebar": true,
            "console": false,
            "tostatus": false,
            "complete": "true",
            "x": 1310,
            "y": 400,
            "wires": []
        },
        {
            "id": "1bda1130.a220ff",
            "type": "function",
            "z": "211f2157.c10eae",
            "name": "设定上传",
            "func": "msg.type = \"image\"; //上传临时文件必须要的\nmsg.filename = msg.filename; //文件名可以随便写\nreturn msg;",
            "outputs": 1,
            "noerr": 0,
            "x": 560,
            "y": 400,
            "wires": [
                [
                    "18714c43.d539b4"
                ]
            ]
        },
        {
            "id": "88976cf9.bb4bd",
            "type": "debug",
            "z": "211f2157.c10eae",
            "name": "",
            "active": true,
            "tosidebar": true,
            "console": false,
            "tostatus": false,
            "complete": "true",
            "targetType": "full",
            "x": 730,
            "y": 520,
            "wires": []
        },
        {
            "id": "e84be81c.243138",
            "type": "file in",
            "z": "211f2157.c10eae",
            "name": "",
            "filename": "",
            "format": "",
            "chunk": false,
            "sendError": false,
            "encoding": "binary",
            "x": 430,
            "y": 340,
            "wires": [
                [
                    "68d8ffdf.8bdd8"
                ]
            ]
        },
        {
            "id": "147eac4.3574f54",
            "type": "file in",
            "z": "211f2157.c10eae",
            "name": "",
            "filename": "",
            "format": "",
            "chunk": false,
            "sendError": false,
            "encoding": "binary",
            "x": 430,
            "y": 400,
            "wires": [
                [
                    "1bda1130.a220ff"
                ]
            ]
        },
        {
            "id": "33867db5.e09152",
            "type": "bizwechat-upload",
            "z": "211f2157.c10eae",
            "name": "",
            "bizwechat": "dcc952ef.2d506",
            "x": 740,
            "y": 460,
            "wires": [
                [
                    "6b2f12b4.720a3c"
                ]
            ]
        },
        {
            "id": "6b2f12b4.720a3c",
            "type": "function",
            "z": "211f2157.c10eae",
            "name": "误报",
            "func": "msg.payload = {\n   \"touser\" : \"@all\",\n   \"msgtype\" : \"mpnews\",\n   \"mpnews\" : {\n       \"articles\":[\n           {\n               \"title\": \"误报\", \n               \"thumb_media_id\": `${msg.payload.media_id}`,\n               //\"author\": \"Author\",\n               //\"content_source_url\": \"URL\",\n               \"content\": `aaa`,\n               \"digest\": \"别紧张\"\n            }\n       ]\n   },\n   \"safe\":0\n}\nreturn msg;",
            "outputs": 1,
            "noerr": 0,
            "x": 910,
            "y": 460,
            "wires": [
                [
                    "61626474.34ec0c"
                ]
            ]
        },
        {
            "id": "61626474.34ec0c",
            "type": "bizwechat-push",
            "z": "211f2157.c10eae",
            "name": "",
            "bizwechat": "dcc952ef.2d506",
            "x": 1120,
            "y": 460,
            "wires": [
                [
                    "ae153b49.027c28"
                ]
            ]
        },
        {
            "id": "ae153b49.027c28",
            "type": "debug",
            "z": "211f2157.c10eae",
            "name": "",
            "active": true,
            "tosidebar": true,
            "console": false,
            "tostatus": false,
            "complete": "true",
            "x": 1310,
            "y": 460,
            "wires": []
        },
        {
            "id": "8f0c9a81.6837b8",
            "type": "function",
            "z": "211f2157.c10eae",
            "name": "设定上传",
            "func": "msg.type = \"image\"; //上传临时文件必须要的\nmsg.filename = msg.filename; //文件名可以随便写\nreturn msg;",
            "outputs": 1,
            "noerr": 0,
            "x": 560,
            "y": 460,
            "wires": [
                [
                    "33867db5.e09152"
                ]
            ]
        },
        {
            "id": "9fd9d17d.e0e66",
            "type": "file in",
            "z": "211f2157.c10eae",
            "name": "",
            "filename": "",
            "format": "",
            "chunk": false,
            "sendError": false,
            "encoding": "binary",
            "x": 430,
            "y": 460,
            "wires": [
                [
                    "8f0c9a81.6837b8"
                ]
            ]
        },
        {
            "id": "dcc952ef.2d506",
            "type": "bizwechat-configurator",
            "z": "",
            "name": "导出专用",
            "port": "",
            "corpid": "",
            "agentid": "",
            "corpsecret": "",
            "url": "",
            "token": "",
            "aeskey": "",
            "client_id": "",
            "client_secret": ""
        }
    ]


上图把狗识别出猫是因为返回结果里没有狗这个字,而出现了一次猫这个字导致的。所以需要经过更多的数据采集来完善判断。