Example #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();
                });
            });
        }
        public void Send(string body)
        {
            if (client == null || requestMessage == null)
            {
                return;
            }
            if (requestTask != null)
            {
                return;
            }

            requestTask = Task.Run(async() => {
                try {
                    var responseHeaders = await client.SendRequestAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead);

                    ProjectRuntime.Inst.DispatchRuntimeCode(() => {
                        objRef.SetProperty(JavaScriptPropertyId.FromString("status"), JavaScriptValue.FromInt32((int)responseHeaders.StatusCode), true);
                        string statusText = $"{(int)responseHeaders.StatusCode} {responseHeaders.ReasonPhrase}";
                        objRef.SetProperty(JavaScriptPropertyId.FromString("statusText"), JavaScriptValue.FromString(statusText), true);
                    });
                    ReadyState = ReadyStates.Headers_Received;

                    // ReadyState = ReadyStates.Loading;  // ignore, we don't do partial responses
                    var content = await responseHeaders.Content.ReadAsStringAsync();
                    ProjectRuntime.Inst.DispatchRuntimeCode(() => {
                        objRef.SetProperty(JavaScriptPropertyId.FromString("responseType"), JavaScriptValue.FromString("text"), true);
                        objRef.SetProperty(JavaScriptPropertyId.FromString("response"), JavaScriptValue.FromString(content), true);
                        objRef.SetProperty(JavaScriptPropertyId.FromString("responseText"), JavaScriptValue.FromString(content), true);

                        var callback = objRef.Get("onload");
                        if (callback.ValueType == JavaScriptValueType.Function)
                        {
                            callback.CallFunction(callback);
                        }
                    });
                } catch (Exception e) {
                    ProjectRuntime.Inst.DispatchRuntimeCode(() => {
                        var callback = objRef.Get("onerror");
                        if (callback.ValueType == JavaScriptValueType.Function)
                        {
                            callback.CallFunction(callback);
                        }
                    });
                }
                ReadyState     = ReadyStates.Done;
                requestMessage = null;
                requestTask    = null;
            });

            /*
             * cancellationTokenSource = new CancellationTokenSource();
             * requestTask = Task.Run(async () => {
             *  var response = await request;//.AsTask(cancellationTokenSource.Token);
             *  //response.
             *  int dsa = 0;
             * });
             */
        }
Example #3
0
        private void OnFrameCollection(JavaScriptValue reference, IntPtr callbackState)
        {
            var frameID = reference.Get("id").ToInt32();

            if (frameID != -1)
            {
                SceneCameraManager.Inst.RemoveFrameFromCache(frameID);
            }
        }
        public static Vector3 ToVector3(this JavaScriptValue v)
        {
            switch (v.ValueType)
            {
            case JavaScriptValueType.Array:
                var length = v.Length();
                if (!length.HasValue || length.Value != 3)
                {
                    return(new Vector3(0, 0, 0));
                }
                return(new Vector3(
                           (float)v.Get(0).ToDouble(),
                           (float)v.Get(1).ToDouble(),
                           (float)v.Get(2).ToDouble()
                           ));

            case JavaScriptValueType.Object:
                if (v.Has("x") && v.Has("y") && v.Has("z"))
                {
                    return(new Vector3(
                               (float)v.Get("x").ConvertToNumber().ToDouble(),
                               (float)v.Get("y").ConvertToNumber().ToDouble(),
                               (float)v.Get("z").ConvertToNumber().ToDouble()
                               ));
                }
                else
                {
                    return(new Vector3(0, 0, 0));
                }

            default:
                return(new Vector3(0, 0, 0));
            }
        }
        public static Color ToColor(this JavaScriptValue v)
        {
            switch (v.ValueType)
            {
            case JavaScriptValueType.String:
                var s = v.ToString();
                // TODO: do this
                return(Color.White);

            case JavaScriptValueType.Object:
                if (v.Has("r") && v.Has("g") && v.Has("b"))
                {
                    var a = v.Has("a") ? v.Get("a").ConvertToNumber().ToInt32() : 255;
                    return(Color.FromByteFormat(
                               (byte)v.Get("r").ConvertToNumber().ToInt32(),
                               (byte)v.Get("g").ConvertToNumber().ToInt32(),
                               (byte)v.Get("b").ConvertToNumber().ToInt32(),
                               (byte)a));
                }
                else
                {
                    return(Color.White);
                }

            case JavaScriptValueType.Array:
                var length = v.Length();
                if (length == 4)
                {
                    return(Color.FromByteFormat(
                               (byte)v.Get(0).ConvertToNumber().ToInt32(),
                               (byte)v.Get(1).ConvertToNumber().ToInt32(),
                               (byte)v.Get(2).ConvertToNumber().ToInt32(),
                               (byte)v.Get(3).ConvertToNumber().ToInt32()));
                }
                else if (length == 3)
                {
                    return(Color.FromByteFormat(
                               (byte)v.Get(0).ConvertToNumber().ToInt32(),
                               (byte)v.Get(1).ConvertToNumber().ToInt32(),
                               (byte)v.Get(2).ConvertToNumber().ToInt32(),
                               0));
                }
                else
                {
                    return(Color.White);
                }

            default:
                return(Color.White);
            }
        }
Example #6
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();
                });
            });
        }