unit Demos.ImageModeration.Forms.Main;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtDlgs, System.ImageList,
  Vcl.ImgList, System.Actions, Vcl.ActnList, Vcl.ComCtrls, Vcl.ToolWin,
  Vcl.ExtCtrls, Vcl.Imaging.JPEG,
  Azure.CognitiveServices.Core.Entities,
  Azure.CognitiveServices.ImageModeration.Types, Vcl.Imaging.pngimage,
  Vcl.StdCtrls;

type

{ TMainForm }

  TMainForm = class(TForm)
    MainToolbar: TToolBar;
    LoadPhotoButton: TToolButton;
    FindFaceButton: TToolButton;
    ClearDataButton: TToolButton;
    AppExitButton: TToolButton;
    MainActionList: TActionList;
    LoadPhotoAction: TAction;
    ModeratePhotoAction: TAction;
    ClearDataAction: TAction;
    AppExitAction: TAction;
    MainImageList: TImageList;
    PhotoOpenDialog: TOpenPictureDialog;
    PhotoScrollBox: TScrollBox;
    PhotoImage: TImage;
    ResponsePanel: TPanel;
    ResultAllowImage: TImage;
    ResultDenyImage: TImage;
    RatingPanel: TGridPanel;
    AdultImage: TImage;
    AdultProgressBar: TProgressBar;
    AdultLabel: TLabel;
    RacyImage: TImage;
    RacyProgressBar: TProgressBar;
    RacyLabel: TLabel;
    procedure LoadPhotoActionExecute(Sender: TObject);
    procedure ModeratePhotoActionExecute(Sender: TObject);
    procedure ClearDataActionExecute(Sender: TObject);
    procedure AppExitActionExecute(Sender: TObject);
    procedure MainActionListUpdate(Action: TBasicAction; var Handled: Boolean);
  private
    FHasResponse: Boolean;
    FResponseData: TAcsImageModerationResponse;
    FPhotoData: TBytes;
    procedure ShowResponse;
  public
    constructor Create(AOwner: TComponent); override;
  end;

var
  MainForm: TMainForm;

implementation

{$R *.dfm}

uses
  System.IOUtils, System.Math,
  Azure.CognitiveServices.ImageModeration.HTTP;

{ TMainForm }

constructor TMainForm.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  ShowResponse();
end;

procedure TMainForm.AppExitActionExecute(Sender: TObject);
begin
  Close();
end;

procedure TMainForm.ClearDataActionExecute(Sender: TObject);
begin
  FHasResponse := False;
  ShowResponse();
end;

procedure TMainForm.LoadPhotoActionExecute(Sender: TObject);
begin
  if not PhotoOpenDialog.Execute then
    Exit;

  FPhotoData := TFile.ReadAllBytes(PhotoOpenDialog.FileName);

  var LStream := TMemoryStream.Create;
  try
    LStream.Write(FPhotoData, Length(FPhotoData));
    LStream.Position := 0;
    PhotoImage.Picture.LoadFromStream(LStream);
  finally
    LStream.Free;
  end;

  PhotoImage.Left := 0;
  PhotoImage.Top := 0;
  PhotoImage.AutoSize := True;

  ClearDataAction.Execute;
end;

procedure TMainForm.MainActionListUpdate(Action: TBasicAction;
  var Handled: Boolean);
begin
  ClearDataAction.Enabled := FHasResponse;
  ModeratePhotoAction.Enabled := Length(FPhotoData) > 0;
  Handled := True;
end;

procedure TMainForm.ModeratePhotoActionExecute(Sender: TObject);
var
  LConfig: IAcsConfiguration;
  LBroker: TAcsImageModerationHttpBroker;
  LRequest: TAcsImageModerationRequest;
begin
  LConfig := LoadAcsConfiguration();

  LBroker := TAcsImageModerationHttpBroker.Create(LConfig);
  try
    LBroker.Endpoint := 'https://???.cognitiveservices.azure.com/contentmoderator/moderate/v1.0/ProcessImage/Evaluate';
    with LRequest do
    begin
      CacheImage := False;
      ImageData := FPhotoData;
      ImageType := 'image/jpeg';
    end;
    FResponseData := LBroker.Execute(LRequest);
    FHasResponse := True;
  finally
    LBroker.Free;
  end;

  // Aggiorna la visualizzazione degli esiti di moderazione.
  ShowResponse();
end;

procedure TMainForm.ShowResponse;
begin
  // Verifica che sia stato ricevuto un responso da mostrare.
  if not FHasResponse then
  begin
    ResponsePanel.Hide;
    Exit;
  end;

  // Visualizza i valori per il criterio "soli adulti".
  AdultLabel.Caption := Format('%.2f', [FResponseData.AdultClassificationScore]);
  if FResponseData.IsImageAdultClassified then
    AdultLabel.Font.Color := clRed
  else
    AdultLabel.Font.Color := clGreen;
  AdultProgressBar.Position := Trunc(SimpleRoundTo(FResponseData.AdultClassificationScore * 100, 0));

  // Visualizza i valori per il criterio "compromettente".
  RacyLabel.Caption := Format('%.2f', [FResponseData.RacyClassificationScore]);
  if FResponseData.IsImageRacyClassified then
    RacyLabel.Font.Color := clRed
  else
    RacyLabel.Font.Color := clGreen;
  RacyProgressBar.Position := Trunc(SimpleRoundTo(FResponseData.RacyClassificationScore * 100, 0));

  // Visualizza il responso finale di accettabilità.
  ResultDenyImage.Visible := FResponseData.Result;
  ResultAllowImage.Visible := not ResultDenyImage.Visible;

  // Rende visibile il pannello del responso.
  ResponsePanel.Show;
end;

end.
