Exemple #1
0
        private async static Task AddImage(ImorImage imorImage, ILogger log)
        {
            HttpClient client = new HttpClient();

            var endpoint = Environment.GetEnvironmentVariable("SparqlEndpoint");

            var content = new StringContent(
                JsonConvert.SerializeObject(imorImage),
                Encoding.UTF8,
                "application/json");

            var results = await client.PostAsync(endpoint + "/images/create", content);

            log.LogInformation(results.ToString());

            try
            {
                results.EnsureSuccessStatusCode();
            }
            catch (Exception ex)
            {
                log.LogError(ex.Message);
                throw ex;
            }
        }
Exemple #2
0
        public static async Task Run(
            [EventGridTrigger] EventGridEvent eventGridEvent,
            [Blob("{data.url}", FileAccess.Read, Connection = "imorblobstorage_STORAGE")] Stream input,
            ILogger log)
        {
            try
            {
                if (input != null)
                {
                    var createdEvent = ((JObject)eventGridEvent.Data).ToObject <StorageBlobCreatedEventData>();
                    var extension    = Path.GetExtension(createdEvent.Url);
                    var encoder      = GetEncoder(extension);

                    log.LogInformation($"C# Blob Trigger Function Processed Blob:\n Name: '{createdEvent.Url}'\n Size: {input.Length} Bytes");

                    if (encoder != null)
                    {
                        log.LogInformation($"Analyzing uploaded image '{createdEvent.Url}' ..");

                        ImageAnalysisInfo result = await AnalyzeImageAsync(createdEvent.Url, log);

                        log.LogInformation(" RESULT: RequestId: " + result.requestId);

                        log.LogInformation("Analyzing done successfully.");

                        log.LogInformation("Adding data to SPARQL Endpoint ..");

                        string imageName = Path.GetFileNameWithoutExtension(createdEvent.Url);

                        ImorImage imorImage = new ImorImage()
                        {
                            Uri         = "http://www.semanticweb.org/ImagesOntology#" + imageName,
                            Description = (result.description.captions.Length > 0) ? result.description.captions[0].text : "",
                            Content     = createdEvent.Url,
                            Tags        = result.description.tags
                        };

                        await AddImage(imorImage, log);

                        log.LogInformation("Adding data to SPARQL Endpoint done successfully.");
                    }
                    else
                    {
                        log.LogError($"No encoder support for: {createdEvent.Url}");
                    }
                }
                else
                {
                    log.LogError("No input stream.");
                }
            }
            catch (Exception ex)
            {
                log.LogError(ex.Message);
                throw ex;
            }
        }
        public void InsertImage(ImorImage image)

        {
            var node = graph.CreateUriNode(new Uri(image.Uri));

            var typeNode = graph.GetUriNode(new Uri(ImorEnum.RdfType));

            var imageNode = graph.GetUriNode(new Uri(ImorEnum.Image));

            var t = new Triple(node, typeNode, imageNode);

            if (!string.IsNullOrEmpty(image.Description))
            {
                var descriptionNode = graph.GetUriNode(new Uri(ImorEnum.Description));

                var descriptionTriple = new Triple(node, descriptionNode, graph.CreateLiteralNode(image.Description));

                graph.Assert(descriptionTriple);
            }

            if (!string.IsNullOrEmpty(image.Content))
            {
                var contentNode = graph.GetUriNode(new Uri(ImorEnum.Content));

                var contenTriple = new Triple(node, contentNode, graph.CreateLiteralNode(image.Content));

                graph.Assert(contenTriple);
            }

            if (image.Tags.Any())
            {
                foreach (var tag in image.Tags)
                {
                    var tagNode = graph.GetUriNode(new Uri(ImorEnum.GetUri(tag.Uri)));

                    if (tagNode != null)
                    {
                        var hasNode = graph.GetUriNode(new Uri(ImorEnum.HasA));

                        var tagTriple = new Triple(node, hasNode, tagNode);

                        graph.Assert(tagTriple);
                    }
                }
            }

            graph.Assert(t);

            var sw = new StringWriter(new StringBuilder(DatabaseInitializer.ontology));

            graph.SaveToStream(sw, new CompressingTurtleWriter());
        }
Exemple #4
0
        private ImorImage MapImage(string imageUri, IEnumerable <Triple> triples)
        {
            var imorImage = new ImorImage
            {
                Uri = imageUri
            };

            foreach (var imageProperty in triples)
            {
                var propertyName = ImorImage.RdfPropertiesDictionary.SingleOrDefault(x => x.Key == imageProperty.Predicate.ToString());

                var properties = imorImage.GetType().GetProperties();

                foreach (var property in properties)
                {
                    if (property.Name == propertyName.Value)
                    {
                        property.SetValue(imorImage, imageProperty.Object.AsValuedNode().AsString());
                    }
                }
            }

            return(imorImage);
        }