FaceDetectUtil
public class FaceDetectUtil {
/**
* 字符转整数
* @param attribute
* @return
*/
public int ToInt(String attribute) {
if(attribute==null || attribute.length()==0)
{
return -1;
}
try
{
return Integer.parseInt(attribute);
}
catch(Exception evp)
{
evp.printStackTrace();
}
return 0;
}
public class FaceDetectInfo
{
/**
* 人脸在1920*1080中的坐标 left
*/
public int x=0;
/**
* 人脸在1920*1080中的坐标 top
*/
public int y=0;
/**
* 人脸在1920*1080中的宽
*/
public int w=0;
/**
* 人脸在1920*1080中的高
*/
public int h=0;
/**
* 人脸标识跟踪ID
*/
public int trackid=0;
}
/**
* 上一次的人脸数量
*/
public int mLastHaveFace=0;
/**
* 返回-1表示失败 0表示无变化,1表示从无人脸到有人脸,2表示从有人脸到无人脸,3表示前面都有人脸,但人脸数变化了。
*
* @param param
* @param len
* @param mFaceList 解释完后 mFaceList为当前人脸列表记录,如果为空,则不解释人脸坐标数据
* @return
*/
public int ParseFaceDetectXml(byte[] param, int len,ArrayList<FaceDetectInfo> mFaceList) {
if(param==null)
{
return -1;
}
int nRetVal=-2;
String xml=new String(param);
try
{
//Log.i("facechange","recv face status="+ xml);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new ByteArrayInputStream(xml.getBytes()));
//Element rootElement = document.getDocumentElement();
NodeList facels = document.getElementsByTagName("Track");//人脸识别结果
if (facels == null || facels.getLength() == 0) {
if(mLastHaveFace>0)
{//这里是从有人脸到无人脸变化 @2019
Log.i("facechange", "facechange 从有人脸到无人脸变化");
nRetVal=2;
mLastHaveFace=0;
return 2;
}
}
if(facels.getLength()>0 && mLastHaveFace==0)
{//这里是从没有到人脸 @2019
Log.i("facechange", "facechange 从无人脸到有人脸变化");
nRetVal=1;
}
else if(mLastHaveFace>0 && facels.getLength()==0)
{//这里是从有人脸到无人脸变化 @2019
Log.i("facechange", "facechange 从有人脸到无人脸变化");
nRetVal=2;
}
else if(mLastHaveFace!=facels.getLength())
{
Log.i("facechange", "facechange 人脸数发生变化");
nRetVal=3;
}
else
{
nRetVal=0;
}
mLastHaveFace=facels.getLength();
if(mFaceList!=null)
{
mFaceList.clear();
for(int idx=0;idx<mLastHaveFace;idx++)
{
Element node = (Element) facels.item(idx);
FaceDetectInfo item=new FaceDetectInfo();
item.x=ToInt(node.getAttribute("X"));
item.y=ToInt(node.getAttribute("Y"));
item.w=ToInt(node.getAttribute("W"));
item.h=ToInt(node.getAttribute("H"));
item.trackid=ToInt(node.getAttribute("Id"));
mFaceList.add(item);
}
/*
<Tracks Count="5" Time="2019-08-23 09:55:21">
<Track Id="123" X="0" Y="0" W="64" H="64"/>
<Track Id="456" X="100" Y="100" W="64" H="64"/>
<Track Id="789" X="200" Y="200" W="64" H="64"/>
<Track Id="124" X="300" Y="300" W="64" H="64"/>
<Track Id="457" X="400" Y="400" W="64" H="64"/>
</Tracks>
*/
}
}catch(Exception evp)
{
evp.printStackTrace();
}
return nRetVal;
}
/**
* 将原始坐标 1920 *1080 转抱成指定的 640 * 360 坐标
* @param mFaceList
* @param w
* @param h
* @return
*/
public ArrayList<FaceDetectInfo> ChangePos(ArrayList<FaceDetectInfo> mFaceList)
{
return ChangePos(mFaceList,640,360);
}
/**
* 将原始坐标 1920 *1080 转抱成指定的 w * h坐标
* @param mFaceList
* @param w
* @param h
* @return
*/
public ArrayList<FaceDetectInfo> ChangePos(ArrayList<FaceDetectInfo> mFaceList,int w,int h) {
ArrayList<FaceDetectInfo> ret=new ArrayList<FaceDetectInfo>();
int nLen=mFaceList.size();
for(int idx=0;idx<nLen;idx++)
{
FaceDetectInfo item=new FaceDetectInfo();
FaceDetectInfo olditem=mFaceList.get(idx);
item.x=olditem.x*w/1920;
item.y=olditem.y*h/1080;
item.w=olditem.w*w/1920;
item.h=olditem.h*h/1080;
item.trackid=item.trackid;
ret.add(item);
}
return ret;
}
}












网友评论