通過查看其API文檔,入參除了key和secret(注冊賬號后申請獲得)外,只有模版圖、模版圖中人臉位置、用戶上傳圖,三個參數。Face++的人臉檢測API可在線獲取模版圖中人臉位置,例如:251,167,169,169。依次代表人臉框左上角縱坐標(top),左上角橫坐標(left),人臉框寬度(width),人臉框高度(height),如下圖:
然后,直接調用人臉融合接口即可,返回融合后的圖片base64編碼數據,再根據需求直接賦值給img標簽顯示。
關鍵代碼如下:
var postData = 'api_key=xxx&api_secret=xxx' + '&template_url=http://xxx.com/xxx.jpg' + '&template_rectangle=251,167,169,169' + '&merge_url=http://xxx.com/xxx.jpg'; $.ajax({ dataType: 'json', type: 'POST' , url: 'https://api-cn.faceplusplus.com/imagepp/v1/mergeface', data: postData, success: function(response){ if(typeof(response.error_message) == "undefined"){ // todo: 在這里添加生成后的邏輯,response.result 為生成圖的base64編碼 $('.uploadpic').attr('src', 'data:image/jpg/png;base64,' + response.result); }else{ // todo: 在這里添加上傳失敗的邏輯 alert('請重新上傳照片'); } }, error: function(xhr, status, error){ console.log(xhr.responseText); // todo: 在這里添加上傳失敗的邏輯 alert('請重新上傳照片'); } });
BTW
1、注意融合比例參數merge_rate,數字越大融合結果包含越多融合圖特征,默認為50。這里可設為100,融合特征會非常明顯;
2、接口規定模版為JPG格式,所以無法直接實現png圖的融合。騰訊AI的人臉融合接口是可以用png模版的;
3、試用賬號是不保證並發的,如果要保證並發可以查看官方的價格。
轉自:https://blog.csdn.net/gaofei880219/article/details/80805558