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();
                });
            });
        }
        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);
            }
        }
Exemple #4
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();
                });
            });
        }