📜 ⬆️ ⬇️

How I tried Microsoft Project Oxford + Telegram Bot API

I think, as the name implies, it will be about such things as Microsoft Project Oxford and the Telegram API.

What is Microsoft Project Oxford is a set of ready-made REST APIs that in an accessible form give developers the full power of machine vision, natural language analysis and voice recognition algorithms for use in their applications. It is worth noting that the availability of services in the form of a REST API allows you to use it on absolutely any platform and using your favorite development technologies, not limited to those proposed by Microsoft. In more detail - here or here .

Telegram Bot API - ( I think, and so many people know ) who do not know go here .
')
The first thing that occurred to me was the easiest to make a bot, which determines the age and gender of the person in the photo. To do this, we need the Face API and the API key, all of which can be obtained from the official website .

So, let's start (to talk about how to create a bot, I will not, because a lot of information about this). After we registered the bot, webhooks and got the API key, we break the spelling.

I decided to write in PHP. Why? devil lured

1) Receive a message from the user:

require_once '/home/edalqrmq/php/HTTP/Request.php'; define('BOT_TOKEN', 'Token'); define('API_URL', 'https://api.telegram.org/bot'.BOT_TOKEN.'/'); define('FILE_URL', 'https://api.telegram.org/file/bot'.BOT_TOKEN.'/');//     $content = file_get_contents("php://input"); $update = json_decode($content, true); $chatID = $update["message"]["chat"]["id"]; 

Something like this JSON:

 { "update_id":3405704853232, _"message":{ "message_id":238, "from":{ "id":138183417332, "first_name":"Kirill", "last_name":"Dudka" }, "chat":{ "id":1381834173, "first_name":"Kirill", "last_name":"Dudka", "type":"private" }, "date":1461168940323, "photo":[ { "file_id":"AgADAgADw6gxG_mCPAjHE7knq2P_UUJfLyLw4AAgI", "file_size":1211, "width":90, "height":67 }, { "file_id":"AgADAgADw6gxG_mCPAjHE7knDjERzAUtSA0ABGwI", "file_size":16846, "width":320, "height":240 }, { "file_id":"AgADAgADw6gxG_mCPAjHE7knBB_ZFZmQI", "file_size":55547, "width":800, "height":600 } ] } } 


2) Check for the presence of a photo and form a link to the image:

 if($update["message"]["photo"]){ $photoId = $update["message"]["photo"]["2"]["file_id"]; $url_json = file_get_contents(API_URL."getFile?file_id=".$photoId); $massURL = json_decode($url_json, true); $photoURL = FILE_URL.$massURL["result"]["file_path"]; } 

3) Next, we need to send the image for analysis, for more details, see here

 $req = &new HTTP_Request('https://api.projectoxford.ai/face/v1.0/detect'); $req->setMethod(HTTP_REQUEST_METHOD_POST); $req->addHeader('Content-Type','application/json'); $req->addHeader('Ocp-Apim-Subscription-Key','key'); $req->addQueryString('returnFaceId', 'true'); $req->addQueryString('returnFaceLandmarks', 'false'); $req->addQueryString('returnFaceAttributes', 'age,gender,smile');//     $req->setBody('{"url":"'.$photoURL.'"}'); 

In response, we receive something like this:

 Pragma: no-cache Cache-Control: no-cache Date: Wed, 20 Apr 2016 18:31:59 GMT X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Content-Length: 143 Content-Type: application/json; charset=utf-8 Expires: -1 [ { "faceId": "a194a7b3-7e86-46fc-9b73-166e09127546581b", "faceRectangle": { "top": 80, "left": 24, "width": 162, "height": 162 }, "faceAttributes": { "age": 23.3 } } ] 


4) Next thing is small, send the data to the user:

 try { $req->sendRequest(); $face_json = $req->getResponseBody(); $massFace = json_decode($face_json, true); $age = $massFace["0"]["faceAttributes"]["age"]; $gender_En = $massFace["0"]["faceAttributes"]["gender"]; if(!$massFace["0"]["faceAttributes"]){ $sendto = API_URL."sendmessage?chat_id=".$chatID."&text= ,    "; } else{ if($gender_En=="male"){ $gender = ""; } else{ if($gender_En=="female"){ $gender = ""; } } $sendto = API_URL."sendmessage?chat_id=".$chatID."&text= : ".$gender." : ".$age; } } catch (HttpException $ex) { echo $ex; } } else { $sendto = API_URL."sendmessage?chat_id=".$chatID."&text=  "; } file_get_contents($sendto); 

Actually, this is all, in the end we got this picture:

image

Total left an hour and some coffee. The bot displays information only about the first person found, did not set itself the task of writing a bot for the prod ( for now ), Microsoft Project Oxford is quite an interesting API that deserves attention.

Thanks to all.

Next We determine gender and age using Microsoft Project Oxford and C #
May your neurons be with you!

Source: https://habr.com/ru/post/282125/


All Articles