人体关键点识别
所有文档

          人体分析

          人体关键点识别

          接口描述

          对于输入的一张图片(可正常解码,且长宽比适宜),检测图片中的所有人体,输出每个人体的21个主要关键点,包含头顶、五官、脖颈、四肢等部位,同时输出人体的坐标信息和数量

          支持多人检测、人体位置重叠、遮挡、背面、侧面、中低空俯拍、大动作等复杂场景。

          21个关键点的位置:头顶、左耳、右耳、左眼、右眼、鼻子、左嘴角、右嘴角、脖子、左肩、右肩、左手肘、右手肘、左手腕、右手腕、左髋部、右髋部、左膝、右膝、左脚踝、右脚踝。示意图如下,正在持续扩展更多关键点,敬请期待。

          单人场景:

          多人场景:

          请求说明

          请求示例

          HTTP 方法:POST

          请求URL: https://aip.baidubce.com/rest/2.0/image-classify/v1/body_analysis

          URL参数:

          参数
          access_token 通过API Key和Secret Key获取的access_token,参考“Access Token获取

          Header如下:

          参数
          Content-Type application/x-www-form-urlencoded

          Body中放置请求参数,参数详情如下:

          请求参数

          参数 是否必选 类型 可选值范围 说明
          image string - 图像数据,base64编码后进行urlencode,要求base64编码和urlencode后大小不超过4M。图片的base64编码是不包含图片头的,如(data:image/jpg;base64,),支持图片格式:jpg、bmp、png,最短边至少50px,最长边最大4096px

          请求代码示例

          提示一:使用示例代码前,请记得替换其中的示例Token、图片地址或Base64信息。

          提示二:部分语言依赖的类或库,请在代码注释中查看下载地址。

          人体关键点识别
          curl -i -k 'https://aip.baidubce.com/rest/2.0/image-classify/v1/body_analysis?access_token=【调用鉴权接口获取的token】' --data 'image=【图片Base64编码,需UrlEncode】' -H 'Content-Type:application/x-www-form-urlencoded'
          <?php
          /**
           * 发起http post请求(REST API), 并获取REST请求的结果
           * @param string $url
           * @param string $param
           * @return - http response body if succeeds, else false.
           */
          function request_post($url = '', $param = '')
          {
              if (empty($url) || empty($param)) {
                  return false;
              }
          
              $postUrl = $url;
              $curlPost = $param;
              // 初始化curl
              $curl = curl_init();
              curl_setopt($curl, CURLOPT_URL, $postUrl);
              curl_setopt($curl, CURLOPT_HEADER, 0);
              // 要求结果为字符串且输出到屏幕上
              curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
              curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
              // post提交方式
              curl_setopt($curl, CURLOPT_POST, 1);
              curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
              // 运行curl
              $data = curl_exec($curl);
              curl_close($curl);
          
              return $data;
          }
          
          $token = '[调用鉴权接口获取的token]';
          $url = 'https://aip.baidubce.com/rest/2.0/image-classify/v1/body_analysis?access_token=' . $token;
          $img = file_get_contents('[本地文件路径]');
          $img = base64_encode($img);
          $bodys = array(
              'image' => $img
          );
          $res = request_post($url, $bodys);
          
          var_dump($res);~~~~
          package com.baidu.ai.aip;
          
          import com.baidu.ai.aip.utils.Base64Util;
          import com.baidu.ai.aip.utils.FileUtil;
          import com.baidu.ai.aip.utils.HttpUtil;
          
          import java.net.URLEncoder;
          
          /**
          * 人体关键点识别
          */
          public class BodyAnalysis {
          
              /**
              * 重要提示代码中所需工具类
              * FileUtil,Base64Util,HttpUtil,GsonUtils请从
              * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
              * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
              * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
              * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
              * 下载
              */
              public static String body_analysis() {
                  // 请求url
                  String url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/body_analysis";
                  try {
                      // 本地文件路径
                      String filePath = "[本地文件路径]";
                      byte[] imgData = FileUtil.readFileByBytes(filePath);
                      String imgStr = Base64Util.encode(imgData);
                      String imgParam = URLEncoder.encode(imgStr, "UTF-8");
          
                      String param = "image=" + imgParam;
          
                      // 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
                      String accessToken = "[调用鉴权接口获取的token]";
          
                      String result = HttpUtil.post(url, accessToken, param);
                      System.out.println(result);
                      return result;
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
                  return null;
              }
          
              public static void main(String[] args) {
                  BodyAnalysis.body_analysis();
              }
          }
          # encoding:utf-8
          
          import requests
          import base64
          
          '''
          人体关键点识别
          '''
          
          request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/body_analysis"
          # 二进制方式打开图片文件
          f = open('[本地文件]', 'rb')
          img = base64.b64encode(f.read())
          
          params = {"image":img}
          access_token = '[调用鉴权接口获取的token]'
          request_url = request_url + "?access_token=" + access_token
          headers = {'content-type': 'application/x-www-form-urlencoded'}
          response = requests.post(request_url, data=params, headers=headers)
          if response:
              print (response.json())
          #include <iostream>
          #include <curl/curl.h>
          
          // libcurl库下载链接:https://curl.haxx.se/download.html
          // jsoncpp库下载链接:https://github.com/open-source-parsers/jsoncpp/
          const static std::string request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/body_analysis";
          static std::string body_analysis_result;
          /**
           * curl发送http请求调用的回调函数,回调函数中对返回的json格式的body进行了解析,解析结果储存在全局的静态变量当中
           * @param 参数定义见libcurl文档
           * @return 返回值定义见libcurl文档
           */
          static size_t callback(void *ptr, size_t size, size_t nmemb, void *stream) {
              // 获取到的body存放在ptr中,先将其转换为string格式
              body_analysis_result = std::string((char *) ptr, size * nmemb);
              return size * nmemb;
          }
          /**
           * 人体关键点识别
           * @return 调用成功返回0,发生错误返回其他错误码
           */
          int body_analysis(std::string &json_result, const std::string &access_token) {
              std::string url = request_url + "?access_token=" + access_token;
              CURL *curl = NULL;
              CURLcode result_code;
              int is_success;
              curl = curl_easy_init();
              if (curl) {
                  curl_easy_setopt(curl, CURLOPT_URL, url.data());
                  curl_easy_setopt(curl, CURLOPT_POST, 1);
                  curl_httppost *post = NULL;
                  curl_httppost *last = NULL;
                  curl_formadd(&post, &last, CURLFORM_COPYNAME, "image", CURLFORM_COPYCONTENTS, "【base64_img】", CURLFORM_END);
          
                  curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
                  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback);
                  result_code = curl_easy_perform(curl);
                  if (result_code != CURLE_OK) {
                      fprintf(stderr, "curl_easy_perform() failed: %s\n",
                              curl_easy_strerror(result_code));
                      is_success = 1;
                      return is_success;
                  }
                  json_result = body_analysis_result;
                  curl_easy_cleanup(curl);
                  is_success = 0;
              } else {
                  fprintf(stderr, "curl_easy_init() failed.");
                  is_success = 1;
              }
              return is_success;
          }
          using System;
          using System.IO;
          using System.Net;
          using System.Text;
          using System.Web;
          
          namespace com.baidu.ai
          {
              public class BodyAnalysis
              {
                  // 人体关键点识别
                  public static string body_analysis()
                  {
                      string token = "[调用鉴权接口获取的token]";
                      string host = "https://aip.baidubce.com/rest/2.0/image-classify/v1/body_analysis?access_token=" + token;
                      Encoding encoding = Encoding.Default;
                      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host);
                      request.Method = "post";
                      request.KeepAlive = true;
                      // 图片的base64编码
                      string base64 = getFileBase64("[本地图片文件]");
                      String str = "image=" + HttpUtility.UrlEncode(base64);
                      byte[] buffer = encoding.GetBytes(str);
                      request.ContentLength = buffer.Length;
                      request.GetRequestStream().Write(buffer, 0, buffer.Length);
                      HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                      StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
                      string result = reader.ReadToEnd();
                      Console.WriteLine("人体关键点识别:");
                      Console.WriteLine(result);
                      return result;
                  }
          
                  public static String getFileBase64(String fileName) {
                      FileStream filestream = new FileStream(fileName, FileMode.Open);
                      byte[] arr = new byte[filestream.Length];
                      filestream.Read(arr, 0, (int)filestream.Length);
                      string baser64 = Convert.ToBase64String(arr);
                      filestream.Close();
                      return baser64;
                  }
              }
          }

          返回说明

          接口除了返回人体框和每个关键点的坐标信息外,还会输出人体框和关键点的概率分数,实际应用中可以基于概率分数进行过滤,排除掉分数低的误识别“无效人体”推荐的过滤方案:当关键点得分大于0.2的个数大于3,且人体框的得分大于0.03时,才认为是有效人体

          实际应用中,可根据对误识别、漏识别的容忍程度,调整阈值过滤方案,灵活应用,比如对误识别容忍低的应用场景,人体框的得分阈值可以提到0.06甚至更高。

          返回参数

          字段 是否必选 类型 说明
          log_id uint64 唯一的log id,用于问题定位
          person_num uint32 人体数目
          person_info object[] 人体姿态信息
          +location object 人体坐标信息
          ++height float 人体区域的高度
          ++left float 人体区域离左边界的距离
          ++top float 人体区域离上边界的距离
          ++width float 人体区域的宽度
          ++score float 人体框的概率分数,取值0-1,得分越接近1表示识别准确的概率越大
          +body_parts object 身体部位信息,包含21个关键点
          ++top_head object 头顶
          +++x float x坐标
          +++y float y坐标
          +++score float 概率分数
          ++left_eye object 左眼
          +++x float x坐标
          +++y float y坐标
          +++score float 概率分数
          ++right_eye object 右眼
          +++x float x坐标
          +++y float y坐标
          +++score float 概率分数
          ++nose object 鼻子
          +++x float x坐标
          +++y float y坐标
          +++score float 概率分数
          ++left_ear object 左耳
          +++x float x坐标
          +++y float y坐标
          +++score float 概率分数
          ++right_ear object 右耳
          +++x float x坐标
          +++y float y坐标
          +++score float 概率分数
          ++left_mouth_corner object 左嘴角
          +++x float x坐标
          +++y float y坐标
          +++score float 概率分数
          ++right_mouth_corner object 右嘴角
          +++x float x坐标
          +++y float y坐标
          +++score float 概率分数
          ++neck object 颈部
          +++x float x坐标
          +++y float y坐标
          +++score float 概率分数
          ++left_shoulder object 左肩
          +++x float x坐标
          +++y float y坐标
          +++score float 概率分数
          ++right_shoulder object 右肩
          +++x float x坐标
          +++y float y坐标
          +++score float 概率分数
          ++left_elbow object 左手肘
          +++x float x坐标
          +++y float y坐标
          +++score float 概率分数
          ++right_elbow object 右手肘
          +++x float x坐标
          +++y float y坐标
          +++score float 概率分数
          ++left_wrist object 左手腕
          +++x float x坐标
          +++y float y坐标
          +++score float 概率分数
          ++right_wrist object 右手腕
          +++x float x坐标
          +++y float y坐标
          +++score float 概率分数
          ++left_hip object 左髋部
          +++x float x坐标
          +++y float y坐标
          +++score float 概率分数
          ++right_hip object 右髋部
          +++x float x坐标
          +++y float y坐标
          +++score float 概率分数
          ++left_knee object 左膝
          +++x float x坐标
          +++y float y坐标
          +++score float 概率分数
          ++right_knee object 右膝
          +++x float x坐标
          +++y float y坐标
          +++score float 概率分数
          ++left_ankle object 左脚踝
          +++x float x坐标
          +++y float y坐标
          +++score float 概率分数
          ++right_ankle object 右脚踝
          +++x float x坐标
          +++y float y坐标
          +++score float 概率分数

          说明:

          1、body_parts,一共21个part,每个part包含x,y两个坐标,如果part被截断,则x、y坐标为part被截断的图片边界位置,part顺序以实际返回顺序为准。

          2、接口返回人体坐标框和每个关键点的置信度分数,在应用时可综合置信度score分数,过滤掉置信度低的“无效人体”,建议过滤方法:当关键点得分大于0.2的个数大于3,且人体框的分数大于0.03时,才认为是有效人体。实际应用中,可根据对误识别、漏识别的容忍程度,调整阈值过滤方案,灵活应用。

          返回示例

          {
          	"person_num": 2,
          	"person_info": [
          		{
          			"body_parts": {
          				"left_hip": {
          					"y": 573,
          					"x": 686.09375,
          					"score": 0.78743487596512
          				},
          				"top_head": {
          					"y": 242.53125,
          					"x": 620,
          					"score": 0.87757384777069
          				},
          				"right_mouth_corner": {
          					"y": 308.625,
          					"x": 606.78125,
          					"score": 0.90121293067932
          				},
          				"neck": {
          					"y": 335.0625,
          					"x": 620,
          					"score": 0.84662038087845
          				},
          				"left_shoulder": {
          					"y": 361.5,
          					"x": 699.3125,
          					"score": 0.83550786972046
          				},
          				"left_knee": {
          					"y": 731.625,
          					"x": 699.3125,
          					"score": 0.83575332164764
          				},
          				"left_ankle": {
          					"y": 877.03125,
          					"x": 725.75,
          					"score": 0.85220056772232
          				},
          				"left_mouth_corner": {
          					"y": 308.625,
          					"x": 633.21875,
          					"score": 0.91475087404251
          				},
          				"right_elbow": {
          					"y": 348.28125,
          					"x": 461.375,
          					"score": 0.81766486167908
          				},
          				"right_ear": {
          					"y": 282.1875,
          					"x": 593.5625,
          					"score": 0.86551451683044
          				},
          				"nose": {
          					"y": 295.40625,
          					"x": 620,
          					"score": 0.90894532203674
          				},
          				"left_eye": {
          					"y": 282.1875,
          					"x": 633.21875,
          					"score": 0.89628517627716
          				},
          				"right_eye": {
          					"y": 282.1875,
          					"x": 606.78125,
          					"score": 0.89676940441132
          				},
          				"right_hip": {
          					"y": 586.21875,
          					"x": 593.5625,
          					"score": 0.79803824424744
          				},
          				"left_wrist": {
          					"y": 374.71875,
          					"x": 884.375,
          					"score": 0.89635348320007
          				},
          				"left_ear": {
          					"y": 295.40625,
          					"x": 659.65625,
          					"score": 0.86607384681702
          				},
          				"left_elbow": {
          					"y": 361.5,
          					"x": 791.84375,
          					"score": 0.83910942077637
          				},
          				"right_shoulder": {
          					"y": 348.28125,
          					"x": 553.90625,
          					"score": 0.85635334253311
          				},
          				"right_ankle": {
          					"y": 890.25,
          					"x": 580.34375,
          					"score": 0.85149073600769
          				},
          				"right_knee": {
          					"y": 744.84375,
          					"x": 580.34375,
          					"score": 0.83749794960022
          				},
          				"right_wrist": {
          					"y": 348.28125,
          					"x": 368.84375,
          					"score": 0.83893859386444
          				}
          			},
          			"location": {
          				"height": 703.20654296875,
          				"width": 652.61810302734,
          				"top": 221.92272949219,
          				"score": 0.99269664287567,
          				"left": 294.03039550781
          			}
          		},
          		{
          			"body_parts": {
          				"left_hip": {
          					"y": 576,
          					"x": 1239.5625,
          					"score": 0.84608125686646
          				},
          				"top_head": {
          					"y": 261.15625,
          					"x": 1176.59375,
          					"score": 0.871442258358
          				},
          				"right_mouth_corner": {
          					"y": 336.71875,
          					"x": 1164,
          					"score": 0.90951544046402
          				},
          				"neck": {
          					"y": 361.90625,
          					"x": 1176.59375,
          					"score": 0.85904294252396
          				},
          				"left_shoulder": {
          					"y": 361.90625,
          					"x": 1239.5625,
          					"score": 0.8512310385704
          				},
          				"left_knee": {
          					"y": 714.53125,
          					"x": 1277.34375,
          					"score": 0.82312393188477
          				},
          				"left_ankle": {
          					"y": 853.0625,
          					"x": 1315.125,
          					"score": 0.83786374330521
          				},
          				"left_mouth_corner": {
          					"y": 336.71875,
          					"x": 1189.1875,
          					"score": 0.90610301494598
          				},
          				"right_elbow": {
          					"y": 387.09375,
          					"x": 1025.46875,
          					"score": 0.88956367969513
          				},
          				"right_ear": {
          					"y": 311.53125,
          					"x": 1138.8125,
          					"score": 0.86518502235413
          				},
          				"nose": {
          					"y": 324.125,
          					"x": 1176.59375,
          					"score": 0.9168484210968
          				},
          				"left_eye": {
          					"y": 311.53125,
          					"x": 1189.1875,
          					"score": 0.91715461015701
          				},
          				"right_eye": {
          					"y": 311.53125,
          					"x": 1164,
          					"score": 0.90343600511551
          				},
          				"right_hip": {
          					"y": 576,
          					"x": 1164,
          					"score": 0.81976848840714
          				},
          				"left_wrist": {
          					"y": 298.9375,
          					"x": 1378.09375,
          					"score": 0.86095398664474
          				},
          				"left_ear": {
          					"y": 311.53125,
          					"x": 1201.78125,
          					"score": 0.86899447441101
          				},
          				"left_elbow": {
          					"y": 324.125,
          					"x": 1315.125,
          					"score": 0.89198768138885
          				},
          				"right_shoulder": {
          					"y": 387.09375,
          					"x": 1101.03125,
          					"score": 0.85161662101746
          				},
          				"right_ankle": {
          					"y": 878.25,
          					"x": 1151.40625,
          					"score": 0.83667933940887
          				},
          				"right_knee": {
          					"y": 727.125,
          					"x": 1151.40625,
          					"score": 0.85485708713531
          				},
          				"right_wrist": {
          					"y": 387.09375,
          					"x": 949.90625,
          					"score": 0.83042001724243
          				}
          			},
          			"location": {
          				"height": 670.80139160156,
          				"width": 524.25476074219,
          				"top": 241.42504882812,
          				"score": 0.98725789785385,
          				"left": 902.15216064453
          			}
          		}
          	],
          	"log_id": "6362401025381690607"
          }
          上一篇
          调用方式
          下一篇
          人体检测和属性识别