Example #1
0
        /// <summary>
        /// Deserializes the object out of a JSON dynamic object.
        /// </summary>
        /// <param name="jsonObject">the JSON dynamic object</param>
        /// <returns>the deserialized object</returns>
        public static ConceptOutputInfo Deserialize(dynamic jsonObject)
        {
            List <Concept> concepts = null;

            if (jsonObject.data != null)
            {
                if (jsonObject.data.concepts != null)
                {
                    concepts = new List <Concept>();
                    foreach (var concept in jsonObject.data.concepts)
                    {
                        concepts.Add(Concept.Deserialize(concept));
                    }
                }
            }
            bool areConceptsMutuallyExclusive = false;
            bool isEnvironmentClosed          = false;

            if (jsonObject.output_config != null)
            {
                areConceptsMutuallyExclusive = jsonObject.output_config.concepts_mutually_exclusive;
                isEnvironmentClosed          = jsonObject.output_config.closed_environment;
            }
            return(new ConceptOutputInfo(
                       (string)jsonObject.type,
                       (string)jsonObject.type_ext,
                       (string)jsonObject.message,
                       concepts,
                       areConceptsMutuallyExclusive,
                       isEnvironmentClosed,
                       (string)jsonObject.language));
        }
        /// <summary>
        /// Deserializes the object out of a JSON dynamic object.
        /// </summary>
        /// <param name="jsonObject">the JSON dynamic object</param>
        /// <returns>the deserialized object</returns>
        public new static ClarifaiURLVideo Deserialize(dynamic jsonObject)
        {
            var positiveConcepts = new List <Concept>();
            var negativeConcepts = new List <Concept>();

            if (jsonObject.data.concepts != null)
            {
                foreach (dynamic c in jsonObject.data.concepts)
                {
                    var concept = Concept.Deserialize(c);
                    if (concept.Value == 0.0M)
                    {
                        negativeConcepts.Add(concept);
                    }
                    else
                    {
                        positiveConcepts.Add(concept);
                    }
                }
            }
            JObject metadata = null;

            if (jsonObject.data.metadata != null)
            {
                metadata = (JObject)jsonObject.data.metadata;
            }
            GeoPoint geoPoint = null;

            if (jsonObject.data.geo != null)
            {
                geoPoint = GeoPoint.Deserialize(jsonObject.data.geo);
            }
            DateTime?createdAt = null;

            if (jsonObject.created_at != null)
            {
                createdAt = (DateTime)jsonObject.created_at;
            }

            ClarifaiStatus status = null;

            if (jsonObject.status != null)
            {
                status = ClarifaiStatus.Deserialize(jsonObject.status);
            }

            return(new ClarifaiURLVideo(
                       id: (string)jsonObject.id,
                       url: (string)jsonObject.data.video.url,
                       positiveConcepts: positiveConcepts,
                       negativeConcepts: negativeConcepts,
                       metadata: metadata,
                       createdAt: createdAt,
                       geo: geoPoint,
                       status: status));
        }
        protected static List <Concept> DeserializePredictions(dynamic jsonObject)
        {
            var propertyValues = (JObject)jsonObject.data;

            var data = new List <Concept>();

            if (propertyValues.Count > 0)
            {
                foreach (dynamic concept in jsonObject.data.concepts)
                {
                    data.Add(Concept.Deserialize(concept));
                }
            }
            return(data);
        }
Example #4
0
        public void ConceptShouldBeCorrectlyDeserialized()
        {
            var     json    = JsonConvert.DeserializeObject <dynamic>(@"
{
    ""id"": ""ai_HLmqFqBf"",
    ""name"": ""train"",
    ""value"": 0.9989112,
    ""app_id"": ""main""
}");
            Concept concept = Concept.Deserialize(json);

            Assert.AreEqual("ai_HLmqFqBf", concept.ID);
            Assert.AreEqual("train", concept.Name);
            Assert.AreEqual(0.9989112, concept.Value);
            Assert.AreEqual("main", concept.AppID);
        }
Example #5
0
        /// <summary>
        /// Deserializes the object out of a JSON dynamic object.
        /// </summary>
        /// <param name="jsonObject">the JSON dynamic object</param>
        /// <returns>the deserialized object</returns>
        public new static ClarifaiFileVideo Deserialize(dynamic jsonObject)
        {
            var positiveConcepts = new List <Concept>();
            var negativeConcepts = new List <Concept>();

            if (jsonObject.data.concepts != null)
            {
                foreach (dynamic c in jsonObject.data.concepts)
                {
                    var concept = Concept.Deserialize(c);
                    if (concept.Value == 0.0M)
                    {
                        negativeConcepts.Add(concept);
                    }
                    else
                    {
                        positiveConcepts.Add(concept);
                    }
                }
            }
            JObject metadata = null;

            if (jsonObject.data.metadata != null)
            {
                metadata = (JObject)jsonObject.data.metadata;
            }
            GeoPoint geoPoint = null;

            if (jsonObject.data.geo != null)
            {
                geoPoint = GeoPoint.Deserialize(jsonObject.data.geo);
            }
            DateTime?createdAt = null;

            if (jsonObject.created_at != null)
            {
                createdAt = (DateTime)jsonObject.created_at;
            }
            return(new ClarifaiFileVideo(
                       bytes: Convert.FromBase64String((string)jsonObject.data.video.base64),
                       id: (string)jsonObject.id,
                       positiveConcepts: positiveConcepts,
                       negativeConcepts: negativeConcepts,
                       metadata: metadata,
                       createdAt: createdAt,
                       geo: geoPoint));
        }
Example #6
0
        /// <summary>
        /// Deserializes the object out of a JSON dynamic object.
        /// </summary>
        /// <param name="jsonObject">the JSON dynamic object</param>
        /// <returns>the deserialized object</returns>
        public new static ClarifaiFileImage Deserialize(dynamic jsonObject)
        {
            var positiveConcepts = new List <Concept>();
            var negativeConcepts = new List <Concept>();

            if (jsonObject.data.concepts != null)
            {
                foreach (dynamic c in jsonObject.data.concepts)
                {
                    var concept = Concept.Deserialize(c);
                    if (concept.Value == 0.0M)
                    {
                        negativeConcepts.Add(concept);
                    }
                    else
                    {
                        positiveConcepts.Add(concept);
                    }
                }
            }
            Crop crop = null;

            if (jsonObject.data.image.crop != null)
            {
                crop = DTOs.Crop.Deserialize(jsonObject.data.image.crop);
            }
            JObject metadata = null;

            if (jsonObject.data.metadata != null)
            {
                metadata = (JObject)jsonObject.data.metadata;
            }
            GeoPoint geoPoint = null;

            if (jsonObject.data.geo != null)
            {
                geoPoint = GeoPoint.Deserialize(jsonObject.data.geo);
            }
            DateTime?createdAt = null;

            if (jsonObject.created_at != null)
            {
                createdAt = (DateTime)jsonObject.created_at;
            }

            var regions = new List <Region>();

            if (jsonObject.data != null && jsonObject.data.regions != null)
            {
                foreach (dynamic region in jsonObject.data.regions)
                {
                    regions.Add(Region.Deserialize(region));
                }
            }
            return(new ClarifaiFileImage(
                       bytes: Convert.FromBase64String((string)jsonObject.data.image.base64),
                       id: (string)jsonObject.id,
                       positiveConcepts: positiveConcepts,
                       negativeConcepts: negativeConcepts,
                       crop: crop,
                       metadata: metadata,
                       createdAt: createdAt,
                       geo: geoPoint,
                       regions: regions));
        }
        protected static List <IPrediction> DeserializePredictions(ModelType modelType,
                                                                   dynamic jsonObject)
        {
            var propertyValues = (JObject)jsonObject.data;

            var data = new List <IPrediction>();

            if (propertyValues.Count > 0)
            {
                string typeName = modelType.Prediction.Name;
                switch (typeName)
                {
                case "Color":
                {
                    foreach (dynamic color in jsonObject.data.colors)
                    {
                        data.Add(Color.Deserialize(color));
                    }
                    break;
                }

                case "Concept":
                {
                    foreach (dynamic concept in jsonObject.data.concepts)
                    {
                        data.Add(Concept.Deserialize(concept));
                    }
                    break;
                }

                case "Demographics":
                {
                    foreach (dynamic demographics in jsonObject.data.regions)
                    {
                        data.Add(Demographics.Deserialize(demographics));
                    }
                    break;
                }

                case "Embedding":
                {
                    foreach (dynamic embedding in jsonObject.data.embeddings)
                    {
                        data.Add(Embedding.Deserialize(embedding));
                    }
                    break;
                }

                case "FaceConcepts":
                {
                    foreach (dynamic faceConcepts in
                             jsonObject.data.regions)
                    {
                        data.Add(FaceConcepts.Deserialize(faceConcepts));
                    }
                    break;
                }

                case "FaceDetection":
                {
                    foreach (dynamic faceDetection in jsonObject.data.regions)
                    {
                        data.Add(FaceDetection.Deserialize(faceDetection));
                    }
                    break;
                }

                case "FaceEmbedding":
                {
                    foreach (dynamic faceEmbedding in jsonObject.data.regions)
                    {
                        data.Add(FaceEmbedding.Deserialize(faceEmbedding));
                    }
                    break;
                }

                case "Focus":
                {
                    foreach (dynamic focus in jsonObject.data.regions)
                    {
                        data.Add(Focus.Deserialize(focus,
                                                   (decimal)jsonObject.data.focus.value));
                    }
                    break;
                }

                case "Frame":
                {
                    foreach (dynamic frame in jsonObject.data.frames)
                    {
                        data.Add(Frame.Deserialize(frame));
                    }
                    break;
                }

                case "Logo":
                {
                    foreach (dynamic logo in jsonObject.data.regions)
                    {
                        data.Add(Logo.Deserialize(logo));
                    }
                    break;
                }

                default:
                {
                    throw new ClarifaiException(
                              string.Format("Unknown output type `{0}`", typeName));
                }
                }
            }
            return(data);
        }