public void Setup() { _client = new V2.V2Client(ClarifaiChannel.Grpc( Environment.GetEnvironmentVariable("CLARIFAI_GRPC_BASE") )); _metadata = new Metadata { { "Authorization", "Key " + Environment.GetEnvironmentVariable("CLARIFAI_API_KEY") } }; }
private async Task <Output> ClarifaiGeneralModel(String receipt) { var stream = File.Open(receipt, FileMode.Open); try { var client = new V2.V2Client(ClarifaiChannel.Grpc()); var metadata = new Metadata { { "Authorization", "Key " + Config.ClarifaiKey } }; var response = await client.PostModelOutputsAsync( new PostModelOutputsRequest() { // general model id ModelId = "aaa03c23b3724a16a56b629203edc62c", Inputs = { new List <Input>() { new Input() { Data = new Data() { Image = new Image() { Base64 = await Google.Protobuf.ByteString.FromStreamAsync(stream) } } } } } }, metadata ); if (response.Status.Code != Clarifai.Api.Status.StatusCode.Success) { throw new Exception("Post model outputs failed, status: " + response.Status.Description); } var output = response.Outputs.First(); return(output); } finally { stream.Close(); } }
public Boolean PostModelOutputsWithFileBytes(IFormFile file) { Debug.WriteLine(file.FileName); ByteString bytes; //string path = @"D:\" + fileName + "." + fileExtension; var ms = new MemoryStream(); file.CopyTo(ms); var fileBytes = ms.ToArray(); string s = Convert.ToBase64String(fileBytes); bytes = ByteString.FromBase64(s); var client = new V2.V2Client(ClarifaiChannel.Grpc()); var metadata = new Metadata { { "Authorization", "Key " } }; var response = client.PostModelOutputs( new PostModelOutputsRequest() { ModelId = "", Inputs = { new List <Input>() { new Input() { Data = new Clarifai.Api.Data() { Image = new Image() { Base64 = bytes } } } } } }, metadata ); if (response.Status.Code != Clarifai.Api.Status.StatusCode.Success) { throw new Exception("Request failed, response: " + response); } var isHuman = false; Console.WriteLine("Predicted concepts:"); string output = ""; foreach (var concept in response.Outputs[0].Data.Concepts) { if (concept.Name == "portrait" || concept.Name == "woman" || concept.Name == "adult" || concept.Name == "man" || concept.Name == "child" || concept.Name == "face" || concept.Name == "baby" || concept.Name == "human" || concept.Name == "son" || concept.Name == "girl" || concept.Name == "man") { isHuman = true; } output += concept.Name + "\n, "; Console.WriteLine(isHuman); Console.WriteLine($"{concept.Name,15} {concept.Value:0.00}"); } return(isHuman); }
/// <summary> /// This method is called for every Lambda invocation. This method takes in an S3 event object and can be used /// to respond to S3 notifications. /// </summary> /// <param name="evnt"></param> /// <param name="context"></param> /// <returns></returns> public async Task <string> FunctionHandler(S3Event evnt, ILambdaContext context) { context.Logger.LogLine("Start of task"); var s3Event = evnt.Records?[0].S3; if (s3Event == null) { context.Logger.LogLine("s3Event null"); return(null); } byte[] imageBytes = null; var contentType = ""; try { using var objectResponse = await S3Client.GetObjectAsync(s3Event.Bucket.Name, s3Event.Object.Key); await using var ms = new MemoryStream(); contentType = objectResponse.Headers.ContentType; await objectResponse.ResponseStream.CopyToAsync(ms); imageBytes = ms.ToArray(); } catch (Exception e) { context.Logger.LogLine( $"Error getting object {s3Event.Object.Key} from bucket {s3Event.Bucket.Name}. Make sure they exist and your bucket is in the same region as this function."); context.Logger.LogLine(e.Message); context.Logger.LogLine(e.StackTrace); throw; } context.Logger.LogLine("Initializing clarifai client"); //get image analysis var client = new V2.V2Client(ClarifaiChannel.Grpc()); context.Logger.LogLine("Initializing clarifai metadata"); context.Logger.LogLine(Environment.GetEnvironmentVariable("Clarifai_Key")); var metadata = new Metadata() { { "Authorization", "Key " + Environment.GetEnvironmentVariable("Clarifai_Key") } }; context.Logger.LogLine("Clarifai post"); var irsResponse = client.PostModelOutputs( new PostModelOutputsRequest() { ModelId = Environment.GetEnvironmentVariable("Clarifai_Model_Id"), Inputs = { new List <Input>() { new Input() { Data = new Data() { Image = new Image() { Base64 = ByteString.CopyFrom(imageBytes) } } } } } }, metadata ); if (irsResponse.Status.Code != Clarifai.Api.Status.StatusCode.Success) { context.Logger.LogLine("Request failed, response: " + irsResponse); throw new Exception("Request failed, response: " + irsResponse); } //store image analysis var labels = irsResponse.Outputs[0].Data.Concepts .Select(label => new Label { Name = label.Name, Confidence = label.Value }).ToList(); var str = JsonSerializer.Serialize(labels); var fileStream = new MemoryStream(Encoding.UTF8.GetBytes(str)); var status = await S3Client.PutObjectAsync(new PutObjectRequest() { BucketName = "fridgedata-s3", Key = "FridgeContents.json", InputStream = fileStream, }); if (status.HttpStatusCode != HttpStatusCode.OK) { context.Logger.LogLine("Request failed, response: " + status); throw new Exception("Request failed, response: " + status); } context.Logger.LogLine(str); return(str); }
private Action GetAction() { return(() => { try { #if (!DEBUG_DONOTINVOKEREALAPI) var client = new V2.V2Client(ClarifaiChannel.Grpc()); var metadata = new Metadata { { "Authorization", $"Key {Environment.GetEnvironmentVariable("CLARIFAI_API_KEY")}" } }; PostModelOutputsRequest postModelOutputRequest = null; if (ImageLocationType == CVClientCommon.LocationType.Uri) { postModelOutputRequest = new PostModelOutputsRequest() { ModelId = "aaa03c23b3724a16a56b629203edc62c", Inputs = { new List <Input>() { new Input() { Data = new Data() { Image = new Image() { Url = ImageLocation } } } } } }; } else if (ImageLocationType == CVClientCommon.LocationType.Local) { using (Stream imageStream = File.OpenRead(ImageLocation)) { postModelOutputRequest = new PostModelOutputsRequest() { ModelId = "aaa03c23b3724a16a56b629203edc62c", Inputs = { new List <Input>() { new Input() { Data = new Data() { Image = new Image() { Base64 = Google.Protobuf.ByteString.FromStream(imageStream) } } } } } }; } } APIResponse = client.PostModelOutputs(postModelOutputRequest, metadata); #endif #if DEBUG_DONOTINVOKEREALAPI CVClientCommon.SimulateAPIInvocation(); #endif } catch (Exception ex) { ExceptionRasied = ex; } finally { Processed = DateTime.Now; } }); }