Resource

Resource Basics

Resource Name AI Keywords for Descriptions
Resource Version 1.0
Category Catalog
Downloads 542
Release Date 2/18/2025 11:13:07 AM
 

Resource Details

Description This script will use AI to extract keywords from the Description (dc:description) metadata field and then assign those as AI labels to the image.

This makes the script ultimately suitable for users that have descriptions in their images but without assigned keywords. The script will then assign the keywords.
Installation Instructions na
Usage Instructions 1. Select one or more thumbnails for which the keywords should be generated
2. Right click on a selected thumb and choose Run Script from Repository -> Catalog -> AI Keywords for Descriptions
Changes in this version na
Special Requirements
 

Author Information

Author Name IDimager Support
 

Script

{
  This script will use AI to extract keywords from the
  Description (dc:description) metadata field and then assign
  those as AI labels to the image.

  This makes the script ultimately suitable for users that have
  descriptions in their images but without assigned keywords. The
  script will then assign the keywords.

  Initial date: 7 feb 2025
  Initial Author: IDimager Support

  Version History:
    Version: 1.0
    Author: IDimager Support
    Date: 7 feb 2025
}

  procedure ExtractKeywordsFromText(ACatItem: TCatalogItem; AText: WideString);
  var
    AAI: TidBaseOpenAI;
    AResult: TidOpenAIResult;
  begin
    AResult := TidOpenAIResult.Create(nil);

    AAI := Publiccatalog.CreateOpenAIObject;
    if AAI.QueryKeywordsForText(AText, '', AResult) then
    begin
      {
      Say2(
        ACatItem.FileName + CrLf2W + AText,
        AResult.HierarchicalKeywords(CrLfW, '.')
      );
      }

      PublicCatalog.AIOpenAI_ProcessResult(ACatItem, AResult, DefaultOpenAIResultHandling);
    end;
    AAI.Free;

    AResult.Free;
  end;

  procedure HandleItem(ACatItem: TCatalogItem);
  var
    AXmp: TXMP;
    ADescription: TMacroParam;
  begin
    AXmp := TXMP.Create(False);
    PublicCatalog.LoadXMPForItem(ACatItem, AXmp, PublicOptions.CachedXMP);

    ADescription := AXmp.FindDesignProperty('http://purl.org/dc/elements/1.1/', 'dc:description');
    if ADescription <> nil then
    begin
      ExtractKeywordsFromText(ACatItem, ADescription.StrContent);
    end;

    AXmp.Free;
  end;

  procedure HandleImageItem(AImg: TImageItem);
  var
    ACatItem: TCatalogItem;
  begin
    // find the image in the catalog
    ACatItem := TCatalogItem.Create(nil);

    if PublicCatalog.FindItemCombinedByGUID(AImg.GUID, ACatItem) then
      HandleItem(ACatItem);

    ACatItem.Free;
  end;

var
  i: Integer;
  APro: TxomProgress;
begin
  if Selected.Count = 0 then
  begin
    Say('Select at least one thumbnail.');
    exit;
  end;

  APro := TxomProgress.Create(nil);
  APro.Max := Selected.Count;
  APro.Pos := 0;
  APro.Caption := 'Detect keywords for Description';

  for i := 0 to Selected.Count - 1 do
  begin
    APro.Pos := i + 1;
    if APro.Cancel then
      break;

    HandleImageItem(Selected.Items[i]);
  end;

  APro.Free;
end;