Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Fulcrum API/AWS Rekognition with .Net Core
using System;
using System.Drawing;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using Amazon.Rekognition;
using Amazon.Rekognition.Model;
using Newtonsoft.Json;
namespace fulcrumRek
{
class Program
{
private static string imageId = "";
private static string fulcrumKey = "";
private static string awsKey = "";
private static string awsSecret = "";
private static string awsUname = "";
private static string awsPw = "";
private static string fulcrumPhotoTemplate = "https://api.fulcrumapp.com/api/v2/photos/{0}.jpg?token={1}";
//args fulcrum image id, fulcrum api key
static void Main(string[] args)
{
MainAsync(args).Wait();
}
static async Task MainAsync(string[] args)
{
try
{
imageId = args[0];
fulcrumKey = args[1];
awsKey = args[2];
awsSecret = args[3];
byte[] photoBytes = null;
string url = string.Format(fulcrumPhotoTemplate, imageId, fulcrumKey);
photoBytes = getImageFromUrl(url);
Amazon.Rekognition.Model.Image img = new Amazon.Rekognition.Model.Image();
img.Bytes = new MemoryStream(photoBytes);
DetectLabelsRequest detectLabels = new DetectLabelsRequest();
detectLabels.Image = img;
//probably shouldn't hardcode the region
AmazonRekognitionClient rekClient = new AmazonRekognitionClient(awsKey, awsSecret, Amazon.RegionEndpoint.USEast1);
var o = await rekClient.DetectLabelsAsync(detectLabels);
if (o.Labels.Count > 0)
{
var s = JsonConvert.SerializeObject(o.Labels);
Console.WriteLine(s); //do something cooler here
}
}
catch(Exception ex)
{
//the least elegant exception handling in the history of exception handling
Console.WriteLine(JsonConvert.SerializeObject(ex));
}
}
static byte[] getImageFromUrl(string url) //credit: https://snipplr.com/view/64141/pull-image-into-byte-array-from-a-url/
{
System.Net.HttpWebRequest request = null;
System.Net.HttpWebResponse response = null;
byte[] b = null;
request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
response = (System.Net.HttpWebResponse)request.GetResponse();
if (request.HaveResponse)
{
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
Stream receiveStream = response.GetResponseStream();
int byteCount = (int)response.ContentLength;
using (BinaryReader br = new BinaryReader(receiveStream))
{
b = br.ReadBytes(byteCount);
br.Close();
}
}
}
return b;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.