public async Task <DetectionResult> DetectAsync(DetectionRequest request) { var image = Image.FromUri(request.ImageUri); var annotation = await _client.DetectDocumentTextAsync(image); return(new DetectionResult(annotation.Text)); }
public async Task DispatchAsync(DetectionRequest request, IContext context) { await _sender.SendAsync( await _transformer.TransformAsync( await _detector.DetectAsync(request), context ), context ); }
/// <summary>) /// Serialize the image that was taken and turn it into a rawrequest. /// 1. Take photo and decode it as jpeg string string /// 2. decode the jpeg wtih base64 to be serializeable /// 3. serialize everything as json string /// 4. serialize the json string as raw request /// </summary> private IEnumerator <bool> SerializeRequest(PhotoCaptureFrame photoCapturedFrame) { yield return(true); //Texture2D tex = new Texture2D(ImageCapture.Instance.width, // ImageCapture.Instance.height); //photoCapturedFrame.UploadImageDataToTexture(tex); //byte[] jpgEncoded = tex.EncodeToJPG List <byte> jpgEncodedList = new List <byte>(); photoCapturedFrame.CopyRawImageDataIntoBuffer(jpgEncodedList); byte[] jpgEncoded = jpgEncodedList.ToArray(); // server expects an base64 encoded JPG encoded string // should have the form {"inputs": [{"b64": <b64encodejpgencodedstring>}]} string b64Encode = Convert.ToBase64String(jpgEncoded); DetectionRequest detectionRequest = new DetectionRequest { inputs = new List <B64> { new B64 { b64 = b64Encode } } }; string jsonRequest = JsonConvert.SerializeObject(detectionRequest); RequestBufferElem requestBufferElem = new RequestBufferElem() { rawRequest = Encoding.UTF8.GetBytes(jsonRequest) }; if (!photoCapturedFrame.TryGetCameraToWorldMatrix(out requestBufferElem.cameraToWorld) || !photoCapturedFrame.TryGetProjectionMatrix(out requestBufferElem.projection)) { requestBufferElem.hasWorldData = false; } else { requestBufferElem.hasWorldData = true; } photoCapturedFrame.Dispose(); rawRequestBuffer.Enqueue(requestBufferElem); rawRequestBufferEmpty = false; timestamp = stopwatch.ElapsedMilliseconds; }
public virtual Response DetectAnomaly(Guid modelId, DetectionRequest body, CancellationToken cancellationToken = default) { using var scope = _clientDiagnostics.CreateScope("AnomalyDetectorClient.DetectAnomaly"); scope.Start(); try { return(RestClient.DetectAnomaly(modelId, body, cancellationToken).GetRawResponse()); } catch (Exception e) { scope.Failed(e); throw; } }
public virtual async Task <Response> DetectAnomalyAsync(Guid modelId, DetectionRequest body, CancellationToken cancellationToken = default) { using var scope = _clientDiagnostics.CreateScope("AnomalyDetectorClient.DetectAnomaly"); scope.Start(); try { return((await RestClient.DetectAnomalyAsync(modelId, body, cancellationToken).ConfigureAwait(false)).GetRawResponse()); } catch (Exception e) { scope.Failed(e); throw; } }
private async Task <DetectionResult> detectAsync(AnomalyDetectorClient client, string datasource, Guid model_id, DateTimeOffset start_time, DateTimeOffset end_time) { try { Console.WriteLine("Start detect..."); Response <Model> get_response = await client.GetMultivariateModelAsync(model_id).ConfigureAwait(false); DetectionRequest detectionRequest = new DetectionRequest(datasource, start_time, end_time); Response result_response = await client.DetectAnomalyAsync(model_id, detectionRequest).ConfigureAwait(false); var ok = result_response.Headers.TryGetValue("Location", out string result_id_path); Guid result_id = Guid.Parse(result_id_path.Split('/').LastOrDefault()); // get detection result Response <DetectionResult> result = await client.GetDetectionResultAsync(result_id).ConfigureAwait(false); while (result.Value.Summary.Status != DetectionStatus.Ready & result.Value.Summary.Status != DetectionStatus.Failed) { System.Threading.Thread.Sleep(2000); result = await client.GetDetectionResultAsync(result_id).ConfigureAwait(false); } if (result.Value.Summary.Status != DetectionStatus.Ready) { Console.WriteLine(String.Format("Inference failed.")); IReadOnlyList <ErrorResponse> errors = result.Value.Summary.Errors; foreach (ErrorResponse error in errors) { Console.WriteLine(String.Format("Error code: {0}.", error.Code)); Console.WriteLine(String.Format("Error message: {0}.", error.Message)); } return(null); } return(result.Value); } catch (Exception e) { Console.WriteLine(String.Format("Detection error. {0}", e.Message)); throw new Exception(e.Message); } }
private async Task <DetectionResult> DetectAsync(AnomalyDetectorClient client, string datasource, Guid model_id, DateTimeOffset start_time, DateTimeOffset end_time, int max_tryout = 500) { try { Console.WriteLine("Start detect..."); Response <Model> get_response = await client.GetMultivariateModelAsync(model_id).ConfigureAwait(false); DetectionRequest detectionRequest = new DetectionRequest(datasource, start_time, end_time); Response result_response = await client.DetectAnomalyAsync(model_id, detectionRequest).ConfigureAwait(false); var ok = result_response.Headers.TryGetValue("Location", out string result_id_path); Guid result_id = Guid.Parse(result_id_path.Split('/').LastOrDefault()); // get detection result Response <DetectionResult> result = await client.GetDetectionResultAsync(result_id).ConfigureAwait(false); int tryout_count = 0; while (result.Value.Summary.Status != DetectionStatus.Ready & tryout_count < max_tryout) { System.Threading.Thread.Sleep(2000); result = await client.GetDetectionResultAsync(result_id).ConfigureAwait(false); tryout_count += 1; } if (result.Value.Summary.Status != DetectionStatus.Ready) { Console.WriteLine(String.Format("Request timeout after {0} tryouts", max_tryout)); return(null); } return(result.Value); } catch (Exception e) { Console.WriteLine(String.Format("Detection error. {0}", e.Message)); throw; } }
public static DetectionResultSummary DetectionResultSummary(DetectionStatus status = default, IReadOnlyList <ErrorResponse> errors = default, IReadOnlyList <VariableState> variableStates = default, DetectionRequest setupInfo = default) { errors ??= new List <ErrorResponse>(); variableStates ??= new List <VariableState>(); return(new DetectionResultSummary(status, errors, variableStates, setupInfo)); }