美文网首页
2021-10-13 人脸识别 通用类

2021-10-13 人脸识别 通用类

作者: fjasmin | 来源:发表于2021-10-13 21:24 被阅读0次

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;
    }
}

相关文章

  • 2021-10-13 人脸识别 通用类

    FaceDetectUtil

  • 安装人脸识别库dlib简易方法

    Dlib是一个使用现代C++技术编写的跨平台的通用库,库中含有整套的人脸识别框架,包括人脸检测、人脸对齐、人脸比对...

  • MTCNN

      人脸任务总体上分为:人脸检测、人脸关键点检测、人脸判别、人脸识别、人脸聚类等。  作者认为人脸检测和人脸关键点...

  • 人脸识别技术 通识

    人脸识别技术 通识 1 人脸识别概要 2 人脸识别工作原理 3 人脸识别技术分类 4 人脸识别技术优缺点 5 人脸...

  • avcapture

    iOS人脸识别、自定义相机、图像扫描、系统自带二维码识别基于 OpenCV 的人脸识别 视频捕获 用到的类,主要有...

  • 【计算机视觉】OpenCV人脸识别facerec源码分析2——L

    人脸识别 从OpenCV2.4开始,加入了新的类FaceRecognizer,我们可以使用它便捷地进行人脸识别实验...

  • 你是我的眼--计算机视觉(CV)的应用

    计算机视觉识别的方向主要包括人脸识别,文字识别,图像识别,通用物品识别等。 看起来高大上的技术,其实在现实生活中,...

  • 都在说人脸识别有Bug,是真的吗?

    “人脸识别破解”“反人脸识别技术”“人脸识别漏洞”“人脸识别失误”“尽量不要用人脸识别”….网上N多这样的信息铺天...

  • opencv的人脸识别

    opencv2.4提出了一个新的关于人脸识别的类FaceRecognizer,可以用它进行人脸识别实验。当前主要的...

  • 2020-06-09

    云卡通人脸识别系统含盖:动态人脸识别门禁管理系统、动态人脸识别消费系统、动态人脸识别考勤系统。其中动态人脸识别消费...

网友评论

      本文标题:2021-10-13 人脸识别 通用类

      本文链接:https://www.haomeiwen.com/subject/bsbeoltx.html