Skip to content

Instantly share code, notes, and snippets.

@shuheilocale
Last active April 12, 2016 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shuheilocale/23c949242511c7646cb2287eb903801f to your computer and use it in GitHub Desktop.
Save shuheilocale/23c949242511c7646cb2287eb903801f to your computer and use it in GitHub Desktop.
【LINE Bot】 bluemixとFaceAPIを使った年齢性別推定Bot ref: http://qiita.com/shuheilocale@github/items/da6ecdeced3c00acc6f8
// 画像データを取得する
function api_get_message_content_request($message_id) {
$url = "https://trialbot-api.line.me/v1/bot/message/{$message_id}/content";
$headers = array(
"X-Line-ChannelID: {$GLOBALS['CHANNEL_ID']}",
"X-Line-ChannelSecret: {$GLOBALS['CHANNEL_SECRET']}",
"X-Line-Trusted-User-With-ACL: {$GLOBALS['MID']}"
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
error_log("res header::".$header);
error_log("res body::".$body);
return array( "header"=>$header, "body"=>$body);
}
// (メンバ関数)バイナリデータから顔認証を行う
public function analyze_from_binary($img_binary) {
$headers = array(
"Content-Type" => "application/octet-stream"
);
$request = new Http_Request2("https://api.projectoxford.ai/face/v0/detections");
$request->setConfig(array(
"ssl_verify_peer" => false,
));
$request->setMethod(HTTP_Request2::METHOD_POST);
$request->setHeader($headers);
$url = $request->getUrl();
$url->setQueryVariables($this->query_params);
if( empty($img_binary)){
return false;
}
$request->setBody($img_binary);
try
{
$response = $request->send();
$status = $response->getStatus();
$body_json = json_decode($response->getBody());
error_log( "resp status::".$response->getStatus() );
error_log( "resp body::".$response->getBody() );
if($status != 200){
if( $status == 400 || $status == 408 || $status == 415 ){
return array (
"result" => false,
"code" => $body_json->{"code"},
"message" => $body_json->{"message"}
) ;
}
else if( $status == 401 || $status == 403 || $status == 429 ){
return array (
"result" => false,
"code" => $body_json->{"statusCode"},
"message" => $body_json->{"message"}
) ;
}else{
return array (
"result" => false,
"code" => "unknown",
"message" => "unknown error"
) ;
}
}
return array (
"result" => true,
"faces" => $body_json
) ;
}
catch (HttpException $ex)
{
throw $ex;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment