Jump to content


Photo

Emotion API Project Oxford

machine learning emotions

  • Please log in to reply
No replies to this topic

#1 Nico Wouterse

Nico Wouterse
  • Moderators
  • 227 posts
  • LocationAustralia

Posted 08 March 2016 - 07:33 AM

Project Oxford from Microsoft exports some machine learning environments for anyone to use

freemium up to a point

 

This post is about the "Emotion" service which lets one scan images.

The service is able to determine faces and what's more determine the emotions displayed on them.

 

The API gives a score on anger, contempt, disgust, fear, happiness, neutral, sadness and surprise

 

Try-out code below

unit Form1;
 
interface
 
uses
  SmartCL.System, SmartCL.Graphics, SmartCL.Components, SmartCL.Forms,
  SmartCL.Fonts, SmartCL.Borders, SmartCL.Application, SmartCL.Inet,
  SmartCL.Controls.Button, system.types, SmartCL.Controls.EditBox,
  SmartCL.Controls.Memo, ECMA.JSON, SmartCL.Controls.Image;
 
type
  TForm1 = class(TW3Form)
    procedure W3Button1Click(Sender: TObject);
  private
    {$I 'Form1:intf'}
  protected
    procedure InitializeForm; override;
    procedure InitializeObject; override;
    procedure Resize; override;
    procedure AskAssistant(imageurl, baseurl, subscriptionkey: string);
    procedure GetAnswer(Sender: TW3HttpRequest);
    FHTTP: Tw3HttpRequest;
    cursor: Variant;
  end;
 
implementation
 
{ TForm1 }
 
procedure TForm1.InitializeForm;
begin
  inherited;
  // this is a good place to initialize components
  W3Image1.LoadFromURL('https://oxfordportal.blob.core.windows.net/emotion/recognition4.jpg');
end;
 
procedure TForm1.InitializeObject;
begin
  inherited;
  {$I 'Form1:impl'}
end;
 
procedure TForm1.Resize;
begin
  inherited;
end;
 
procedure TForm1.AskAssistant(imageurl, baseURL, subscriptionkey: string);
var
  S: String;
begin
  FHttp := TW3HttpRequest.Create;
  FHttp.OnDataReady := GetAnswer;
  FHttp.open("POST",baseURL);
  FHttp.setRequestHeader("Content-Type","application/json");
  FHttp.setRequestHeader("Ocp-Apim-Subscription-Key",subscriptionkey);
 
  S := '{"url": "' + imageurl +'"}';
 
  FHttp.send(S);
end;
 
procedure TForm1.W3Button1Click(Sender: TObject);
var
  subscriptionKey = '<your subscription key>';
  baseUrl = 'https://api.projectoxford.ai/emotion/v1.0/recognize';
  imageurl = 'https://oxfordportal.blob.core.windows.net/emotion/recognition4.jpg';
begin
  AskAssistant(imageurl, baseURL, subscriptionKey);
end;
 
procedure TForm1.GetAnswer(Sender: TW3HttpRequest);
begin
//  showmessage(Sender.ResponseText);
  cursor := JSON.parse(Sender.ResponseText);
  for var i := 0 to cursor.length-1 do begin
   ShowMessage('Face ' + inttostr(i+1)            + #10 +
     ' anger     : ' + cursor[i].scores.anger     + #10 +
     ' contempt  : ' + cursor[i].scores.contempt  + #10 +
     ' disgust   : ' + cursor[i].scores.disgust   + #10 +
     ' fear      : ' + cursor[i].scores.fear      + #10 +
     ' happiness : ' + cursor[i].scores.happiness + #10 +
     ' neutral   : ' + cursor[i].scores.neutral   + #10 +
     ' sadness   : ' + cursor[i].scores.sadness   + #10 +
     ' surprise  : ' + cursor[i].scores.surprise);
  end;
end;
 
initialization
  Forms.RegisterForm({$I %FILE%}, TForm1);
end.

.
 

The image used is a hard-coded image from the Oxford website

Would be interesting to change this to the Mona Lisa

 

.


  • AlexissNag, Brandonshnitzelino and Darincroft like this
Nico Wouterse




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

IPB Skin By Virteq