欢迎来到天天文库
浏览记录
ID:39467262
大小:61.50 KB
页数:4页
时间:2019-07-04
《php开发人脸识别的api接口》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
php开发人脸识别的api接口作者: 来源:互联网 日期:2013-11-1416:20:25 收藏 评论:( 0 ) 阅读:1982在网上发现一个PHP编写的人脸识别的API,试了一下还不错,这里推荐给大家,有兴趣的可以研究研究。首先,检测出三张输入图片中的Face。然后,使用这三个Face各自创建1个新的Person。这三个Person将构建一个新的Group以作为候选的识别库。最后,输入一张新Image,并在已构建的Group中识别这个新Image中的Face。创建新的Person待识别图片首先需要经过初步检测,找出图片中Face的位置及属性。然后,再使用API中的Person管理创建新的Person。1234567891011121314151617181920//thelistofperson_nametotrainandidentifyfor$person_names = array("1", "2", "3");//storetheface_idsobtainedbydetection/detectAPI$face_ids = array();//registernewpeople,detectfacesforeach ($person_names as $person_name) detect($api, $person_name, $face_ids); /* * createnewperson,detectfacesfromperson'simage_url */function detect(&$api, $person_name,&$face_ids){ //obtainphoto_urltotrain $url =getTrainingUrl($person_name); //detectfacesinthisphoto $result = $api->face_detect($url); //skiperrors if (empty($result->face)) return false; //skipphotowithmultiplefaces(wearenotsurewhichfacetotrain) if (count($result->face)>1) return false; //obtaintheface_id $face_id = $result->face[0]->face_id; $face_ids[]= $face_id; //deletethepersonifexists 212223242526272829303132333435 $api->person_delete($person_name); //createanewpersonforthisface $api->person_create($person_name); //addfaceintonewperson $api->person_add_face($face_id, $person_name);}构建新的Group注册一个新的Group,并将之前创建的Person加入这个Group。1234567891011//thenameofgroupfortesting$group = "sample_group";//generateanewgroup,addpeopleintogroupcreate_group($api, $group, $person_names); /* * generateanewgroupwithgroup_name,addallpeopleintogroup */function create_group(&$api, $group_name, $person_names){ 12131415161718 //deletethegroupifexists $api->group_delete($group_name); //createnewgroup $api->group_create($group_name); //addnewpersonintothegroup foreach ($person_names as $person_name) $api->group_add_person($person_name, $group_name);}训练识别模型对刚创建的group,调用/train/identify来训练识别特征以待识别。(注意:/train/identify是一个异步方法,因此在调用train函数后我们需要等待训练完毕才能继续执行后续代码。)1234567891011121314151617181920212223242526272829//generatetrainingmodelforgrouptrain($api, $group); /* * trainidentificationmodelforgroup */function train(&$api, $group_name){ //trainmodel $session = $api->train_identify($group_name); if (empty($session->session_id)) { //somethingwentwrong,skip return false; } $session_id = $session->session_id; //waituntiltrainingprocessdone while ($session=$api->info_get_session($session_id)) { sleep(1); if (!empty($session->status)){ if ($session->status!= "INQUEUE") break; } } //done return true;}识别新图中的Face在对新图中的Face进行以上提及的检测,注册和训练之后,我们就可以在已构建Group中识别新图中出现的Face了:123456789101112131415161718192021222324252627282930//finally,identifypeopleinthegroupidentify($api, $person_names[0], $group); /* * identifyapersoningroup */function identify(&$api, $person_name, $group_name){ //obtainphoto_urltoidentify $url =getPhotoUrl($person_name); //recoginzation $result = $api->recognition_identify($url, $group_name); //skiperrors if (empty($result->face)) return false; //skipphotowithmultiplefaces if (count($result->face)>1) return false; $face = $result->face[0]; //skipifnopersonreturned if (count($face->candidate)<1) return false; //printresult foreach ($face->candidate as $candidate) echo "$candidate->person_namewasfoundin$group_namewith". "confidence$candidate->confidence ";}完整PHP代码可于此处下载(需要修改api_key,api_secret,getTrainingUrl,getPhotoUrl后才能正常运行)
此文档下载收益归作者所有
举报原因
联系方式
详细说明
内容无法转码请点击此处