人体模型 java代码_【人体分析-人体关键点识别】-Java示例代码

该博客展示了如何使用Java API进行人体关键点识别。通过调用百度AI接口,解析返回的JSON数据,获取人体14个关键点的位置信息,包括四肢、脖颈和鼻子等。提供了完整的Java代码示例,包括接口调用和结果解析。
摘要由CSDN通过智能技术生成

接口说明:http://ai.baidu.com/forum/topic/show/497747

人体关键点识别,即对于输入的一张图片(可正常解码,且长宽比适宜),输出图片中的所有人体的14个关键点,包含四肢、脖颈、鼻子等部位,以及人体的坐标信息和数量

-----------------------------------------------------下面开始代码-----------------------------------------------------

人体关键点识别接口示例代码-(JavaAPI)

package com.xs.image;

import java.net.URLEncoder;

import com.alibaba.fastjson.JSON;

import com.xs.common.image.ImageAPI;

import com.xs.pojo.image.BodyAnalysisBean;

import com.xs.util.baidu.Base64Util;

import com.xs.util.baidu.FileUtil;

import com.xs.util.baidu.HttpUtil;

/**

* 人体关键点识别-JavaAPI示例代码(非官方)

* @author 小帅丶

*/

public class BodyAnalysisSample {

public static void main(String[] args) throws Exception {

//返回字符串

String result = getBodyAnalysisResult("本地图片路径", "自己的accesstoken");

System.out.println(result);

//返回java对象

//BodyAnalysisBean bodyAnalysisBean = getBodyAnalysisBean("本地图片路径", "自己的accesstoken");

//System.out.println("图中有"+bodyAnalysisBean.getPerson_num()+"个人");

}

/**

* 人体关键点识别Demo

* @param imagePath

* @param accessToken

* @return 字符串

* @throws Exception

*/

public static String getBodyAnalysisResult(String imagePath,String accessToken) throws Exception{

byte[] imgData = FileUtil.readFileByBytes(imagePath);

String imgStr = Base64Util.encode(imgData);

String param = "image=" + URLEncoder.encode(imgStr,"UTF-8");

// 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。

String result = HttpUtil.post(ImageAPI.BODYANALYSIS_API, accessToken, param);

System.out.println(result);

return result;

}

/**

* 人体关键点识别Demo

* @param imagePath

* @param accessToken

* @return CarMode对象

* @throws Exception

*/

public static BodyAnalysisBean getBodyAnalysisBean(String imagePath,String accessToken) throws Exception{

byte[] imgData = FileUtil.readFileByBytes(imagePath);

String imgStr = Base64Util.encode(imgData);

String param = "image=" + URLEncoder.encode(imgStr,"UTF-8");

// 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。

String result = HttpUtil.post(ImageAPI.BODYANALYSIS_API, accessToken, param);

BodyAnalysisBean bodyAnalysisBean = JSON.parseObject(result,BodyAnalysisBean.class);

System.out.println(result);

return bodyAnalysisBean;

}

}

识别返回的JSON数据(特别长,不做格式化)

{"person_num": 2, "person_info": [{"body_parts": {"right_wrist": {"y": 187.1351776123047, "x": 356.5876770019531}, "right_hip": {"y": 245.2404174804688, "x": 334.6790466308594}, "neck": {"y": 129.0636596679688, "x": 363.0102233886719}, "left_shoulder": {"y": 130.3773803710938, "x": 382.3029479980469}, "left_knee": {"y": 333.028076171875, "x": 375.8851013183594}, "right_elbow": {"y": 183.2697601318359, "x": 306.3629760742188}, "right_shoulder": {"y": 127.7778701782227, "x": 343.6992492675781}, "right_ankle": {"y": 441.4400329589844, "x": 290.8769836425781}, "left_hip": {"y": 246.5243682861328, "x": 364.3192138671875}, "left_ankle": {"y": 440.1803283691406, "x": 396.4710693359375}, "left_elbow": {"y": 189.7247772216797, "x": 374.6240844726562}, "left_wrist": {"y": 0.0, "x": 0.0}, "right_knee": {"y": 338.1686706542969, "x": 317.9450988769531}, "nose": {"y": 91.65258026123047, "x": 393.8951416015625}}, "location": {"width": 171.0154724121094, "top": 49.52960968017578, "left": 258.1662902832031, "height": 425.4703979492188}}, {"body_parts": {"right_wrist": {"y": 219.4005584716797, "x": 136.4501953125}, "right_hip": {"y": 220.7348327636719, "x": 133.8620758056641}, "neck": {"y": 103.2406234741211, "x": 141.5971832275391}, "left_shoulder": {"y": 101.9672470092773, "x": 158.2781219482422}, "left_knee": {"y": 296.859619140625, "x": 141.5889587402344}, "right_elbow": {"y": 162.6289672851562, "x": 117.1964874267578}, "right_shoulder": {"y": 105.8178405761719, "x": 128.6884613037109}, "right_ankle": {"y": 373.0259094238281, "x": 106.87158203125}, "left_hip": {"y": 220.7161102294922, "x": 150.6210479736328}, "left_ankle": {"y": 374.3226928710938, "x": 119.7342681884766}, "left_elbow": {"y": 165.2007141113281, "x": 154.4678649902344}, "left_wrist": {"y": 222.0592193603516, "x": 166.0699157714844}, "right_knee": {"y": 302.0010681152344, "x": 160.9004821777344}, "nose": {"y": 63.22677230834961, "x": 180.2331695556641}}, "location": {"width": 153.0357818603516, "top": 23.61567878723145, "left": 67.03448486328125, "height": 382.5894470214844}}], "log_id": 1955353647031530539}

原图文件

f627dfbb97ffea267164c67a047718b6.png

即使图上有17个人也是可以返回每个人的姿态信息哦(最好是有半个身子)

字符串返回不太方便处理。那就对象处理。不会转对象的。我已经帮忙做了。

JavaBean对象:

package com.xs.pojo.image;

import java.util.List;

/**

* 人体关键点识别 JavaBean

* @author 小帅丶

*

*/

public class BodyAnalysisBean {

//人体数目

private int person_num;

//人体姿态信息

private List person_info;

//唯一的log id,用于问题定位

private long log_id;

public int getPerson_num() {

return person_num;

}

public void setPerson_num(int person_num) {

this.person_num = person_num;

}

public List getPerson_info() {

return person_info;

}

public void setPerson_info(List person_info) {

this.person_info = person_info;

}

public long getLog_id() {

return log_id;

}

public void setLog_id(long log_id) {

this.log_id = log_id;

}

public static class Person_info {

private Body_parts body_parts;

private Location location;

public void setBody_parts(Body_parts body_parts) {

this.body_parts = body_parts;

}

public Body_parts getBody_parts() {

return body_parts;

}

public void setLocation(Location location) {

this.location = location;

}

public Location getLocation() {

return location;

}

}

public static class Body_parts {

private Right_wrist right_wrist;

private Right_hip right_hip;

private Neck neck;

private Left_shoulder left_shoulder;

private Left_knee left_knee;

private Right_elbow right_elbow;

private Right_shoulder right_shoulder;

private Right_ankle right_ankle;

private Left_hip left_hip;

private Left_ankle left_ankle;

private Left_elbow left_elbow;

private Left_wrist left_wrist;

private Right_knee right_knee;

private Nose nose;

public void setRight_wrist(Right_wrist right_wrist) {

this.right_wrist = right_wrist;

}

public Right_wrist getRight_wrist() {

return right_wrist;

}

public void setRight_hip(Right_hip right_hip) {

this.right_hip = right_hip;

}

public Right_hip getRight_hip() {

return right_hip;

}

public void setNeck(Neck neck) {

this.neck = neck;

}

public Neck getNeck() {

return neck;

}

public void setLeft_shoulder(Left_shoulder left_shoulder) {

this.left_shoulder = left_shoulder;

}

public Left_shoulder getLeft_shoulder() {

return left_shoulder;

}

public void setLeft_knee(Left_knee left_knee) {

this.left_knee = left_knee;

}

public Left_knee getLeft_knee() {

return left_knee;

}

public void setRight_elbow(Right_elbow right_elbow) {

this.right_elbow = right_elbow;

}

public Right_elbow getRight_elbow() {

return right_elbow;

}

public void setRight_shoulder(Right_shoulder right_shoulder) {

this.right_shoulder = right_shoulder;

}

public Right_shoulder getRight_shoulder() {

return right_shoulder;

}

public void setRight_ankle(Right_ankle right_ankle) {

this.right_ankle = right_ankle;

}

public Right_ankle getRight_ankle() {

return right_ankle;

}

public void setLeft_hip(Left_hip left_hip) {

this.left_hip = left_hip;

}

public Left_hip getLeft_hip() {

return left_hip;

}

public void setLeft_ankle(Left_ankle left_ankle) {

this.left_ankle = left_ankle;

}

public Left_ankle getLeft_ankle() {

return left_ankle;

}

public void setLeft_elbow(Left_elbow left_elbow) {

this.left_elbow = left_elbow;

}

public Left_elbow getLeft_elbow() {

return left_elbow;

}

public void setLeft_wrist(Left_wrist left_wrist) {

this.left_wrist = left_wrist;

}

public Left_wrist getLeft_wrist() {

return left_wrist;

}

public void setRight_knee(Right_knee right_knee) {

this.right_knee = right_knee;

}

public Right_knee getRight_knee() {

return right_knee;

}

public void setNose(Nose nose) {

this.nose = nose;

}

public Nose getNose() {

return nose;

}

}

public static class Left_ankle {

private double y;

private double x;

public void setY(double y) {

this.y = y;

}

public double getY() {

return y;

}

public void setX(double x) {

this.x = x;

}

public double getX() {

return x;

}

}

public static class Left_elbow {

private double y;

private double x;

public void setY(double y) {

this.y = y;

}

public double getY() {

return y;

}

public void setX(double x) {

this.x = x;

}

public double getX() {

return x;

}

}

public static class Left_hip {

private double y;

private double x;

public void setY(double y) {

this.y = y;

}

public double getY() {

return y;

}

public void setX(double x) {

this.x = x;

}

public double getX() {

return x;

}

}

public static class Left_knee {

private double y;

private double x;

public void setY(double y) {

this.y = y;

}

public double getY() {

return y;

}

public void setX(double x) {

this.x = x;

}

public double getX() {

return x;

}

}

public static class Left_shoulder {

private double y;

private double x;

public void setY(double y) {

this.y = y;

}

public double getY() {

return y;

}

public void setX(double x) {

this.x = x;

}

public double getX() {

return x;

}

}

public static class Left_wrist {

private int y;

private int x;

public void setY(int y) {

this.y = y;

}

public int getY() {

return y;

}

public void setX(int x) {

this.x = x;

}

public int getX() {

return x;

}

}

public static class Location {

private double width;

private double top;

private double left;

private double height;

public void setWidth(double width) {

this.width = width;

}

public double getWidth() {

return width;

}

public void setTop(double top) {

this.top = top;

}

public double getTop() {

return top;

}

public void setLeft(double left) {

this.left = left;

}

public double getLeft() {

return left;

}

public void setHeight(double height) {

this.height = height;

}

public double getHeight() {

return height;

}

}

public static class Neck {

private double y;

private double x;

public void setY(double y) {

this.y = y;

}

public double getY() {

return y;

}

public void setX(double x) {

this.x = x;

}

public double getX() {

return x;

}

}

public static class Nose {

private double y;

private double x;

public void setY(double y) {

this.y = y;

}

public double getY() {

return y;

}

public void setX(double x) {

this.x = x;

}

public double getX() {

return x;

}

}

public static class Right_ankle {

private double y;

private double x;

public void setY(double y) {

this.y = y;

}

public double getY() {

return y;

}

public void setX(double x) {

this.x = x;

}

public double getX() {

return x;

}

}

public static class Right_elbow {

private double y;

private double x;

public void setY(double y) {

this.y = y;

}

public double getY() {

return y;

}

public void setX(double x) {

this.x = x;

}

public double getX() {

return x;

}

}

public static class Right_hip {

private double y;

private double x;

public void setY(double y) {

this.y = y;

}

public double getY() {

return y;

}

public void setX(double x) {

this.x = x;

}

public double getX() {

return x;

}

}

public static class Right_knee {

private double y;

private double x;

public void setY(double y) {

this.y = y;

}

public double getY() {

return y;

}

public void setX(double x) {

this.x = x;

}

public double getX() {

return x;

}

}

public static class Right_wrist {

private double y;

private double x;

public void setY(double y) {

this.y = y;

}

public double getY() {

return y;

}

public void setX(double x) {

this.x = x;

}

public double getX() {

return x;

}

}

public static class Right_shoulder {

private double y;

private double x;

public void setY(double y) {

this.y = y;

}

public double getY() {

return y;

}

public void setX(double x) {

this.x = x;

}

public double getX() {

return x;

}

}

}

以上就是人体关键点识别示例代码-JavaAPI形式

代码地址:https://gitee.com/xshuai/ai/blob/master/AIDemo/src/main/java/com/xs/image/BodyAnalysisSample.java

C#语言(By 图像识别群 透明色(69046437))

using AForge.Video.DirectShow;

using DevExpress.XtraBars.Docking2010.Customization;

using DevExpress.XtraEditors;

using Emgu.CV;

using Emgu.CV.Structure;

using FaceDemo.Entity;

using FaceDemo.Entity.BodyAPI;

using FaceDemo.Entity.Config;

using FaceDemo.Entity.FaceAPI;

using FaceDemo.Utils;

using Newtonsoft.Json;

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Net;

using System.Text;

using System.Threading;

using System.Windows.Forms;

namespace FaceDemo

{

public partial class FormPassengerFlow XtraForm

{

public FormPassengerFlow()

{

InitializeComponent();

}

private ConfigEntity _ConfigEntity;

private string access_token;

private string authHost = httpsaip.baidubce.comoauth2.0token;获取Token POST

private string bodyAnalysisUrl = httpsaip.baidubce.comrest2.0image-classifyv1body_analysis;人体关键点识别 POST

private string bodyAttrUrl = httpsaip.baidubce.comrest2.0image-classifyv1body_attr;人体属性识别 POST

private string bodyNumUrl = httpsaip.baidubce.comrest2.0image-classifyv1body_num;人流量统计 POST

private Encoding encode = Encoding.UTF8;

private string contentType = applicationx-www-form-urlencoded;

private Rectangle detectArea = new Rectangle();检测区域

private int areaNum = 0;区域人数

private int inNum = 0;进入人数

private int outNum = 0;离开人数

private int curFPS = 1;

private bool isFormClose = false;

private void FormPassengerFlow_Load(object sender, EventArgs e)

{

_ConfigEntity = ConfigHelper.Read();

access_token = GetToken(_ConfigEntity.BodyAccount.APIKey, _ConfigEntity.BodyAccount.SecretKey);

获取并枚举所有摄像头设备

FilterInfoCollection Cameras = new FilterInfoCollection(FilterCategory.VideoInputDevice);

判断设备个数,选择第一个设备

if (Cameras != null && Cameras.Count 0)

{

VideoCaptureDevice vcd = new VideoCaptureDevice(Cameras[0].MonikerString);

检查摄像头支持的像素,优先设置为1280720

if (vcd.VideoCapabilities.ToList().Exists(p = p.FrameSize.Width == 1280 && p.FrameSize.Height == 720))

vcd.VideoResolution = vcd.VideoCapabilities.ToList().Find(p = p.FrameSize.Width == 1280 && p.FrameSize.Height == 720);

videoSourcePlayer1.VideoSource = vcd;

videoSourcePlayer1.NewFrame += VideoSourcePlayer1_NewFrame;

videoSourcePlayer1.Start();

}

}

private void VideoSourcePlayer1_NewFrame(object sender, ref Bitmap image)

{

Emgu.CV.ImageBgr, Byte imageCV = new ImageBgr, byte(image.Clone() as Bitmap);

抽帧进行客流量统计

int maxFPS = 25;

if (this.curFPS % (maxFPS Convert.ToDouble(_ConfigEntity.ImageOperate.GrabFrame)) 1)

{

if (!detectArea.IsEmpty)

{

byte[] bytes = ImageHelper.CaptureImage(imageCV.ToJpegData(), detectArea.X, detectArea.Y, detectArea.Width, detectArea.Height - detectArea.Y);

ThreadPool.QueueUserWorkItem(new WaitCallback(BodyNumLookup), bytes);

}

}

using (Graphics g = Graphics.FromImage(image))

{

设置检测区

if (detectArea.IsEmpty)

{

detectArea = new Rectangle(0, (int)(image.Height 0.4), image.Width, (int)(image.Height 0.6));

}

try

{

进入检测线

g.DrawLine(new Pen(Color.FromArgb(100, Color.Lime), 2), detectArea.X, detectArea.Y, detectArea.Width, detectArea.Y);

g.DrawString(进入检测线 ↓,

new Font(微软雅黑, 9, FontStyle.Regular),

new SolidBrush(Color.FromArgb(150, Color.Lime)),

10, detectArea.Y - 20);

离开检测线

g.DrawLine(new Pen(Color.FromArgb(100, Color.Red), 2),

0, detectArea.Height, detectArea.Width, detectArea.Height);

g.DrawString(离开检测线 ↑,

new Font(微软雅黑, 9, FontStyle.Regular),

new SolidBrush(Color.FromArgb(150, Color.Red)),

10, detectArea.Height + 5);

显示统计人数

g.DrawString(区域: + areaNum.ToString() + 人,

new Font(微软雅黑, 9, FontStyle.Regular),

new SolidBrush(Color.FromArgb(150, Color.White)),

10, 20);

g.DrawString(进入: + inNum.ToString() + 人,

new Font(微软雅黑, 9, FontStyle.Regular),

new SolidBrush(Color.FromArgb(150, Color.White)),

10, 45);

g.DrawString(离开: + outNum.ToString() + 人,

new Font(微软雅黑, 9, FontStyle.Regular),

new SolidBrush(Color.FromArgb(150, Color.White)),

10, 70);

}

catch { }

}

if (this.curFPS == maxFPS)

this.curFPS = 1;

else

this.curFPS++;

}

private void BodyNumLookup(object parameter)

{

byte[] sJpegPicBuffer = parameter as byte[];

string image = Convert.ToBase64String(sJpegPicBuffer).Replace(n, n).Replace(+, %2B);

try

{

string url = bodyNumUrl + access_token= + this.access_token;

string paras = string.Empty;

paras += image= + image ;

HttpWebResponse response = HttpWebResponseUtility.CreatePostHttpResponse(url, paras, encode, contentType);

string content = HttpWebResponseUtility.GetResponesContent(response);

BodyNum bodyNum = JsonConvert.DeserializeObjectBodyNum(content);

this.Invoke(new MethodInvoker(() =

{

textBox1.Text = bodyNum.person_num.ToString();

}));

}

catch { }

}

private string GetToken(string client_id, string client_secret)

{

try

{

string paras = string.Empty;

string grant_type = client_credentials;

paras += grant_type= + grant_type + &;

paras += client_id= + client_id + &;

paras += client_secret= + client_secret;

HttpWebResponse response = HttpWebResponseUtility.CreatePostHttpResponse(authHost, paras, encode, contentType);

string content = HttpWebResponseUtility.GetResponesContent(response);

OauthInfo oauthInfo = JsonConvert.DeserializeObjectOauthInfo(content);

return oauthInfo.access_token;

}

catch { }

return ;

}

private void simpleButton_ClearCount_Click(object sender, EventArgs e)

{

if (this.areaNum 0)

{

DialogResult dr = FlyoutDialog.Show(this, rn区域存在人数,确定要清除吗?rn , MessageBoxButtons.OKCancel);

if (dr == DialogResult.Cancel) return;

}

this.areaNum = 0;

this.inNum = 0;

this.outNum = 0;

}

#region 关闭窗体

private void simpleButton_Exit_Click(object sender, EventArgs e)

{

this.Close();

}

private void FormPassengerFlow_FormClosing(object sender, FormClosingEventArgs e)

{

if (videoSourcePlayer1.IsRunning)

{

videoSourcePlayer1.SignalToStop();

videoSourcePlayer1.WaitForStop();

}

this.isFormClose = true;

}

#endregion

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值