private void OnBarcodeDetectionComplete(string error) { // Remove rectangle detection from the ongoing requests indicator _requestsInProgress &= ~VisionRequest.BarcodeScanning; // Handle errors if (!string.IsNullOrEmpty(error)) { if (error.Contains("Error") || error.Contains("error")) { Debug.LogError(error); } else { Debug.LogWarning(error); } // Since the message only represents errors, return if its not empty return; } // If anyone is interested in the results if (OnBarcodesDetected != null) { // Acquire resulting points if (_vision_acquireBarcodeBuffer(_barcodeBuffer) > 0) { // Notify listeners about the resulting points of the recognized rectangles OnBarcodesDetected(this, new BarcodesDetectedArgs(_barcodeBuffer)); } } }
/// <summary> /// Invoked from native component when an object gets classified. /// </summary> /// <param name="error">Error message sent from the native component.</param> private void OnClassificationComplete(string error) { // Remove classification from the ongoing requests indicator _requestsInProgress &= ~VisionRequest.Classification; // Handle errors if (!string.IsNullOrEmpty(error)) { if (error.Contains("Error") || error.Contains("error")) { Debug.LogError(error); } else { Debug.LogWarning(error); } // Since the message only represents errors, return if its not empty return; } // If anyone is interested in the results if (OnObjectClassified != null) { var length = _vision_acquireClassificationBuffer(_classificationBuffer, _maxObservations); if (length < 1) { return; } // Notify listeners OnObjectClassified(this, new ClassificationResultArgs(_classificationBuffer.Take(length).ToArray())); } }
/// <summary> /// Note: Heavy call! /// Allocates VNRequest objects for iOS Vision Framework based on the specified flags. /// </summary> /// <param name="requests">Requests.</param> /// <param name="maxObservations"></param> public void SetAndAllocateRequests(VisionRequest requests, int maxObservations) { // Cache the required vision requests _requestsToPerform = requests; // Allocate vision requests _vision_allocateVisionRequests((int)_requestsToPerform, maxObservations); // Re-allocate copy buffers _classificationBuffer = new VisionClassification[maxObservations]; _maxObservations = maxObservations; }
/// <summary> /// Note: Heavy call! /// Allocates VNRequest objects for iOS Vision Framework based on the specified flags. /// </summary> /// <param name="requests">Requests.</param> /// <param name="maxObservations"></param> public void SetAndAllocateRequests(VisionRequest requests, int maxObservations) { // Cache the required vision requests _requestsToPerform = requests; // Allocate vision requests _vision_allocateVisionRequests((int)_requestsToPerform, maxObservations); // Re-allocate copy buffers _barcodeBuffer = new VisionBarcode[maxObservations]; _pointBuffer = new CGPoint[maxObservations * 4]; _maxObservations = maxObservations; }
private VisionResponse Request(string[] imagesAsBase64, string url) { var request = new VisionRequest(imagesAsBase64); var jsonRequest = JsonConvert.SerializeObject(request, JsonSettings); var requestContent = new StringContent(jsonRequest, Encoding.UTF8, "application/json"); if (!string.IsNullOrWhiteSpace(_apiKey)) { _httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {_apiKey}"); } var requestUrl = UseProxyIfPossible(BaseUrl + url); var response = _httpClient.PostAsync(requestUrl, requestContent).Result; var result = response.Content.ReadAsStringAsync().Result; return(JsonConvert.DeserializeObject <VisionResponse>(result)); }
/// <summary> /// Evaluates the provided pixel buffer for recognizable objects. /// </summary> /// <param name="buffer">Native pointer to the image data to evaluate.</param> /// <param name="dataType">The nature of the data buffer.</param> public void EvaluateBuffer(IntPtr buffer, ImageDataType dataType) { if (_requestsToPerform == VisionRequest.None) { Debug.LogError("[Vision] Unspecified vision request."); return; } if (_requestsInProgress != VisionRequest.None) { Debug.LogError("[Vision] One or more vision requests are still in progress."); return; } if (buffer == IntPtr.Zero) { Debug.LogError("[Vision] Pointer to buffer is null."); return; } bool success; switch (dataType) { case ImageDataType.MetalTexture: success = _vision_evaluateWithTexture(buffer) > 0; break; case ImageDataType.CoreVideoPixelBuffer: success = _vision_evaluateWithBuffer(buffer) > 0; break; default: throw new ArgumentOutOfRangeException("dataType", dataType, null); } if (success) { // Store requests in progress _requestsInProgress = _requestsToPerform; } else { Debug.LogError("[Vision] Unable to perform vision request. Pointer to buffer is not in expected type or is no longer accessible."); } }
public static VisionRequestBody Default(byte[] content = null) { VisionImage image = new VisionImage(); if (content != null) { image.EncodeContent(content); } VisionFeature feature = new VisionFeature(); feature.type = Enum.GetName(typeof(VisionFeature.FeatureType), 0); VisionRequest request = new VisionRequest(); request.features = new VisionFeature[] { feature }; request.image = image; VisionRequestBody body = new VisionRequestBody(); body.requests = new VisionRequest[] { request}; return body; }
/// <summary> /// Invoked from native component when rectangles get recognized. /// </summary> /// <param name="error">Error message sent from the native component.</param> private void OnRectangleRecognitionComplete(string error) { // Remove rectangle detection from the ongoing requests indicator _requestsInProgress &= ~VisionRequest.RectangleDetection; // Handle errors if (!string.IsNullOrEmpty(error)) { if (error.Contains("Error") || error.Contains("error")) { Debug.LogError(error); } else { Debug.LogWarning(error); } // Since the message only represents errors, return if its not empty return; } // If anyone is interested in the results if (OnRectanglesRecognized != null) { // Acquire resulting points var length = _vision_acquirePointBuffer(_pointBuffer); if (length < 4) { return; } var coordinates = _pointBuffer.Take(length).Select(point => (Vector2)point).ToArray(); AlignScreenCoordinates(coordinates); // Notify listeners about the resulting points of the recognized rectangles OnRectanglesRecognized(this, new RectanglesRecognizedArgs(coordinates)); } }
public static bool HasFlag(this VisionRequest request, VisionRequest flag) { return((request & flag) == flag); }