Tutorial: Get started with the Cognitive capabilities of U-SQL

Overview

Cognitive capabilities for U-SQL enable developers to use put intelligence in their big data programs.

The following cognitive capabilities are available:

  • Imaging: Detect faces
  • Imaging: Detect emotion
  • Imaging: Detect objects (tagging)
  • Imaging: OCR (optical character recognition)
  • Text: Key Phrase Extraction
  • Text: Sentiment Analysis

How to use Cognitive in your U-SQL script

The overall process is simple:

  • Use the REFERENCE ASSEMBLY statement to enable the cognitive features for the U-SQL Script
  • Use the PROCESS on an input Rowset using a Cognitive UDO, to generate an output RowSet

Detecting objects in images

The following example shows how to use the cognitive capabilities to detect objects in images.

REFERENCE ASSEMBLY ImageCommon;
REFERENCE ASSEMBLY FaceSdk;
REFERENCE ASSEMBLY ImageEmotion;
REFERENCE ASSEMBLY ImageTagging;
REFERENCE ASSEMBLY ImageOcr;

// Get the image data

@imgs =
    EXTRACT 
        FileName string, 
        ImgData byte[]
    FROM @"/usqlext/samples/cognition/{FileName}.jpg"
    USING new Cognition.Vision.ImageExtractor();

//  Extract the number of objects on each image and tag them 

@tags =
    PROCESS @imgs 
    PRODUCE FileName,
            NumObjects int,
            Tags SQL.MAP<string, float?>
    READONLY FileName
    USING new Cognition.Vision.ImageTagger();

@tags_serialized =
    SELECT FileName,
           NumObjects,
           String.Join(";", Tags.Select(x => String.Format("{0}:{1}", x.Key, x.Value))) AS TagsString
    FROM @tags;

OUTPUT @tags_serialized
    TO "/tags.csv"
    USING Outputters.Csv();

For more examples, look at the U-SQL/Cognitive Samples in the Next steps section.

Next steps