{"id":33733,"date":"2024-11-25T09:49:19","date_gmt":"2024-11-25T01:49:19","guid":{"rendered":"https:\/\/fwq.ai\/blog\/33733\/"},"modified":"2024-11-25T09:49:19","modified_gmt":"2024-11-25T01:49:19","slug":"%e5%b0%8f%e7%a8%8b%e5%ba%8f%e8%b0%83%e7%94%a8%e7%99%be%e5%ba%a6%e4%ba%91%e6%8e%a5%e5%8f%a3%e5%ae%9e%e7%8e%b0%e4%ba%ba%e8%84%b8%e8%af%86%e5%88%ab","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/33733\/","title":{"rendered":"\u5c0f\u7a0b\u5e8f\u8c03\u7528\u767e\u5ea6\u4e91\u63a5\u53e3\u5b9e\u73b0\u4eba\u8138\u8bc6\u522b"},"content":{"rendered":"<h2> \u680f\u76ee\u4ecb\u7ecd\u4e0d\u4e00\u6837\u7684\u5b9e\u73b0\u4eba\u8138\u8bc6\u522b\u65b9\u6cd5<\/h2>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/052\/5fd730fb6392d672.jpg\" class=\"aligncenter\" title=\"\u5c0f\u7a0b\u5e8f\u8c03\u7528\u767e\u5ea6\u4e91\u63a5\u53e3\u5b9e\u73b0\u4eba\u8138\u8bc6\u522b\u63d2\u56fe\" alt=\"\u5c0f\u7a0b\u5e8f\u8c03\u7528\u767e\u5ea6\u4e91\u63a5\u53e3\u5b9e\u73b0\u4eba\u8138\u8bc6\u522b\u63d2\u56fe\" \/><\/p>\n<blockquote><p>\n  \u76f8\u5173\u514d\u8d39\u5b66\u4e60\u63a8\u8350\uff1a\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u6559\u7a0b\n<\/p><\/blockquote>\n<p>\u4e00&nbsp; \u51c6\u5907\u597d\u767e\u5ea6\u4e91\u7684\u5f00\u53d1\u8005\u8d26\u53f7<\/p>\n<ol>\n<li>\u767b\u5f55<\/li>\n<li>\u8fdb\u5165\u63a7\u5236\u53f0<\/li>\n<li>\u4eba\u5de5\u667a\u80fd&#8212;&#8212;\u56fe\u50cf\u8bc6\u522b<\/li>\n<li>\u521b\u5efa\u5e94\u7528&nbsp; &nbsp; &nbsp; &nbsp;<\/li>\n<\/ol>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<img decoding=\"async\" style=\"max-width:90%\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/052\/9dec0f148ad0b8e4273727a52ae4e37d-0.png\" class=\"aligncenter\" title=\"\u5c0f\u7a0b\u5e8f\u8c03\u7528\u767e\u5ea6\u4e91\u63a5\u53e3\u5b9e\u73b0\u4eba\u8138\u8bc6\u522b\u63d2\u56fe1\" alt=\"\u5c0f\u7a0b\u5e8f\u8c03\u7528\u767e\u5ea6\u4e91\u63a5\u53e3\u5b9e\u73b0\u4eba\u8138\u8bc6\u522b\u63d2\u56fe1\" \/>&nbsp;<\/p>\n<p>\u83b7\u53d6\u63a5\u53e3\u9700\u8981\u7684\u53c2\u6570<\/p>\n<p><img decoding=\"async\" style=\"max-width:90%\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/052\/ee4fe44c696d3238bbc3de79acdd3438-1.png\" class=\"aligncenter\" title=\"\u5c0f\u7a0b\u5e8f\u8c03\u7528\u767e\u5ea6\u4e91\u63a5\u53e3\u5b9e\u73b0\u4eba\u8138\u8bc6\u522b\u63d2\u56fe2\" alt=\"\u5c0f\u7a0b\u5e8f\u8c03\u7528\u767e\u5ea6\u4e91\u63a5\u53e3\u5b9e\u73b0\u4eba\u8138\u8bc6\u522b\u63d2\u56fe2\" \/><\/p>\n<p>\u67e5\u770b\u5b98\u7f51API\u6587\u6863<\/p>\n<p><img decoding=\"async\" style=\"max-width:90%\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/052\/ee4fe44c696d3238bbc3de79acdd3438-2.png\" class=\"aligncenter\" title=\"\u5c0f\u7a0b\u5e8f\u8c03\u7528\u767e\u5ea6\u4e91\u63a5\u53e3\u5b9e\u73b0\u4eba\u8138\u8bc6\u522b\u63d2\u56fe3\" alt=\"\u5c0f\u7a0b\u5e8f\u8c03\u7528\u767e\u5ea6\u4e91\u63a5\u53e3\u5b9e\u73b0\u4eba\u8138\u8bc6\u522b\u63d2\u56fe3\" \/><\/p>\n<p>\u4e8c \u9875\u9762\u5e03\u5c40<\/p>\n<p><img decoding=\"async\" style=\"max-width:90%\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/052\/9a39694d3cd963eacf4e8cd8425717a4-3.png\" class=\"aligncenter\" title=\"\u5c0f\u7a0b\u5e8f\u8c03\u7528\u767e\u5ea6\u4e91\u63a5\u53e3\u5b9e\u73b0\u4eba\u8138\u8bc6\u522b\u63d2\u56fe4\" alt=\"\u5c0f\u7a0b\u5e8f\u8c03\u7528\u767e\u5ea6\u4e91\u63a5\u53e3\u5b9e\u73b0\u4eba\u8138\u8bc6\u522b\u63d2\u56fe4\" \/><\/p>\n<p><img decoding=\"async\" style=\"max-width:90%\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/052\/9a39694d3cd963eacf4e8cd8425717a4-4.png\" class=\"aligncenter\" title=\"\u5c0f\u7a0b\u5e8f\u8c03\u7528\u767e\u5ea6\u4e91\u63a5\u53e3\u5b9e\u73b0\u4eba\u8138\u8bc6\u522b\u63d2\u56fe5\" alt=\"\u5c0f\u7a0b\u5e8f\u8c03\u7528\u767e\u5ea6\u4e91\u63a5\u53e3\u5b9e\u73b0\u4eba\u8138\u8bc6\u522b\u63d2\u56fe5\" \/><\/p>\n<p>\u6587\u4ef6ai.wxml\uff1a<\/p>\n<pre>&lt;view class=\"c1\"&gt;\n    &lt;view class=\"c1-1\"&gt;\n       \n    &lt;\/view&gt;\n    &lt;button type=\"primary\" size=\"mini\" bindtap=\"chooseImage\"&gt;\u9009\u62e9\u56fe\u7247&lt;\/button&gt;\n\n    &lt;view class=\"c1-2\"&gt;\n      &lt;image src=\"{{img}}\" mode=\"widthFix\"&gt;&lt;\/image&gt;\n   \n       &lt;text&gt;\u989c\u503c\uff1a{{face.beauty}}&lt;\/text&gt;\n       &lt;text&gt;\u5e74\u9f84\uff1a{{face.age}}&lt;\/text&gt;\n       &lt;text&gt;\u6027\u522b\uff1a{{face.gender.type}}&lt;\/text&gt;\n       &lt;text&gt;\u60c5\u7eea\uff1a{{face.emotion.type}}&lt;\/text&gt;\n    &lt;\/view&gt;\n&lt;\/view&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7f16\u5199\u6837\u5f0f\u6587\u4ef6ai.wxss<\/p>\n<pre>.c1{\n  padding: 50rpx;\n\n}\n.c1-1{\n  height: 800rpx;\n  margin-bottom: 20rpx;\n  display: flex;\n  justify-content: center;\n  font-size: 30rpx;\n  box-shadow: 0px 0px 10px gray;\n}\n.c1-2{\n\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u9875\u9762\u5e03\u5c40\u5982\u4e0b\uff1a&nbsp;<\/p>\n<p><img decoding=\"async\" style=\"max-width:90%\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/052\/9a39694d3cd963eacf4e8cd8425717a4-5.png\" class=\"aligncenter\" title=\"\u5c0f\u7a0b\u5e8f\u8c03\u7528\u767e\u5ea6\u4e91\u63a5\u53e3\u5b9e\u73b0\u4eba\u8138\u8bc6\u522b\u63d2\u56fe6\" alt=\"\u5c0f\u7a0b\u5e8f\u8c03\u7528\u767e\u5ea6\u4e91\u63a5\u53e3\u5b9e\u73b0\u4eba\u8138\u8bc6\u522b\u63d2\u56fe6\" \/><\/p>\n<p>ai.js<\/p>\n<pre>\/\/\u83b7\u53d6app.js\u5bf9\u8c61\nvar app = getApp();\n\nPage({\n  data: {\n\tface: {},\/\/\u68c0\u6d4b\u7ed3\u679c\n\timg: '',  \/\/\u9009\u62e9\u7684\u56fe\u7247\n\tshowResult: false \/\/\u68c0\u6d4b\u662f\u7531\u6709\u7ed3\u679c\n  },\n  onLoad: function (options) {\n\t\/\/console.log('\u83b7\u53d6\u5168\u5c40\u53d8\u91cf\u6570\u636e\uff1a' + app.globalData.access_token);\n  },\n  \/\/\u9009\u62e9\u56fe\u7247\u4e8b\u4ef6\n  chooseImage(){\n\t  var that = this;\n\t  wx.chooseImage({\n\t    count: 1,\n\t    sizeType: ['original', 'compressed'],\n\t    sourceType: ['album', 'camera'],\n\t    success (res) {\n\t      const tempPath = res.tempFilePaths[0];\/\/\u83b7\u53d6\u9009\u62e9\u7684\u56fe\u7247\u7684\u5730\u5740\n\t\t  \/\/\u51c6\u5907\u597d\u4e86access_token\u548c\u56fe\u7247\u540e\uff0c\u5c31\u53ef\u4ee5\u5f00\u59cb\u8bf7\u6c42\u767e\u5ea6\u7684\u4eba\u8138\u68c0\u6d4b\u63a5\u53e3\u4e86https:\/\/aip.baidubce.com\/rest\/2.0\/face\/v3\/detect\n\t\t  \/\/\u8be5\u63a5\u53e3\u9700\u8981\u7684\u53c2\u6570\u662faccess_token\uff0c\u56fe\u7247\u7684base64\u503c\n\t\t  \/\/\u56fe\u7247\u7684base64\u503c\u7684\u5904\u7406\n\t\t  var base64 = wx.getFileSystemManager().readFileSync(tempPath,'base64');\n\t\t  \/\/\u63d0\u793a\n\t\t  wx.showLoading({\n\t\t\t  title: '\u4eba\u8138\u68c0\u6d4b\u4e2d...',\n\t\t\t  mask: true\n\t\t  });\n\t\t  \/\/\u5f00\u59cb\u8bf7\u6c42\u767e\u5ea6\u7684\u4eba\u8138\u68c0\u6d4b\u63a5\u53e3\n\t\t  wx.request({\n\t\t    url: 'https:\/\/aip.baidubce.com\/rest\/2.0\/face\/v3\/detect?access_token='+app.globalData.access_token,\n      \n\t\t    data: {\n\t\t    image: base64,\n\t\t\t  image_type: 'BASE64',\n\t\t\t face_field: 'age,beauty,expression,face_shape,gender,glasses,race,emotion'\n\t\t    face_field: 'name, kind'\n        },\n\t\t\tmethod: 'POST',\n\t\t    header: {'content-type': 'application\/json'},\n\t\t    \/\/header: {'content-type': 'application\/x-www-form-urlencoded'},\n        success (res) {\n\t\t\t\tconsole.log(res);\n\t\t\t\tif(res.statusCode == 200 &amp;&amp; res.data.error_code == 0){ \/\/\u68c0\u6d4b\u7ed3\u679c\u6b63\u786e\n\t\t\t\t\t\/\/\u5c06\u9009\u62e9\u7684\u56fe\u7247\u56de\u663e\u5230\u9875\u9762\n\t\t\t\t\t\/\/that.setData({img: tempPath});\n          that.setData();\n          \/\/\u690d\u7269\u8bc6\u522b\u8981\u4f20\u5165\u952e\u503c\u5bf9\n\t\t\t\t\t\/\/\u53d6\u51fa\u68c0\u6d4b\u7684\u7ed3\u679c\u8fdb\u884c\u9875\u9762\u663e\u793a\n\t\t\t\t\tvar face = res.data.result.face_list[0];\n          console.log(face);\n\t\t\t\t\tthat.setData({face: face,showResult: true});\n\t\t\t\t\t\/\/\u9690\u85cf\u52a0\u8f7d\u7a97\u53e3\n\t\t\t\t\twx.hideLoading();\n\t\t\t\t}else{\n\t\t\t\t\twx.showToast({\n\t\t\t\t\t\ttitle: '\u68c0\u6d4b\u5931\u8d25'+res.data.error_msg,\n\t\t\t\t\t\tduration: 5000\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t    }\n\t\t  })\n\t    }\n\t  })\n  },\n  \n  \/**\n   * \u7528\u6237\u70b9\u51fb\u53f3\u4e0a\u89d2\u5206\u4eab\n   *\/\n  onShareAppMessage: function () {\n\n  }\n})<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>app.js<\/p>\n<pre>\/\/app.js\nApp({\n  onLaunch: function () {\n    var access_token = wx.getStorageSync('access_token');\n    var expire_in = wx.getStorageSync('expire_in');\n   \/\/ var access_token = parse;\n    var access_token_date = parseInt(wx.getStorageSync('access_token_date'));\n    var now = new Date().getTime();\n    if(!access_token){\n      this.requestToken();\n\n    } else if(now &gt; access_token_date + expire_in){\n      this.requestToken();\n    }else{\n\n    }\n  \n\n    \/\/ \u5c55\u793a\u672c\u5730\u5b58\u50a8\u80fd\u529b\n    var logs = wx.getStorageSync('logs') || []\n    logs.unshift(Date.now())\n    wx.setStorageSync('logs', logs)\n\n    \/\/ \u767b\u5f55\n    wx.login({\n      success: res =&gt; {\n        \/\/ \u53d1\u9001 res.code \u5230\u540e\u53f0\u6362\u53d6 openId, sessionKey, unionId\n      }\n    })\n    \/\/ \u83b7\u53d6\u7528\u6237\u4fe1\u606f\n    wx.getSetting({\n      success: res =&gt; {\n        if (res.authSetting['scope.userInfo']) {\n          \/\/ \u5df2\u7ecf\u6388\u6743\uff0c\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528 getUserInfo \u83b7\u53d6\u5934\u50cf\u6635\u79f0\uff0c\u4e0d\u4f1a\u5f39\u6846\n          wx.getUserInfo({\n            success: res =&gt; {\n              \/\/ \u53ef\u4ee5\u5c06 res \u53d1\u9001\u7ed9\u540e\u53f0\u89e3\u7801\u51fa unionId\n              this.globalData.userInfo = res.userInfo\n\n              \/\/ \u7531\u4e8e getUserInfo \u662f\u7f51\u7edc\u8bf7\u6c42\uff0c\u53ef\u80fd\u4f1a\u5728 Page.onLoad \u4e4b\u540e\u624d\u8fd4\u56de\n              \/\/ \u6240\u4ee5\u6b64\u5904\u52a0\u5165 callback \u4ee5\u9632\u6b62\u8fd9\u79cd\u60c5\u51b5\n              if (this.userInfoReadyCallback) {\n                this.userInfoReadyCallback(res)\n              }\n            }\n          })\n        }\n      }\n    })\n    \n  },\n  globalData: {\n    userInfo: null\n  },\n\n  requestToken() {\n    var that = this;\n    wx.request({\n      url: 'https:\/\/aip.baidubce.com\/oauth\/2.0\/token',\n      data: {\n        grant_type: 'client_credentials',\n        \/\/ aaa\u90a3\u91cc\u586b\u5199\u81ea\u5df1\u7684\u767e\u5ea6key\u503c\n        client_id: 'aaa',\n        client_secret: 'aaa'\n\n      },\n      \/\/header: {'content-type': 'application\/json'},\n      header: {'content-type': 'application\/x-www-form-urlencoded'},\n      success (res) {\n        if(res.statusCode == 200){\n          wx.setStorageSync(\"access_token\", res.data.access_token);\n          wx.setStorageSync(\"expires_in\", res.data.expires_in);\n          \/\/wx.setStorageSync(\"access_token_date\", res.data.access_token_date);\n          wx.setStorageSync(\"access_token_date\", new Date().getTime());\n          that.globalData.access_token = res.data.access_token;\n        }\n      }\n    })\n  }\n})<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8bc6\u522b\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<p><img decoding=\"async\" style=\"max-width:90%\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/052\/9a39694d3cd963eacf4e8cd8425717a4-6.png\" class=\"aligncenter\" title=\"\u5c0f\u7a0b\u5e8f\u8c03\u7528\u767e\u5ea6\u4e91\u63a5\u53e3\u5b9e\u73b0\u4eba\u8138\u8bc6\u522b\u63d2\u56fe7\" alt=\"\u5c0f\u7a0b\u5e8f\u8c03\u7528\u767e\u5ea6\u4e91\u63a5\u53e3\u5b9e\u73b0\u4eba\u8138\u8bc6\u522b\u63d2\u56fe7\" \/><\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5c0f\u7a0b\u5e8f\u8c03\u7528\u767e\u5ea6\u4e91\u63a5\u53e3\u5b9e\u73b0\u4eba\u8138\u8bc6\u522b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u680f\u76ee\u4ecb\u7ecd\u4e0d\u4e00\u6837\u7684\u5b9e\u73b0\u4eba\u8138\u8bc6\u522b\u65b9\u6cd5 \u76f8\u5173\u514d\u8d39\u5b66\u4e60\u63a8\u8350\uff1a\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u6559\u7a0b \u4e00&nbsp; \u51c6\u5907\u597d\u767e\u5ea6\u4e91\u7684\u5f00\u53d1\u8005\u8d26\u53f7 \u767b\u5f55 \u8fdb\u5165\u63a7\u5236\u53f0 \u4eba\u5de5\u667a\u80fd&#8212;&#8212;\u56fe\u50cf\u8bc6\u522b \u521b\u5efa\u5e94\u7528&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; \u83b7\u53d6\u63a5\u53e3\u9700\u8981\u7684\u53c2\u6570 \u67e5\u770b\u5b98\u7f51API\u6587\u6863 \u4e8c \u9875\u9762\u5e03\u5c40 \u6587\u4ef6ai.wxml\uff1a &lt;view class=&#8221;c1&#8243;&gt; &lt;view class=&#8221;c1-1&#8243;&gt; &lt;\/view&gt; &lt;button type=&#8221;primary&#8221; size=&#8221;mini&#8221; bindtap=&#8221;chooseImage&#8221;&gt;\u9009\u62e9\u56fe\u7247&lt;\/button&gt; &lt;view class=&#8221;c1-2&#8243;&gt; &lt;image src=&#8221;{{img}}&#8221; mode=&#8221;widthFix&#8221;&gt;&lt;\/image&gt; &lt;text&gt;\u989c\u503c\uff1a{{face.beauty}}&lt;\/text&gt; &lt;text&gt;\u5e74\u9f84\uff1a{{face.age}}&lt;\/text&gt; &lt;text&gt;\u6027\u522b\uff1a{{face.gender.type}}&lt;\/text&gt; &lt;text&gt;\u60c5\u7eea\uff1a{{face.emotion.type}}&lt;\/text&gt; &lt;\/view&gt; &lt;\/view&gt; \u767b\u5f55\u540e\u590d\u5236 \u7f16\u5199\u6837\u5f0f\u6587\u4ef6ai.wxss .c1{ padding: 50rpx; } .c1-1{ height: 800rpx; margin-bottom: 20rpx; display: flex; justify-content: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-33733","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/33733","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=33733"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/33733\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=33733"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=33733"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=33733"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}