thinkphp3.2:Face++人工智能開放平臺;笑臉檢測

最近接觸一個新的項目,其中有一個功能是人臉識別情緒、笑臉識別

用的face++平臺的一個接口:https://api-cn.faceplusplus.com/facepp/v3/detect

簡單擼一下

請求參數(必選)更多參數請移步https://console.faceplusplus.com.cn/documents/4888373;比如 return_attributes:是否檢測並返回根據人臉特徵判斷出的年齡、性別、情緒等屬性。

是否必選

參數名

類型

參數說明

必選

api_key

String

調用此API的API Key

必選

api_secret

String

調用此API的API Secret

必選(三選一)

image_url

String

圖片的 URL。

注:在下載圖片時可能由於網絡等原因導致下載圖片時間過長,建議使用 image_file 或 image_base64 參數直接上傳圖片。

image_file

File

一個圖片,二進制文件,需要用post multipart/form-data的方式上傳。

image_base64 String

base64 編碼的二進制圖片數據

如果同時傳入了 image_url、image_file 和 image_base64 參數,本API使用順序爲 image_file 優先,image_url 最低。

php端control代碼:

/**
     * face++接口測試
     */
    public function test(){
        if(IS_POST){
            if($_FILES['img']['tmp_name']!=''){
                $upload = new \Think\Upload();// 實例化上傳類
                $upload->maxSize   =     3145728 ;// 設置附件上傳大小
                $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 設置附件上傳類型
                $upload->rootPath  =      './'; // 設置附件上傳目錄
                $upload->savePath  =      '/Public/Uploads/'; // 設置附件上傳(子)目錄
                // 上傳單個文件
                $info   =   $upload->uploadOne($_FILES['img']);
                if(!$info) {// 上傳錯誤提示錯誤信息
                    $this->error($upload->getError());
                }else{// 上傳成功 獲取上傳文件信息
                    $data['img'] = $info['savepath'].$info['savename'];
                }
            }
            $url = 'https://api-cn.faceplusplus.com/facepp/v3/detect';
            $dd = array(
                'api_key' => "XXXXXXXXXXXXXXX",
                'api_secret'=> "XXXXXXXXXXXXXXXXXXXXxx",
                'image_url'=>'http://dove.io/'.$data['img'], //圖片的 URL。
                'return_attributes'=>'gender,age,smiling,emotion,facequality,ethnicity,beauty,skinstatus',

            );
//
            $result = $this->https_curl_json($url,$dd,'Nojson');
            echo "<pre>";
            print_r($result);
        }else{
            $this->display();
        }

    }
    /* 發送json格式的數據,到api接口 */
    function https_curl_json($url,$data,$type){
        if($type=='json'){
            $headers = array("Content-type: application/json;charset=UTF-8","Accept: application/json","Cache-Control: no-cache", "Pragma: no-cache");
            $data=json_encode($data);
        }
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, 1); // 發送一個常規的Post請求
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers );
        $output = curl_exec($curl);
        if (curl_errno($curl)) {
            echo 'Errno'.curl_error($curl);//捕抓異常
        }
        curl_close($curl);
        return $output;
    }

php端html代碼

 <form method="post" action="" class="form-horizontal" enctype="multipart/form-data"  >
                        <!--<input type="hidden" name="id" value="1">-->
                        <div class="form-group img">
                            <label class="col-sm-2 control-label">上傳圖片</label>
                            <div class="col-sm-10 back-change">
                                <div id="file-pretty" >
                                    <input type="file" value=" " class="form-control" name="img" multiple="multiple">
                                </div>
                            </div>
                        </div>


                        <div class="form-group">
                            <div class="col-sm-4 col-sm-offset-2">
                                <button class="btn btn-primary" type="submit">保存內容</button>
                                <button class="btn btn-white" type="button" οnclick="javascript:history.back(-1);">返回</button>
                            </div>
                        </div>
                    </form>

效果

例圖

上傳

打印輸出

{"request_id":"1584702273,32ea89c8-fe9d-4490-882e-f626cad4a570","time_used":629,"faces":[{"face_token":"fdd5cc1a79f766a75fe6c31f53b92adf","face_rectangle":{"top":179,"left":163,"width":246,"height":246},"attributes":{"gender":{"value":"Female"},"age":{"value":27},"smile":{"value":99.997,"threshold":50.000},"emotion":{"anger":0.013,"disgust":0.006,"fear":5.631,"happiness":93.449,"neutral":0.885,"sadness":0.009,"surprise":0.006},"facequality":{"value":91.966,"threshold":70.100},"ethnicity":{"value":""},"beauty":{"male_score":68.024,"female_score":73.253},"skinstatus":{"health":26.727,"stain":12.353,"dark_circle":8.756,"acne":1.058}}}],"image_id":"Lu7EMtu0gyGyCZyV/leFlg==","face_num":1}

 調用成功!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章