Exemple #1
0
        public void RecognizeFaces(JavaScriptValue faces, JavaScriptValue callback)
        {
            var boundList = new List <BitmapBounds>();

            for (int i = 0; i < faces.Length().Value; ++i)
            {
                var jsBounds = faces.Get(i).Get("bounds");
                var bounds   = new BitmapBounds()
                {
                    X      = (uint)jsBounds.Get("x").ToInt32(),
                    Y      = (uint)jsBounds.Get("y").ToInt32(),
                    Width  = (uint)jsBounds.Get("width").ToInt32(),
                    Height = (uint)jsBounds.Get("height").ToInt32()
                };
                boundList.Add(bounds);
            }

            int frameID = faces.Get(0).Get("frame").Get("id").ToInt32();
            var frame   = SceneCameraManager.Inst.GetFrameFromCache(frameID);

            callback.AddRef();
            faces.AddRef();

            server.RecognizeFaces(frame.bitmap, boundList, (s) => {
                JsonObject json;
                if (!JsonObject.TryParse(s, out json))
                {
                    ProjectRuntime.Inst.DispatchRuntimeCode(() => {
                        for (int i = 0; i < faces.Length().Value; ++i)
                        {
                            faces.Get(i).SetProperty(JavaScriptPropertyId.FromString("name"), JavaScriptValue.FromString("Unknown"), true);
                        }
                        callback.CallFunction(callback, faces);
                        callback.Release();
                        faces.Release();
                    });
                    return;
                }

                var responses = json.GetNamedArray("ResponsePerFace");
                var names     = new List <string>();
                for (int i = 0; i < responses.Count; ++i)
                {
                    var faceResponse = responses.GetObjectAt((uint)i);
                    names.Add(faceResponse.GetNamedString("FaceRecognition"));
                }

                ProjectRuntime.Inst.DispatchRuntimeCode(() => {
                    for (int i = 0; i < faces.Length().Value; ++i)
                    {
                        faces.Get(i).SetProperty(JavaScriptPropertyId.FromString("name"), JavaScriptValue.FromString(names[i]), true);
                    }
                    callback.CallFunction(callback, faces);
                    callback.Release();
                    faces.Release();
                });
            });
        }
Exemple #2
0
        public static void ProcessUpdate()
        {
            while (csharpCallback.Count != 0)
            {
                Action fn = csharpCallback.Dequeue();
                fn();
            }

            while (oneUpdateCallbacks.Count != 0)
            {
                JavaScriptValue fn = oneUpdateCallbacks.Dequeue();
                try {
                    fn.CallFunction(JavaScriptValue.Undefined);
                } catch (Exception e) {
                    Debug.LogError(e);
                } finally {
                    fn.Release();
                }
            }

            foreach (var pair in updateCallbacks)
            {
                try {
                    pair.Value.CallFunction(JavaScriptValue.Undefined);
                } catch (Exception e) {
                    Debug.LogError(e);
                }
            }
        }
Exemple #3
0
 public void Dispose()
 {
     if (rawvalue.IsValid)
     {
         rawvalue.Release();
     }
     rawvalue = JavaScriptValue.Invalid;
 }
        protected override void Dispose(bool disposing)
        {
            // We need to release the object so that the Chakra GC can reclaim it.
            taskFunc.Release();

            base.Dispose(disposing);

            if (disposing)
            {
                // No need to finalize the object if we've been disposed.
                GC.SuppressFinalize(this);
            }
        }
        /// <summary>
        ///     A promise continuation callback.
        /// </summary>
        /// <remarks>
        ///     The host can specify a promise continuation callback in <c>JsSetPromiseContinuationCallback</c>. If
        ///     a script creates a task to be run later, then the promise continuation callback will be called with
        ///     the task and the task should be put in a FIFO queue, to be run when the current script is
        ///     done executing.
        /// </remarks>
        /// <param name="task">The task, represented as a JavaScript function.</param>
        /// <param name="callbackState">The data argument to be passed to the callback.</param>
        /// <see cref="JavaScriptPromiseContinuationCallback"></see>
        private void PromiseContinuationCallback(JavaScriptValue task, IntPtr callbackState)
        {
            task.AddRef();

            try
            {
                task.CallFunction(JavaScriptValue.GlobalObject);
            }
            finally
            {
                task.Release();
            }
        }
 internal void SetJSSource(JavaScriptValue value, IContextSwitchService context)
 {
     if (JSSource.IsValid)
     {
         throw new InvalidOperationException("cannot set jsvalue twice");
     }
     if (!value.IsValid)
     {
         throw new ArgumentException("not a valid javascriptvalue", nameof(value));
     }
     value.AddRef();
     JSSource       = value;
     releaseJSValue = new Action(() => { context.With(() => value.Release()); });
 }
            public void Dispose()
            {
                if (!IsDisposed)
                {
                    OnChangeCallback.Release();

                    UnsafeUtility.Free(DataPtr, Allocator.Persistent);

                    HasData = false;
                    Size    = 0;
                }

                IsDisposed = true;
            }
Exemple #8
0
        public static void ProcessPromiseTaskQueue() // called inside With()
        {
            while (promiseTaskQueue.Count != 0)
            {
                JavaScriptValue task = promiseTaskQueue.Dequeue();

                try {
                    task.CallFunction(JavaScriptValue.Undefined);
                } catch (Exception e) {
                    Debug.LogError(e);
                }

                task.Release();
            }
        }
Exemple #9
0
        public string runScript(string script)
        {
            IntPtr returnValue;

            try
            {
                JavaScriptValue result;

                if (Native.JsRunScript(script, currentSourceContext++, "", out result) != JavaScriptErrorCode.NoError)
                {
                    // Get error message and clear exception
                    JavaScriptValue exception;
                    if (Native.JsGetAndClearException(out exception) != JavaScriptErrorCode.NoError)
                    {
                        return("failed to get and clear exception");
                    }

                    JavaScriptPropertyId messageName;
                    if (Native.JsGetPropertyIdFromName("message",
                                                       out messageName) != JavaScriptErrorCode.NoError)
                    {
                        return("failed to get error message id");
                    }

                    JavaScriptValue messageValue;
                    if (Native.JsGetProperty(exception, messageName, out messageValue)
                        != JavaScriptErrorCode.NoError)
                    {
                        return("failed to get error message");
                    }

                    IntPtr  message;
                    UIntPtr length;
                    if (Native.JsStringToPointer(messageValue, out message, out length) != JavaScriptErrorCode.NoError)
                    {
                        return("failed to convert error message");
                    }

                    return(Marshal.PtrToStringUni(message));
                }

                // Execute promise tasks stored in taskQueue
                while (taskQueue.Count != 0)
                {
                    JavaScriptValue task = (JavaScriptValue)taskQueue.Dequeue();
                    JavaScriptValue promiseResult;
                    JavaScriptValue global;
                    Native.JsGetGlobalObject(out global);
                    JavaScriptValue[] args = new JavaScriptValue[1] {
                        global
                    };
                    Native.JsCallFunction(task, args, 1, out promiseResult);
                    task.Release();
                }

                // Convert the return value.
                JavaScriptValue stringResult;
                UIntPtr         stringLength;
                if (Native.JsConvertValueToString(result, out stringResult) != JavaScriptErrorCode.NoError)
                {
                    return("failed to convert value to string.");
                }
                if (Native.JsStringToPointer(stringResult, out returnValue, out stringLength) != JavaScriptErrorCode.NoError)
                {
                    return("failed to convert return value.");
                }
            }
            catch (Exception e)
            {
                return("chakrahost: fatal error: internal error: " + e.Message);
            }

            return(Marshal.PtrToStringUni(returnValue));
        }
Exemple #10
0
        public void GetEmotionForFaces(JavaScriptValue faces, JavaScriptValue callback)
        {
            if (RecognitionServer.Inst.IsBusy)
            {
                return;
            }

            var boundList = new List <BitmapBounds>();

            for (int i = 0; i < faces.Length().Value; ++i)
            {
                var jsBounds = faces.Get(i).Get("bounds");
                var bounds   = new BitmapBounds()
                {
                    X      = (uint)jsBounds.Get("x").ToInt32(),
                    Y      = (uint)jsBounds.Get("y").ToInt32(),
                    Width  = (uint)jsBounds.Get("width").ToInt32(),
                    Height = (uint)jsBounds.Get("height").ToInt32()
                };
                boundList.Add(bounds);
            }

            int frameID = faces.Get(0).Get("frame").Get("id").ToInt32();
            var frame   = SceneCameraManager.Inst.GetFrameFromCache(frameID);

            callback.AddRef();
            faces.AddRef();

            server.RecognizeEmotions(frame.bitmap, boundList, (s) => {
                JsonObject json;
                if (!JsonObject.TryParse(s, out json))
                {
                    ProjectRuntime.Inst.DispatchRuntimeCode(() => {
                        var emotions = JavaScriptValue.CreateArray(0);
                        var pushFunc = emotions.GetProperty(JavaScriptPropertyId.FromString("push"));
                        for (var i = 0; i < faces.Length().Value; ++i)
                        {
                            pushFunc.CallFunction(faces, JavaScriptValue.FromString("Unknown"));
                        }
                        callback.CallFunction(callback, faces);
                        callback.Release();
                        faces.Release();
                    });
                    return;
                }

                var responses = json.GetNamedArray("ResponsePerFace");
                ProjectRuntime.Inst.DispatchRuntimeCode(() => {
                    var emotions = JavaScriptValue.CreateArray(0);
                    var pushFunc = emotions.GetProperty(JavaScriptPropertyId.FromString("push"));
                    for (int i = 0; i < faces.Length().Value; ++i)
                    {
                        var emotion = responses.GetObjectAt((uint)i).GetNamedString("Emotion");
                        pushFunc.CallFunction(emotions, JavaScriptValue.FromString(emotion));
                    }
                    callback.CallFunction(callback, emotions);
                    callback.Release();
                    faces.Release();
                });
            });
        }
Exemple #11
0
        } // Register

        static Action <UnityWebRequest> MakeHttpCallback(JavaScriptValue callback, bool wantsArrayBuffer = false)
        {
            return((www) => {
                if (www.isNetworkError || www.isHttpError)
                {
                    Engine.With(() => {
                        try {
                            callback.CallFunction(
                                JavaScriptValue.Undefined,
                                JavaScriptValue.FromString(www.error),
                                JavaScriptValue.Null
                                );
                        } catch (Exception e) {
                            Debug.LogError(e);
                        } finally {
                            callback.Release();
                        }
                    });
                    return;
                }

                if (!wantsArrayBuffer)
                {
                    string text = www.downloadHandler.text;
                    Engine.With(() => {
                        try {
                            callback.CallFunction(
                                JavaScriptValue.Undefined,
                                JavaScriptValue.Null,
                                JavaScriptValue.FromString(text)
                                );
                        } catch (Exception e) {
                            Debug.LogError(e);
                        } finally {
                            callback.Release();
                        }
                    });
                }
                else // ArrayBuffer
                {
                    byte[] bytes = www.downloadHandler.data;
                    IntPtr unmanagedPointer = Marshal.AllocHGlobal(bytes.Length);
                    Marshal.Copy(bytes, 0, unmanagedPointer, bytes.Length);

                    Engine.With(() => {
                        Debug.Log("created ptr " + unmanagedPointer);
                        JavaScriptValue buffer = JavaScriptValue.CreateExternalArrayBuffer(
                            unmanagedPointer,
                            (uint)bytes.Length,
                            (ptr) => {
                            Debug.Log("freeing " + ptr);
                            Marshal.FreeHGlobal(ptr);
                        },
                            unmanagedPointer // pass it in since the upvalue will die! (probably)
                            );
                        try {
                            callback.CallFunction(
                                JavaScriptValue.Undefined,
                                JavaScriptValue.Null,
                                buffer
                                );
                        } catch (Exception e) {
                            Debug.LogError(e);
                        } finally {
                            callback.Release();
                        }
                    });
                }
            });
        }