private Action GetAction() { return(() => { try { #if (!DEBUG_DONOTINVOKEREALAPI) var iaClient = ImageAnnotatorClient.Create(); APIResponse = iaClient.Annotate(GetAnnotateImageRequest()); #endif #if DEBUG_DONOTINVOKEREALAPI CVClientCommon.SimulateAPIInvocation(); #endif } catch (Exception ex) { ExceptionRasied = ex; } finally { Processed = DateTime.Now; } }); }
private Action GetAction() { return(() => { try { #if (!DEBUG_DONOTINVOKEREALAPI) string endpoint = Environment.GetEnvironmentVariable("OPEN_SOURCE_CV_ENDPOINT"); var camelCaseJsonSerializerSetting = new JsonSerializerSettings() { ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver() }; var client = new HttpClient() { Timeout = HTTPConnectionTimeout }; var stringContent = new StringContent(JsonConvert.SerializeObject(GetOpenSourceRequest(), camelCaseJsonSerializerSetting), Encoding.UTF8, "application/json"); var postTask = client.PostAsync(endpoint, stringContent); System.Diagnostics.Debug.WriteLine($"Posting to API for {this.ImageLocation}"); postTask.Wait(HTTPReadTimeout); var httpResponse = postTask.Result; var readStringTask = httpResponse.Content.ReadAsStringAsync(); System.Diagnostics.Debug.WriteLine($"Reading API response for {this.ImageLocation}"); readStringTask.Wait(HTTPReadTimeout); try { APIResponse = JsonConvert.DeserializeObject <OpenSourceModelResult>(readStringTask.Result, camelCaseJsonSerializerSetting); } catch { throw new Exception($"Invalid response from API: {readStringTask.Result}"); } foreach (var modelResult in APIResponse.Results) { modelResult.Labels = modelResult.Labels.Where(l => l.Score >= FlatteningMinScore).OrderByDescending(l => l.Score).ToArray(); } #endif #if DEBUG_DONOTINVOKEREALAPI CVClientCommon.SimulateAPIInvocation(); #endif } catch (Exception ex) { ExceptionRasied = ex; } finally { Processed = DateTime.Now; } }); }
private static Dictionary <string, string> readModelOptions(string resourceId) { var modelDict = new Dictionary <string, string>(); var cvModels = JsonConvert.DeserializeObject <List <CVModel> >(CVClientCommon.GetEmbeddedResourceText(resourceId)); foreach (var cvModel in cvModels) { modelDict.Add(cvModel.Title, cvModel.ModelId); } return(modelDict); }
private Action GetAction() { return(() => { try { #if (!DEBUG_DONOTINVOKEREALAPI) var client = new ComputerVisionClient(new ApiKeyServiceClientCredentials(Environment.GetEnvironmentVariable("MICROSOFT_AZURE_CV_KEY"))) { Endpoint = Environment.GetEnvironmentVariable("MICROSOFT_AZURE_CV_ENDPOINT") }; Task <ImageAnalysis> imageAnalysisTask = null; if (ImageLocationType == CVClientCommon.LocationType.Local) { Stream imageStream = new MemoryStream(File.ReadAllBytes(ImageLocation)); imageAnalysisTask = client.AnalyzeImageInStreamAsync(imageStream, visualFeatures: DetectionFeatureTypes.ToList()); } else if (ImageLocationType == CVClientCommon.LocationType.Uri) { imageAnalysisTask = client.AnalyzeImageAsync(ImageLocation, visualFeatures: DetectionFeatureTypes.ToList()); } imageAnalysisTask.Wait(); APIResponse = imageAnalysisTask.Result; #endif #if DEBUG_DONOTINVOKEREALAPI CVClientCommon.SimulateAPIInvocation(); #endif } catch (Exception ex) { ExceptionRasied = ex; } finally { Processed = DateTime.Now; } }); }
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; } }); }