faceppclient.init.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. define("DEBUG_MODE", false);
  3. class faceppclientInit {
  4. private $api_server_url;
  5. private $auth_params;
  6. public function __construct() {
  7. $this->api_server_url = "http://apitest.cn.faceplusplus.com/";
  8. $this->auth_params = array();
  9. $this->auth_params['api_key'] = FACEKEY;
  10. $this->auth_params['api_secret'] = FACESECRET;
  11. }
  12. /**
  13. * 检测一张照片中的人脸信息(脸部位置、年龄、种族、性别等等)[通过URL提供图片 ]
  14. * @param string $urls 待检测图片的URL
  15. * @return Ambigous <NULL, mixed>
  16. */
  17. public function face_detect($urls = null) {
  18. return $this->post_call("detection/detect", array(
  19. "url" => $urls
  20. ));
  21. }
  22. /**
  23. * 检测给定人脸相应的面部轮廓,五官等关键点的位置
  24. * @param string $face_id
  25. * @return Ambigous <NULL, mixed>
  26. */
  27. public function face_landmark($face_id = null) {
  28. return $this->post_call("detection/landmark", array(
  29. "face_id" => $face_id
  30. ));
  31. }
  32. /**
  33. * 检测一张照片中的人脸信息(脸部位置、年龄、种族、性别等等)[通过POST方法上传二进制数据]
  34. * @param string $urls
  35. * @return Ambigous <NULL, mixed>
  36. */
  37. public function face_detect_post($filename) {
  38. return $this->post_call("detection/detect", array(
  39. "img" => '@' . $filename
  40. ));
  41. }
  42. /**
  43. * 检测一张照片中的人脸信息(脸部位置、年龄、种族、性别等等)【通过URL提供图片】
  44. * @param string $filename URL
  45. * @param string $offline_result 默认脸区域(四个点)
  46. * @return Ambigous <NULL, mixed>
  47. */
  48. public function face_detect_post_final($filename,$attribute="gender,age,race,smiling,glass,pose,landmark83p",$offline_result="[[72,35,172,135]]") {
  49. return $this->post_call("detection/detect", array(
  50. 'url' => $filename,
  51. 'attribute' => $attribute,//可以是none或者由逗号分割的属性列表。默认为gender, age, race, smiling。目前支持的属性包括:gender, age, race, smiling, glass, pose
  52. 'mode' => 'offline',//检测模式可以是normal(默认) 或者 oneface 。在oneface模式中,检测器仅找出图片中最大的一张脸。
  53. 'offline_result' => $offline_result,
  54. ));
  55. }
  56. // ////////////////////////////////////////////////////////
  57. // private mathods
  58. // ////////////////////////////////////////////////////////
  59. protected function post_call($method, $params = array()) {
  60. $params = array_merge($this->auth_params, $params);
  61. $url = $this->api_server_url . "$method";
  62. if (DEBUG_MODE) {
  63. echo "REQUEST: $url?" . http_build_query($params) . "\n";
  64. }
  65. $ch = curl_init();
  66. curl_setopt($ch, CURLOPT_URL, $url);
  67. curl_setopt($ch, CURLOPT_POST, 1);
  68. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  69. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  70. $data = curl_exec($ch);
  71. curl_close($ch);
  72. $result = null;
  73. if (! empty($data)) {
  74. if (DEBUG_MODE) {
  75. echo "RETURN: " . $data . "\n";
  76. }
  77. $result = json_decode($data);
  78. }
  79. return $result;
  80. }
  81. }
  82. ?>