예제 #1
0
 private WorkflowResult(ClarifaiStatus status, ClarifaiInput input,
                        List <ClarifaiOutput> predictions)
 {
     Status      = status;
     Input       = input;
     Predictions = predictions;
 }
예제 #2
0
        /// <inheritdoc />
        public async Task <ClarifaiResponse <T> > ExecuteAsync()
        {
            HttpResponseMessage response;
            string responseContent;

            try
            {
                response = await HttpRequest();

                responseContent = await response.Content.ReadAsStringAsync();
            }
            catch (HttpRequestException ex)
            {
                return(new ClarifaiResponse <T>(
                           new ClarifaiStatus(ClarifaiStatus.StatusType.NetworkError, 404,
                                              "HTTP request failed.", ex.Message),
                           HttpStatusCode.NotFound, "", default(T)));
            }

            dynamic jsonObject;

            try
            {
                jsonObject = JsonConvert.DeserializeObject <dynamic>(responseContent);
            }
            catch (JsonException ex)
            {
                return(new ClarifaiResponse <T>(
                           new ClarifaiStatus(ClarifaiStatus.StatusType.NetworkError,
                                              (int)response.StatusCode, "Server provided a malformed JSON response.",
                                              ex.Message),
                           response.StatusCode, responseContent, default(T)));
            }

            dynamic        statusJsonObject = jsonObject.status;
            ClarifaiStatus status           = ClarifaiStatus.Deserialize(statusJsonObject,
                                                                         response.StatusCode);

            if (status.Type == ClarifaiStatus.StatusType.Successful ||
                status.Type == ClarifaiStatus.StatusType.MixedSuccess)
            {
                var deserializedResponse = Unmarshaller(jsonObject);
                return(new ClarifaiResponse <T>(status, response.StatusCode, responseContent,
                                                deserializedResponse));
            }
            else
            {
                T deserializedResponse;
                try
                {
                    deserializedResponse = Unmarshaller(jsonObject);
                }
                catch (Exception)
                {
                    deserializedResponse = default(T);
                }
                return(new ClarifaiResponse <T>(status, response.StatusCode, responseContent,
                                                deserializedResponse));
            }
        }
예제 #3
0
 /// <summary>
 /// Ctor.
 /// </summary>
 /// <param name="status">the response status</param>
 /// <param name="httpCode">the HTTP code</param>
 /// <param name="rawBody">the raw body</param>
 /// <param name="deserialized">the deserialized object</param>
 public ClarifaiResponse(ClarifaiStatus status, HttpStatusCode httpCode, string rawBody,
                         T deserialized)
 {
     Status        = status;
     HttpCode      = httpCode;
     RawBody       = rawBody;
     _deserialized = deserialized;
 }
예제 #4
0
 /// <summary>
 /// Ctor.
 /// </summary>
 /// <param name="id">the output ID</param>
 /// <param name="status">the output status</param>
 /// <param name="createdAt">date & time of output creation</param>
 /// <param name="input">the input</param>
 /// <param name="data">the data</param>
 protected ClarifaiOutput(string id, ClarifaiStatus status, DateTime createdAt,
                          IClarifaiInput input, List <IPrediction> data)
 {
     ID        = id;
     Status    = status;
     CreatedAt = createdAt;
     Input     = input;
     Data      = data;
 }
예제 #5
0
 /// <summary>
 /// Ctor.
 /// </summary>
 /// <param name="url">the video URL</param>
 /// <param name="id">the ID</param>
 /// <param name="positiveConcepts">the concepts associated with the video</param>
 /// <param name="negativeConcepts">the concepts not associated with the video</param>
 /// <param name="metadata">the video's optional metadata by which you can search</param>
 /// <param name="createdAt">the date & time of video's creation</param>
 /// <param name="geo">input's geographical point</param>
 /// <param name="status">the status</param>
 public ClarifaiURLVideo(string url, string id = null,
                         IEnumerable <Concept> positiveConcepts = null,
                         IEnumerable <Concept> negativeConcepts = null, JObject metadata = null,
                         DateTime?createdAt = null, GeoPoint geo = null, ClarifaiStatus status = null)
     : base(InputType.Video, InputForm.URL, id, positiveConcepts, negativeConcepts, metadata,
            createdAt, geo, null, status)
 {
     URL = url;
 }
예제 #6
0
 /// <summary>
 /// Ctor.
 /// </summary>
 /// <param name="bytes">the image bytes</param>
 /// <param name="id">the ID</param>
 /// <param name="positiveConcepts">the concepts associated with the image</param>
 /// <param name="negativeConcepts">the concepts not associated with the image</param>
 /// <param name="metadata">the video's optional metadata by which you can search</param>
 /// <param name="createdAt">the date & time of video's creation</param>
 /// <param name="geo">input's geographical point</param>
 /// <param name="status">the status</param>
 public ClarifaiFileVideo(byte[] bytes, string id = null,
                          IEnumerable <Concept> positiveConcepts = null,
                          IEnumerable <Concept> negativeConcepts = null, JObject metadata = null,
                          DateTime?createdAt = null, GeoPoint geo = null, ClarifaiStatus status = null)
     : base(InputType.Video, InputForm.File, id, positiveConcepts, negativeConcepts,
            metadata, createdAt, geo, null, status)
 {
     _bytes = bytes;
 }
예제 #7
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 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));
        }
 /// <summary>
 /// Ctor.
 /// </summary>
 /// <param name="id">the output ID</param>
 /// <param name="status">the output status</param>
 /// <param name="createdAt">date & time of output creation</param>
 /// <param name="input">the input</param>
 /// <param name="data">the data</param>
 protected ModerationOutput(string id, ClarifaiStatus status, DateTime createdAt,
                            IClarifaiInput input, List <Concept> data, ModerationStatus moderationStatus)
 {
     ID               = id;
     Status           = status;
     CreatedAt        = createdAt;
     Input            = input;
     Data             = data;
     ModerationStatus = moderationStatus;
 }
예제 #9
0
        /// <summary>
        /// Deserializes the object out of a JSON dynamic object.
        /// </summary>
        /// <param name="modelType">the model type</param>
        /// <param name="jsonObject">the JSON object</param>
        /// <returns>the deserialized object</returns>
        public static ClarifaiOutput Deserialize(ModelType modelType, dynamic jsonObject)
        {
            dynamic data = DeserializePredictions(modelType, jsonObject);

            return(new ClarifaiOutput(
                       (string)jsonObject.id,
                       ClarifaiStatus.Deserialize(jsonObject.status),
                       (DateTime)jsonObject.created_at,
                       jsonObject.input != null ? ClarifaiInput.Deserialize(jsonObject.input) : null,
                       data));
        }
        /// <summary>
        /// Deserializes the object out of a JSON dynamic object.
        /// </summary>
        /// <param name="modelType">the model type</param>
        /// <param name="jsonObject">the JSON object</param>
        /// <returns>the deserialized object</returns>
        public static ModerationOutput Deserialize(dynamic jsonObject)
        {
            List <Concept> data = DeserializePredictions(jsonObject);

            return(new ModerationOutput(
                       (string)jsonObject.id,
                       ClarifaiStatus.Deserialize(jsonObject.status),
                       (DateTime)jsonObject.created_at,
                       jsonObject.input != null ? ClarifaiInput.Deserialize(jsonObject.input) : null,
                       data,
                       Clarifai.Solutions.Moderation.DTOs.ModerationStatus.Deserialize(
                           jsonObject.moderation.status
                           )));
        }
예제 #11
0
 protected ClarifaiInput(InputType type, InputForm form, string id,
                         IEnumerable <Concept> positiveConcepts, IEnumerable <Concept> negativeConcepts,
                         JObject metadata, DateTime?createdAt, GeoPoint geo, List <Region> regions,
                         ClarifaiStatus status)
 {
     Type             = type;
     Form             = form;
     ID               = id;
     PositiveConcepts = positiveConcepts;
     NegativeConcepts = negativeConcepts;
     Metadata         = metadata;
     CreatedAt        = createdAt;
     Geo              = geo;
     Regions          = regions;
     Status           = status;
 }
예제 #12
0
        /// <summary>
        /// Ctor.
        /// </summary>
        /// <param name="bytes">the image bytes</param>
        /// <param name="id">the ID</param>
        /// <param name="positiveConcepts">the concepts associated with the image</param>
        /// <param name="negativeConcepts">the concepts not associated with the image</param>
        /// <param name="metadata">the video's optional metadata by which you can search</param>
        /// <param name="createdAt">the date & time of video's creation</param>
        /// <param name="geo">input's geographical point</param>
        /// <param name="crop">(deprecated) the crop</param>
        /// <param name="regions">the regions</param>
        /// <param name="status">the status</param>
        public ClarifaiFileImage(byte[] bytes, string id = null,
                                 IEnumerable <Concept> positiveConcepts = null,
                                 IEnumerable <Concept> negativeConcepts = null, JObject metadata = null,
                                 DateTime?createdAt    = null, GeoPoint geo          = null, Crop crop = null,
                                 List <Region> regions = null, ClarifaiStatus status = null)
            : base(InputType.Image, InputForm.File, id, positiveConcepts, negativeConcepts,
                   metadata, createdAt, geo, regions, status)
        {
            _bytes = bytes;
            Crop   = crop;

            if (crop != null)
            {
                throw new ClarifaiException(
                          "The `crop` argument is not used/supported by any more by ClarifaiFileImage.");
            }
        }
예제 #13
0
        /// <summary>
        /// Deserializes the object out of a JSON dynamic object.
        /// </summary>
        /// <param name="httpClient">the HTTP client</param>
        /// <param name="jsonObject">the JSON object</param>
        /// <returns>the deserialized object</returns>
        public static WorkflowResult Deserialize(IClarifaiHttpClient httpClient, dynamic jsonObject)
        {
            var status = ClarifaiStatus.Deserialize(jsonObject.status);
            var input  = ClarifaiInput.Deserialize(jsonObject.input);

            var predictions = new List <ClarifaiOutput>();

            foreach (dynamic output in jsonObject.outputs)
            {
                dynamic   model     = output.model;
                ModelType modelType = ModelType.DetermineModelType(
                    (string)model.output_info.type_ext);

                predictions.Add(ClarifaiOutput.Deserialize(httpClient, modelType, output));
            }

            return(new WorkflowResult(status, input, predictions));
        }
예제 #14
0
        /// <summary>
        /// Ctor.
        /// </summary>
        /// <param name="url">the image URL</param>
        /// <param name="id">the ID</param>
        /// <param name="allowDuplicateUrl">should allow duplicate URLs</param>
        /// <param name="positiveConcepts">the concepts associated with the image</param>
        /// <param name="negativeConcepts">the concepts not associated with the image</param>
        /// <param name="metadata">the image's optional metadata by which you can search</param>
        /// <param name="createdAt">the date & time of image's creation</param>
        /// <param name="geo">input's geographical point</param>
        /// <param name="crop">(deprecated) the crop</param>
        /// <param name="regions">the regions</param>
        /// <param name="status">the status</param>
        public ClarifaiURLImage(string url, string id = null, bool?allowDuplicateUrl = null,
                                IEnumerable <Concept> positiveConcepts = null,
                                IEnumerable <Concept> negativeConcepts = null, JObject metadata = null,
                                DateTime?createdAt    = null, GeoPoint geo          = null, Crop crop = null,
                                List <Region> regions = null, ClarifaiStatus status = null)
            : base(InputType.Image, InputForm.URL, id, positiveConcepts, negativeConcepts,
                   metadata, createdAt, geo, regions, status)
        {
            URL = url;
            AllowDuplicateUrl = allowDuplicateUrl;
            Crop = crop;

            if (crop != null)
            {
                throw new ClarifaiException(
                          "The `crop` argument is not used/supported by any more by ClarifaiURLImage.");
            }
        }
예제 #15
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);
                    }
                }
            }
            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));
                }
            }

            ClarifaiStatus status = null;

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

            return(new ClarifaiFileImage(
                       bytes: Convert.FromBase64String((string)jsonObject.data.image.base64),
                       id: (string)jsonObject.id,
                       positiveConcepts: positiveConcepts,
                       negativeConcepts: negativeConcepts,
                       metadata: metadata,
                       createdAt: createdAt,
                       geo: geoPoint,
                       regions: regions,
                       status: status));
        }