Skip to content

Instantly share code, notes, and snippets.

@xximjasonxx
Created September 6, 2020 02:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xximjasonxx/3215a61045a6bd892f9f0545b5d7326f to your computer and use it in GitHub Desktop.
Save xximjasonxx/3215a61045a6bd892f9f0545b5d7326f to your computer and use it in GitHub Desktop.
public static List<OcrResult> AsResultList(this ImageAnalysis analysisResult, string fileId)
{
var returnList = new List<OcrResult>();
returnList.AddRange(analysisResult.Adult.AsOcrPairs(fileId, OcrType.ComputerVision));
returnList.AddRange(analysisResult.Color.AsOcrPairs(fileId, OcrType.ComputerVision));
returnList.AddRange(analysisResult.ImageType.AsOcrPairs(fileId, OcrType.ComputerVision));
returnList.AddRange(analysisResult.Description.Captions.FirstOrDefault()?.AsOcrPairs(fileId, OcrType.ComputerVision));
//returnList.AddRange(analysisResult.Brands.AsOcrPairs(fileId, OcrType.ComputerVision));
//returnList.AddRange(analysisResult.Faces.AsOcrPairs(fileId, OcrType.ComputerVision));
return returnList;
}
static IEnumerable<OcrResult> AsOcrPairs(this object obj, string fileId, OcrType ocrType)
{
foreach (var propertyInfo in obj.GetType().GetProperties())
{
if (typeof(IEnumerable).IsAssignableFrom(propertyInfo.PropertyType) == false || propertyInfo.PropertyType == typeof(string))
{
yield return new OcrResult(fileId)
{
KeyName = propertyInfo.Name,
OcrValue = propertyInfo.GetValue(obj).ToString(),
OcrType = ocrType
};
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment